springboot-web开发(rest风格支持)
生活随笔
收集整理的這篇文章主要介紹了
springboot-web开发(rest风格支持)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
?rest風(fēng)格支持(使用http請求方式動詞來標(biāo)識對資源的操作)
<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"><!-- 自定義參數(shù)名稱 --><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風(fēng)格的methodParamhiddenHttpMethodFilter.setMethodParam("_m");return hiddenHttpMethodFilter;} }核心Filter:HiddenHttpMethodFilter? 會對 _method的參數(shù)進(jìn)行解析。
?我們要想使用系統(tǒng)提供的這個filter,那么這個屬性就要我們手工開啟。默認(rèn)是false,不開啟。
總結(jié)
以上是生活随笔為你收集整理的springboot-web开发(rest风格支持)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: springboot-web开发(静态资
- 下一篇: springboot-web开发(请求映