这是我当前的 Guice 配置:
public class MyServletModule extends ServletModule {
@Override
protected void configureServlets() {
bind(MyRest.class);
serveRegex(".+(?<!\\.(html|css|png|jpg))")
.with(HttpServletDispatcher.class);
}
}
但是我希望我的 Rest 资源只能以 http://127.0.0.1:8888/{hashcode_or_filename}
的形式访问,并且是接受和处理的唯一形式(好吧,加上/create下面的方法)。
现在,我可以在此路径模式中正确处理哈希码和文件名。
但是我不确定如何处理下面的类型或场景,其中客户端请求未映射的路径,在我的情况下会返回:
Could not find resource for relative : /examples/foo of full path:
http://127.0.0.1:8888/examples/foo
或
Could not find resource for relative : /examples/bar/foo of full path:
http://127.0.0.1:8888/examples/bar/foo
我需要的是能够处理未映射的路径,这样我就可以返回一个错误的 HTML 页面或其他内容,而不是在浏览器中显示这些错误文本。
另外如果请求是:http://127.0.0.1:8888/
我需要转发到http://127.0.0.1:8888/index.html
自动。现在我必须手动将 index.html 放在尾部。
我的 Resteasy 资源仅配置或连接:
@Singleton
@Path("/")
public class MyRest {
@GET
@Path({hashcode})
public Response getSomething(...){}
@POST
@Path("create")
public Response createSomething(...){}
}
最佳答案
最简单的方法是注册过滤器来处理错误代码不是 200 (OK) 的响应。或者添加到您的 web.xml 中,如下所示:
<error-page>
<error-code>404</error-code>
<location>/ErrorPage.jsp</location>
</error-page>
Also if the request is:
http://127.0.0.1:8888/
I need to forward tohttp://127.0.0.1:8888/index.html
automatically. As right now I have to manually put the index.html in the tail.
您可以使用此模块 http://tuckey.org/urlrewrite/
WEB-INF/web.xml
<filter>
<filter-name>UrlRewriteFilter</filter-name>
<filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
<init-param>
<param-name>confPath</param-name>
<param-value>/WEB-INF/urlrewrite.xml</param-value>
</init-param>
<!--...omitted...-->
</filter>
<filter-mapping>
<filter-name>UrlRewriteFilter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>
WEB-INF/urlrewrite.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE urlrewrite
PUBLIC "-//tuckey.org//DTD UrlRewrite 4.0//EN"
"http://www.tuckey.org/res/dtds/urlrewrite4.0.dtd">
<urlrewrite>
<rule match-type="regex">
<from>^/$</from>
<to type="redirect">/index.html</to>
</rule>
</urlrewrite>
关于java - 处理未映射的休息路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15874698/