How can I rewrite url in SpringMVC with tuckey's UrlRewriteFilter like this:
From
http://localhost:8080/demo/api.do?method=getUser&uid=1001
to
http://localhost:8080/demo/api/user/1001
The controller snippets:
@Controller
@RequestMapping("/api.do")
public class APIController {
@RequestMapping(params = "method=getUser")
public String methodName(HttpServletRequest request) {
//CODE BLOCK
}
}
Add a dependency if you're using maven
<dependency>
<groupId>org.tuckey</groupId>
<artifactId>urlrewritefilter</artifactId>
<version>4.0.3</version>
</dependency>
Enable the filter in your 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>
Add the following rule to WEB-INF/urlrewrite.xml
<urlrewrite>
<rule>
<from>^/demo/api.do?method=getUser&uid=(.*)$</from>
<to>/demo/api/user/$1</to>
</rule>
</urlrewrite>