method属性可以控制请求的方式,值为RequestMethod的枚举值
@RequestMapping( value = "/***" ,method = RequestMethod.GET)
param:表示请求中必须包含名为param的参数
!param:表示请求中不能包含名为param的参数
param = value 表示请求中包含名为param的参数,但是值必须是value
param != value 表示请求中包含名为param的参数,但是值不能是value
{"param1","param2=value"},可以将对于多个参数的要求写入数组
@RequestMapping( value = "/***" ,params = {"username!=root","password"})
@RequestMapping( value = "/***",headers = {"Accept-Encoding=gzip, deflate"})
普通形式的url
*****/contextPath/aaa.do
*****/contextPath/aaa.jsp
*****/contextPath/aaa.html
*****/contextPath/css/aaa.css
*****/contextPath/js/aaa.js
*****/contextPath/aaa.do?id=10&username=root
restFul风格的url
*****/contextPath/aaa/10/root
*****/contextPath/aaa
controller 处理单元
@Controller
public class PathController {
@RequestMapping("/testPathVariable/{id}/{username}")
public String testPathVariable(@PathVariable("id") Integer id, @PathVariable("username") String username){
System.out.println("id:"+id);
System.out.println("username:"+username);
System.out.println("testPathVariable1");
return "success";
}
}
请求测试
Http协议中,四个表示操作方式的动词"GET POST PUT DELETE",他们对应四种基本操作,GET用来获取资源,POST用来新建资源,PUT用来更新资源,DELETE用来删除资源
简单的说,就是我们在访问资源时,可以通过这四个状态来表示对于资源的不同操作,这四个状态表现为我们请求的四种方式
/controller/1 HTTP GET :得到id为1 的资源
/controller/1 HTTP DELETE :删除id为1的资源
/controller/1 HTTP PUT :更新id为1 的资源
/controller/1 HTTP POST :增加id为1 的资源
在访问同一个url的时候,通过不同的请求方式,对应到不同的controller处理单元
<!--配置hiddenHttpMethodFilter ,将POST请求转换为PUT或者DELETE请求-->
<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>
转换原理
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
HttpServletRequest requestToUse = request;
if ("POST".equals(request.getMethod()) && request.getAttribute("javax.servlet.error.exception") == null) {
String paramValue = request.getParameter(this.methodParam);// "_method"
if (StringUtils.hasLength(paramValue)) {
String method = paramValue.toUpperCase(Locale.ENGLISH);
if (ALLOWED_METHODS.contains(method)) {
requestToUse = new HiddenHttpMethodFilter.HttpMethodRequestWrapper(request, method);
}
}
}
filterChain.doFilter((ServletRequest)requestToUse, response);
}
package com.lanson.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
/**
* @Author: Lansonli
* @Description: MircoMessage:Mark_7001
*/
@RequestMapping(value = "/myController")
@RestController
public class MyController {
@RequestMapping(value = "/testRest/{id}",method = RequestMethod.PUT)
public String testPut(@PathVariable(value = "id") Integer id){
System.out.println("testPut, id:"+id);
return "show";
}
@RequestMapping(value = "/testRest/{id}",method = RequestMethod.DELETE)
public String testDelete(@PathVariable(value = "id") Integer id){
System.out.println("testDelete, id:"+id);
return "show";
}
@RequestMapping(value = "/testRest/{id}",method = RequestMethod.POST)
public String testPOST(@PathVariable(value = "id") Integer id){
System.out.println("testPOST, id:"+id);
return "show";
}
@RequestMapping(value = "/testRest/{id}",method = RequestMethod.GET)
public String testGET(@PathVariable(value = "id") Integer id){
System.out.println("testGET, id:"+id);
return "show";
}
}
<form action="myController/testRest/10" method="POST">
<input type="hidden" name="_method" value="PUT">
<input type="submit" value="testPUT">
</form>
<br/>
<form action="myController/testRest/10" method="POST">
<input type="hidden" name="_method" value="DELETE">
<input type="submit" value="testDELETE">
</form>
<br/>
<form action="myController/testRest/10" method="POST">
<input type="submit" value="testPOST">
</form>
<br/>
<form action="myController/testRest/10" method="GET">
<input type="submit" value="testGET">
</form>
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。