当前位置:
首页 >
springboot-web开发(rest风格支持)
发布时间:2025/6/15
43
豆豆
生活随笔
收集整理的这篇文章主要介绍了
springboot-web开发(rest风格支持)
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
rest风格支持(使用http请求方式动词来标识对资源的操作)
<html><head><meta charset="UTF-8"> </head><body><form action="/user" method="get"><input value="rest-get 提交" type="submit"></form><form action="/user" method="post"><input value="rest-post 提交" type="submit"></form><form action="/user" method="post"><input name="_method" type="hidden" value="DELETE"><input value="rest-delete 提交" type="submit"></form><form action="/user" method="post"><input name="_method" type="hidden" value="PUT"><input value="rest-put_method 提交" type="submit"></form><form action="/user" method="post"><!-- 自定义参数名称 --><input name="_m" type="hidden" value="PUT"><input value="rest-put_m 提交" type="submit"></form> </body></html> package com.atchina.boot.controller;import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController;@RestController public class HelloController {@RequestMapping(value = "/user",method = RequestMethod.GET)public String getUser(){return "GET user";}@RequestMapping(value = "/user",method = RequestMethod.POST)public String saveUser(){return "POST user";}@RequestMapping(value = "/user",method = RequestMethod.PUT)public String putUser(){return "PUT user";}@RequestMapping(value = "/user",method = RequestMethod.DELETE)public String deleteUser(){return "DELETE user";} } import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.filter.HiddenHttpMethodFilter;@Configuration public class WebConfig {@Beanpublic HiddenHttpMethodFilter hiddenHttpMethodFilter(){HiddenHttpMethodFilter hiddenHttpMethodFilter = new HiddenHttpMethodFilter();// 可以自定义restful风格的methodParamhiddenHttpMethodFilter.setMethodParam("_m");return hiddenHttpMethodFilter;} }核心Filter:HiddenHttpMethodFilter 会对 _method的参数进行解析。
我们要想使用系统提供的这个filter,那么这个属性就要我们手工开启。默认是false,不开启。
总结
以上是生活随笔为你收集整理的springboot-web开发(rest风格支持)的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: springboot-web开发(静态资
- 下一篇: springboot-web开发(请求映