您的位置:首页 > 理论基础 > 计算机网络

Spring MVC 使用HiddenHttpMethodFilter配置Rest风格的URL

2018-03-20 00:00 537 查看

RESTFUL格式

/** Rest风格的URL. 以CRUD为例: 新增:/orderPOST 修改:/order/1PUTupdate?id=1 获取:/order/1GETget?id=1 删除:/order/1DELETEdelete?id=1 如何发送PUT请求和DELETE请求呢? 1.需要在web.xml文件中配置HiddenHttpMethodFilter <!-- 配置org.springframework.web.filter.HiddenHttpMethodFilter:可以把POST请求转为DELETE或POST请求 --> <filter> <filter-name>HiddenHttpMethodFilter</filter-name> <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class> </filter> <filter-mapping> <filter-name>HiddenHttpMethodFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> 2.需要发送POST请求 3.需要在发送POST请求时携带一个name="_method"的隐藏域,值为DELETE或PUT jsp文件如下: <formaction="springmvc/testRest/1"method="post"> <inputtype="hidden"name="_method"value="PUT"/> <inputtype="submit"value="TestRestPUT"/> </form> <br><br> <formaction="springmvc/testRest/1"method="post"> <inputtype="hidden"name="_method"value="DELETE"/> <inputtype="submit"value="TestRestDELETE"/> </form> <br><br> <formaction="springmvc/testRest"method="post"> <inputtype="submit"value="TestRestPOST"/> </form> <br><br> <ahref="springmvc/testRest/1">TestRestGet</a> <br><br> 在SpringMVC的目标方法中如何得到id呢?使用@PathVariable注解 */ @RequestMapping(value="/testRest/{id}",method=RequestMethod.PUT) publicStringtestRestPut(@PathVariableIntegerid){ System.out.println("testRestPut:"+id); returnSUCCESS; } @RequestMapping(value="/testRest/{id}",method=RequestMethod.DELETE) publicStringtestRestDelete(@PathVariableIntegerid){ System.out.println("testRestDelete:"+id); returnSUCCESS; } @RequestMapping(value="/testRest",method=RequestMethod.POST) publicStringtestRest(){ System.out.println("testRestPOST"); returnSUCCESS; } @RequestMapping(value="/testRest/{id}",method=RequestMethod.GET) publicStringtestRest(@PathVariableIntegerid){ System.out.println("testRestGET:"+id); returnSUCCESS; } /** *@PathVariable可以来映射URL中的占位符到目标方法的参数中. *@paramid *@return */ @RequestMapping("/testPathVariable/{id}") publicStringtestPathVariable(@PathVariable("id")Integerid){ System.out.println("testPathVariable:"+id); returnSUCCESS; } @RequestMapping("/testAntPath/*/abc") publicStringtestAntPath(){ System.out.println("testAntPath"); returnSUCCESS; }

URL的匹配规则

importorg.springframework.boot.SpringApplication; importorg.springframework.boot.autoconfigure.SpringBootApplication; importorg.springframework.web.servlet.config.annotation.PathMatchConfigurer; importorg.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; /** *@作者Mitkey *@时间2017年1月19日下午3:31:26 *@类说明: *@版本xx */ @SpringBootApplication publicclassApplicationextendsWebMvcConfigurationSupport{ publicstaticvoidmain(String[]args){ SpringApplication.run(Application.class,args); } /** *1、extendsWebMvcConfigurationSupport *2、重写下面方法; *setUseSuffixPatternMatch:设置是否是后缀模式匹配,如“/user”是否匹配/user.*,默认真即匹配; *setUseTrailingSlashMatch:设置是否自动后缀路径模式匹配,如“/user”是否匹配“/user/”,默认真即匹配; */ @Override protectedvoidconfigurePathMatch(PathMatchConfigurerconfigurer){ super.configurePathMatch(configurer); //常用的两种 //匹配结尾/:会识别url的最后一个字符是否为/ //localhost:8080/test与localhost:8080/test/等价 configurer.setUseTrailingSlashMatch(true); //匹配后缀名:会识别xx.*后缀的内容 //localhost:8080/test与localhost:8080/test.jsp等价 configurer.setUseSuffixPatternMatch(true); //TODOPathMatchConfigurer还提供其他的一些api以供使用 } }
以上代码有两句核心的代码:

setUseSuffixPatternMatch(booleanuseSuffixPatternMatch):

设置是否是后缀模式匹配,如“/user”是否匹配/user.*,默认真即匹配;

当此参数设置为true的时候,那么/user.html,/user.aa,/user.*都能是正常访问的。

当此参数设置为false的时候,那么只能访问/user或者/user/(这个前提是setUseTrailingSlashMatch设置为true了)。

setUseTrailingSlashMatch(booleanuseSuffixPatternMatch):

设置是否自动后缀路径模式匹配,如“/user”是否匹配“/user/”,默认真即匹配;

当此参数设置为true的会后,那么地址/user,/user/都能正常访问。

当此参数设置为false的时候,那么就只能访问/user了。

当以上两个参数都设置为true的时候,那么路径/user或者/user.aa,/user.*,/user/都是能正常访问的,但是类似/user.html/是无法访问的。

当都设置为false的时候,那么就只能访问/user路径了。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: