java-使用Tuckey UrlRewriteFilter重写SpringMVC url
作者:互联网
我该如何在SpringMVC中使用tuckey’s UrlRewriteFilter重写URL,如下所示:
从
HTTP://本地主机:8080 /演示/ api.do方法=&的getUser放大器; UID = 1001
至
HTTP://本地主机:8080 /演示/ API /用户/ 1001
控制器片段:
@Controller
@RequestMapping("/api.do")
public class APIController {
@RequestMapping(params = "method=getUser")
public String methodName(HttpServletRequest request) {
//CODE BLOCK
}
}
解决方法:
如果您使用的是Maven,请添加依赖项
<dependency>
<groupId>org.tuckey</groupId>
<artifactId>urlrewritefilter</artifactId>
<version>4.0.3</version>
</dependency>
在web.xml中启用过滤器
<filter>
<filter-name>UrlRewriteFilter</filter-name>
<filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>UrlRewriteFilter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>
将以下规则添加到WEB-INF / urlrewrite.xml
<urlrewrite>
<rule>
<from>^/demo/api.do?method=getUser&uid=(.*)$</from>
<to>/demo/api/user/$1</to>
</rule>
</urlrewrite>
标签:restful-url,java,spring-mvc,url-rewriting,tuckey-urlrewrite-filter 来源: https://codeday.me/bug/20191009/1878606.html