Jersey Servlet: RequestDispatcher returning 404

765 Views Asked by At

I need to forward a request to a REST Service and made this code:

private void performTask(HttpServletRequest request, HttpServletResponse response) throws ServletException,
IOException {
    ServletContext portalContext = this.getServletContext();
    ServletContext restService = portalContext.getContext("/restService");
    RequestDispatcher dispatcher = restService.getRequestDispatcher("/resources/*");
    dispatcher.forward(request, response);

}

Service "/restService" has also a servlet that processes the request and it's defined as below:

<servlet>
    <servlet-name>Jersey REST Service</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    <init-param>
        <param-name>javax.ws.rs.Application</param-name>
        <param-value>com.service.RESTApplication</param-value>
    </init-param>
    <init-param>
        <param-name>jersey.config.server.provider.packages</param-name>
        <param-value>com.rest.resources</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>Jersey REST Service</servlet-name>
    <url-pattern>/resources/*</url-pattern>
</servlet-mapping>

The class RESTApplication has this code:

public RESTApplication() {
    register(WadlFeature.class);
    register(JacksonFeature.class);
    register(MultiPartFeature.class);
    property(CommonProperties.FEATURE_AUTO_DISCOVERY_DISABLE, true);
}

There are several classes at "com.rest.resources" mapped to the path that comes with the request (/resources/*). But when the request is being forwarded by "getRequestDispatcher" Jersey throws a 404 error. It seems that Jersey can't map my request to the correct servlet. If I paste the same URL using Postman, the request is processed normally. The inspect of dispatcher is: enter image description here

In order to verify if the problem was with Jersey, I've created a Servlet that extends from HttpServlet and isn't submitted to Jersey:

<servlet>
    <servlet-name>HelloServlet</servlet-name>
    <servlet-class>com.rest.resources.helloResource</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>  

After this, I executed this code:

private void performTask(HttpServletRequest request, HttpServletResponse response) throws ServletException,
IOException {
    ServletContext portalContext = this.getServletContext();
    ServletContext restService = portalContext.getContext("/restService");
    RequestDispatcher dispatcher = restService.getRequestDispatcher("/hello");
    dispatcher.forward(request, response);
}

Surprisely, this forward achieved class helloResource that is placed into "com.rest.resources".

Does anyone faced something similar? How can "getRequestDispacther" find and forward the servlet existing at "/resources/"?*

Versions used: jersey 2.6 servlet 2.5 jboss 4.2.3-GA

1

There are 1 best solutions below

0
On

It seems that Jersey expects that "getRequestDispatcher" fulfill the URL that has to be processed instead of delegating this to the servlet mapped.

The problem was solved with this line:

RequestDispatcher dispatcher = datasulRest.getRequestDispatcher("/resources/" + request.getPathInfo());

After this, the request was processed by the resources placed into "com.rest.resources".