Jersey Servlet:RequestDispatcher返回404

时间:2017-08-29 12:50:47

标签: java rest servlets jersey requestdispatcher

我需要将请求转发给REST服务并生成此代码:

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);

}

服务“/ restService”还有一个处理请求的servlet,其定义如下:

<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>

RESTApplication类具有以下代码:

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

“com.rest.resources”中有几个类映射到请求附带的路径(/ resources / *)。 但是当“getRequestDispatcher”转发请求时,Jersey会抛出404错误。看来泽西岛无法将我的请求映射到正确的servlet。如果我使用Postman粘贴相同的URL,则会正常处理请求。调度员的检查是: enter image description here

为了验证问题是否与Jersey有关,我已经创建了一个从HttpServlet扩展而未提交给Jersey的Servlet:

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

在此之后,我执行了这段代码:

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);
}

令人惊讶的是,这个前向实现的类helloResource被置于“com.rest.resources”中。

有没有人遇到类似的事情? “getRequestDispacther”如何找到并转发“/ resources / ”中存在的servlet?*

使用的版本: 球衣2.6 servlet 2.5 jboss 4.2.3-GA

1 个答案:

答案 0 :(得分:0)

泽西岛似乎希望“getRequestDispatcher”能够实现必须处理的URL,而不是将其委托给映射的servlet。

这个问题解决了这个问题:

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

此后,请求由放入“com.rest.resources”的资源处理。