简体   繁体   中英

How to dispatch servlet requests to Jersey resource methods

I am wondering if it is possible to dispatch a request from a servlet to a Jersey (JAX-RS implementation) resource class. I am trying to do it but it doesn't seem to work and according to my logging, the jersey resource is never reached.

Code examples are below. Is what I'm trying to do impossible for some reason?

Please note, the Jersey resource works correctly when I access it directly in a web browser via the address bar.

Also please note that 'RequestDispatcher.forward()' works as expected. It is just 'include' that doesn't.

The servlet

//The Jersey dispatcher url-filter is set to '/api/*'
String servletID = "/api/items";
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(servletID);
dispatcher.include(request, response);    

The Jersey resource

@GET @Path("/items")
@Produces ({MediaType.TEXT_XML})
public JAXBElement<Items> getItems(@PathParam("project") String project) throws       IOException, JAXBException {

    log.debug("reached getItems");

    //Omitted code that returns 'Items' object wrapped in JAXBElement

}

Relevant parts of web.xml

<servlet>
    <servlet-name>jerseyDispatcher</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    <init-param>
        <param-name>jersey.config.server.provider.packages</param-name>
        <param-value>uk.co.web.api.resource</param-value>
    </init-param>
</servlet>

<servlet-mapping>
    <servlet-name>jerseyDispatcher</servlet-name>
    <url-pattern>/api/*</url-pattern>
</servlet-mapping>

It is possible you forward the request.

HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
RequestDispatcher requestDispatcher = null;
requestDispatcher = httpServletRequest.getRequestDispatcher("/items");
dispatcher.forward(request, response);
return;

However note if you receive a GET request and try to forward to a POST resource, It will throw a 405 error.

Edit:

Let me understand what you are trying to achieve, if you need write content to response output stream you could use jersey Resource Filter.

 public class YourResourceFilter implements ResourceFilter
    {
    public ContainerRequestFilter getRequestFilter()
        {
            return new ContainerRequestFilter()
            {
                @Override
                public ContainerRequest filter(ContainerRequest containerRequest)
                {
//Pre- editing the request
                return containerRequest;
                }
            };
        }

    @Override
        public ContainerResponseFilter getResponseFilter()
        {
            return new ContainerResponseFilter()
            {
                @Override
                public ContainerResponse filter(ContainerRequest containerRequest, ContainerResponse containerResponse)
                {
// after the request has been completed by your jersey resource
                    return containerResponse;
                }
            };
        }

    }

I got it to work, sort of (Jersey 2.13) by configuring Jersey as a filter, and not a servlet, in web.xml. Then, you can tell the container to apply the filter to included requests too:

<filter-mapping>
    <filter-name>Jersey Web Application</filter-name>
    <url-pattern>/api/*</url-pattern>
    <dispatcher>FORWARD</dispatcher>
    <dispatcher>INCLUDE</dispatcher>
    <dispatcher>REQUEST</dispatcher>
</filter-mapping>

RequestDispatcher.include will then work for request handled by Jersey, too. There's a caveat, though. Jersey calls response.getOutputStream, so all output must be performed through said output stream - this rules out JSP pages, that use response.getWriter instead. So, unless you figure out how to work around the problem, forget about including a Jersey resource in a JSP page or, vice versa, including the result of evaluating a JSP as part of a REST response.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM