RESTful架构与SpringMVC框架的结合使用以及PUT、DELETE
2017-10-25 11:38
483 查看
步骤:
在web.xml文件配置过滤器HiddenHttpMethodFilter
在controller中设置与调用
详细讲解:
配置过滤器HiddenHttpMethodFilter
关注源码中的doFilterInternal
而这个methodParam就是通过前端参数传过来的值来判断PUT、DELETE的
以下是前端页面表格中_method的值,对应的是DELETE
<form action="springmvc/testRest/1" method="post">
<input type="hidden" name="_method" value="DELETE">
<input type="submit" value="restDelete">
</form>
在SpringMVC中调用
在web.xml文件配置过滤器HiddenHttpMethodFilter
在controller中设置与调用
详细讲解:
配置过滤器HiddenHttpMethodFilter
<filter> <filter-name>methodFilter</filter-name> <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class> </filter> <filter-mapping> <filter-name>methodFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
关注源码中的doFilterInternal
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { HttpServletRequest requestToUse = request; if ("POST".equals(request.getMethod()) && request.getAttribute(WebUtils.ERROR_EXCEPTION_ATTRIBUTE) == null) { String paramValue = request.getParameter(this.methodParam); if (StringUtils.hasLength(paramValue)) { requestToUse = new HttpMethodRequestWrapper(request, paramValue); } } filterChain.doFilter(requestToUse, response); }里面高亮的代码就是获取请求方式,默认是get
而这个methodParam就是通过前端参数传过来的值来判断PUT、DELETE的
以下是前端页面表格中_method的值,对应的是DELETE
<form action="springmvc/testRest/1" method="post">
<input type="hidden" name="_method" value="DELETE">
<input type="submit" value="restDelete">
</form>
在SpringMVC中调用
@Controller @RequestMapping("/springmvc") public class Hello { private static final String SUCCESS = "success"; /** * 最终的视图: * prefix+returnValue+suffix * /WEB-INF/views/success.jsp */ /** * https://www.douyu.com/485503 restful风格 * {roomId}占位符 * 两种命名情况 * 1.参数的名字和占位符的名字保持一致 * 2.@PathVariable中的名字和占位符中的名字一致,参数的名字就任意 * @return */ /** * /testRest POST 新增 * /testRest/1 GET 获取 * /testRest/1 PUT 更新 * /testRest/1 DELETE 删除 * * @param id * @return */ @RequestMapping("/testRest/{id}") public String testRestGet(@PathVariable Integer id){ System.out.println("testRestGet:"+id); return SUCCESS; } @RequestMapping(value = "/testRest",method = RequestMethod.POST) public String testRestPost(){ System.out.println("testRestPost:"); return SUCCESS; } @RequestMapping(value = "/testRest/{id}",method = RequestMethod.PUT) public String testRestPut(@PathVariable Integer id){ System.out.println("testRestPut:"+id); return SUCCESS; } @RequestMapping(value = "/testRest/{id}",method = RequestMethod.DELETE) public String testRestDelete(@ 9e3b PathVariable Integer id){ System.out.println("testRestDelete:"+id); return SUCCESS; } }
相关文章推荐
- springMVC系列(七)——springMVC实现restful风格开发(post、get、put、delete)
- 针对RestFul风格的PUT,DELETE请求在SpringMVC的Controller层无法接收参数的解决方案
- 使用Intellij Idea 和SpringMVC开发具有RESTful架构风格的API接口
- JAVAWEB开发之SpringMVC详解(一)——SpringMVC的框架原理、架构简介、与mybatis整合和注解方式的使用、
- 让python bottle框架支持jquery ajax的RESTful风格的PUT和DELETE等请求
- springMVC系列(七)——springMVC实现restful风格开发(post、get、put、delete)
- iOS各框架使用场景以及系统架构层次
- [置顶] springmvc 框架下使用restful接口参数数据传输
- 关于SpringMVC与JDBC结合实现对数据库增删改查(适合初学者理解JDBC使用,但是对于SpringMVC框架使用了扫描,不太适合初学者)
- 【SpringMVC】用restful风格接口写DELETE或PUT请求时遇到405错误
- 权限设计,以及使用优秀的权限框架shiro整合SpringMVC
- 使用MVP框架,retrofit结合Rxjava以及fresco加载图片,用recycleview实现
- Springmvc 使用Restful架构(五)
- springMVC使用PUT、DELETE方法传递参数解决方案
- 使用原生JavaScriptAjax以及jQuery的Ajax结合SpringMVC发送和获取json数据
- Yii2框架之使用Restful自定义Api以及用户的授权认证
- SpringMVC 使用 RESTful 架构实现 CRUD 操作
- JAVAWEB开发之SpringMVC详解(一)——SpringMVC的框架原理、架构简介、与mybatis整合和注解方式的使用、
- 让python bottle框架支持jquery ajax的RESTful风格的PUT和DELETE等请求(新方法)
- springMvc实现restful解决put和delete表单提交问题