2013-03-25 87 views
0

你好,我是Spring的新手。 我正在开发一个Spring Web Api,并且在使用正则表达式解析URL时遇到问题。我已经看过以下职位:Spring Web Api,解析URLS

http://stackoverflow.com/questions/7841770/optional-path-variables-in-spring-mvc-requestmapping-uritemplate 
http://stackoverflow.com/questions/12516969/spring-mvc-getting-pathvariables-containing-dots-and-slashes 
http://stackoverflow.com/questions/8998419/requestmapping-annotation-in-spring-mvc 

,但我还没有找到一个尚未解决我的问题。 我希望我的所有请求都可以映射到单个方法,URL的长度可以变化,参数的数量也可以变化。我想捕捉整个URL与可变pathValue,而不是直到斜线/:

@RequestMapping(值=“{} pathValue”,方法= RequestMethod.GET)

所有的正则表达式,我有在Spring中捕获斜杠(/....../)的内容进行了测试,并且不考虑de剩余的URL。

重点是,我想解析一个单一的方法中的网址,这意味着所有请求都映射到该方法。 有什么办法可以在Spring中实现这一点?

非常感谢您的帮助和建议。

回答

0

如果你真的想将所有的请求分派给一个处理程序,那么你根本不需要拥有spring方法分派器。

相反,你可以有自己的请求处理程序

<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> 
    <property name="urlMap"> 
     <map> 
       <entry key="/**" value="myCatchAllResourceHandler" /> 
     </map> 
    </property> 
    <property name="order" value="100000" />  
</bean> 

<bean id="myCatchAllResourceHandler" name="myCatchAllResourceHandler" 
     class="MyCatchAllResourceHandler"> 
</bean> 

你必须实现自己的请求处理程序

public class MyCatchAllResourceHandler extends HttpRequestHandler() { 

    /** 
    * Process the given request, generating a response. 
    * @param request current HTTP request 
    * @param response current HTTP response 
    * @throws ServletException in case of general errors 
    * @throws IOException in case of I/O errors 
    */ 
    void handleRequest(HttpServletRequest request, HttpServletResponse response) 
        throws ServletException, IOException; 
     System.out.println("I get invoked");  
    } 
} 

但说实话,这几乎是像投掷所有的Spring MVC了!

+0

谢谢你的回应。但是,如果我使用Spring功能,我怎样才能让Spring过滤不正确的URLS并显示一个自定义错误页面,因为不正确的URLS的数量比正确的大。有没有办法映射不正确的URL?谢谢。 – amartin 2013-03-25 10:41:03

+0

你的意思是你想自定义“资源未找到”页面? – Ralph 2013-03-25 11:10:17

+0

我想过滤无效的URL,例如:portal/customer/item和门户/推荐/用户/产品/ book是有效的URLS,但门户/推荐不是有效的URL。我想有一个方法来确定一个URL是否正确。 – amartin 2013-03-25 11:58:54