处理未映射的休息路径 [英] Handling un-mapped Rest path
问题描述
这是我当前的Guice配置:
This is my current Guice configuration:
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方法).
However I want that my Rest resource is only access in form of http://127.0.0.1:8888/{hashcode_or_filename}
and the only form accepted and processed (well, plus the /create method below).
现在,我可以在此路径模式中正确处理哈希码和文件名.
Right now, I can deal with hashcode and filename properly in this path pattern.
但是我不确定在客户端请求未映射路径的情况下,如何处理下面的种类或情况,在我的情况下会返回此路径:
However I am not sure how to deal the kind or scenario below, where the client is requesting path that is not mapped, which returns this in my case:
找不到相对资源:/examples/foo的完整路径:
http://127.0.0.1:8888/examples/foo
Could not find resource for relative : /examples/foo of full path:
http://127.0.0.1:8888/examples/foo
或
找不到相对资源:/examples/bar/foo已满 路径:
http://127.0.0.1:8888/examples/bar/foo
Could not find resource for relative : /examples/bar/foo of full path:
http://127.0.0.1:8888/examples/bar/foo
我需要的是能够处理未映射的路径,以便我可以返回错误的HTML页面或其他内容,而不在浏览器中显示这些错误文本.
What I need is to be able to be able to handle unmapped paths so I can return a error HTML page or something and not show these error text in the browser.
如果请求是:http://127.0.0.1:8888/
,我也需要自动转发到http://127.0.0.1:8888/index.html
.现在,我必须手动将index.html放在尾部.
Also if the request is: http://127.0.0.1:8888/
I need to forward to http://127.0.0.1:8888/index.html
automatically. As right now I have to manually put the index.html in the tail.
我的Resteasy资源仅通过以下方式进行配置或接线:
My Resteasy resource is configure or wired with just:
@Singleton
@Path("/")
public class MyRest {
@GET
@Path({hashcode})
public Response getSomething(...){}
@POST
@Path("create")
public Response createSomething(...){}
}
推荐答案
最简单的方法是注册过滤器以处理错误代码不是200(确定)的响应.或在您的web.xml中添加如下内容:
Easiest way is to register filter to handle responses with error code other that 200 (OK). Or add to your web.xml something like this:
<error-page>
<error-code>404</error-code>
<location>/ErrorPage.jsp</location>
</error-page>
如果请求是:
http://127.0.0.1:8888/
我还需要转发给http://127.0.0.1:8888/index.html
自动.现在,我必须 手动将index.html放在尾部.
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>
这篇关于处理未映射的休息路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!