JAX-RS:以斜杠 (/) 结尾的路径会导致 404 错误
尝试访问 http://localhost:8080/enterprise/session/new/ 处的资源导致 HTTP 404(未找到)错误。尝试访问 http://localhost:8080/enterprise/session/new 上的相同资源正如预期的那样。
会话资源:
@GET
@Path("/new")
@Produces(MediaType.TEXT_HTML)
public Viewable newSession() {
Map<String, Object> map = new HashMap<String, Object>();
map.put("method","secEnterprise");
// create authentication form
return new Viewable("/session/new", map);
}
视图位于 WEB-INF\views\session\new.jsp
我怀疑部分问题与 web.xml 的部分有关:
<filter>
<filter-name>jersey</filter-name>
<filter-class>com.sun.jersey.spi.container.servlet.ServletContainer</filter-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.businessobjects.resources</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.config.property.WebPageContentRegex</param-name>
<!-- is this capturing the '/' and redirecting it to a non-existent view (.JSP)? -->
<param-value>/((WEB-INF/views))/.*</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.config.property.JSPTemplatesBasePath</param-name>
<param-value>/WEB-INF/views/</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.config.feature.Redirect</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.config.feature.FilterForwardOn404</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>jersey</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
我不确定这是否相关,但视图的形式POST 到 /session/session,而不仅仅是 /session:
<!-- new.jsp -->
...
<form method="post" action="session">
...
我缺少什么?
** 编辑 0 **
更改表单元素的操作属性以
<form method="post" action="/enterprise/session">
按预期工作。但是,结尾斜杠:
<form method="post" action="/enterprise/session/">
会导致前面提到的相同错误。
似乎需要更改正则表达式 /((WEB-INF/views))/.*
以忽略结尾斜杠 ('/')。
** 编辑 1 **
我有一个根资源:
@Path("/")
public class HomeResource {
@GET
public Response index() {
return Response.ok(new Viewable("/home/index")).build();
}
}
对 http://localhost:8080/enterprise 的请求会自动进行路由到 http://localhost:8080/enterprise/,因此自动重定向的某些方面工作正常。
Attempts to reach a resource at http://localhost:8080/enterprise/session/new/ result in a HTTP 404 (not found) error. Attempts to reach the same resource at http://localhost:8080/enterprise/session/new work as expected.
Session Resource:
@GET
@Path("/new")
@Produces(MediaType.TEXT_HTML)
public Viewable newSession() {
Map<String, Object> map = new HashMap<String, Object>();
map.put("method","secEnterprise");
// create authentication form
return new Viewable("/session/new", map);
}
View is located at WEB-INF\views\session\new.jsp
I suspect that part of the problem is related to the section of the web.xml:
<filter>
<filter-name>jersey</filter-name>
<filter-class>com.sun.jersey.spi.container.servlet.ServletContainer</filter-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.businessobjects.resources</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.config.property.WebPageContentRegex</param-name>
<!-- is this capturing the '/' and redirecting it to a non-existent view (.JSP)? -->
<param-value>/((WEB-INF/views))/.*</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.config.property.JSPTemplatesBasePath</param-name>
<param-value>/WEB-INF/views/</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.config.feature.Redirect</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.config.feature.FilterForwardOn404</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>jersey</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
I'm not sure if this is related, but the view's form POSTs to /session/session, rather than just to /session:
<!-- new.jsp -->
...
<form method="post" action="session">
...
What am I missing?
** edit 0 **
Changing the action attribute of the form element to
<form method="post" action="/enterprise/session">
works as expected. However, an ending slash:
<form method="post" action="/enterprise/session/">
results in the same error mentioned earlier.
It seems like the regex expression /((WEB-INF/views))/.*
needs to be changed to ignore ending slashes ('/').
** edit 1 **
I have a root resource:
@Path("/")
public class HomeResource {
@GET
public Response index() {
return Response.ok(new Viewable("/home/index")).build();
}
}
Requests to http://localhost:8080/enterprise are automatically routed to http://localhost:8080/enterprise/, so some aspects of the automatic redirection are working correctly.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不确定您使用的是哪种实现,但引用 Jersey 文档:
@Path 值可能或可能不以“/”开头,没有区别。同样,默认情况下,@Path 值可能以“/”结尾,也可能不以“/”结尾,这没有什么区别,因此以“/”结尾或不以“/”结尾的请求 URL 都会被匹配。但是,Jersey 有一个重定向机制,如果启用该机制,如果请求 URL 不以“/”结尾且匹配的 @Path 确实以“/”结尾,则自动重定向到以“/”结尾的请求 URL。
Not sure which implementation you are using but quoting from Jersey documentation:
A @Path value may or may not begin with a '/', it makes no difference. Likewise, by default, a @Path value may or may not end in a '/', it makes no difference, and thus request URLs that end or do not end in a '/' will both be matched. However, Jersey has a redirection mechanism, which if enabled, automatically performs redirection to a request URL ending in a '/' if a request URL does not end in a '/' and the matching @Path does end in a '/'.