欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

SpringMVC之@RequestMapping用法 博客分类: SpringMVC SpringMVC@RequestMapping用法 

程序员文章站 2024-03-16 20:20:22
...
SpringMVC之@RequestMapping用法
            
    
    博客分类: SpringMVC SpringMVC@RequestMapping用法 

一、基本介绍
@RequestMapping 注解可以用在类上,也可以用在方法上。



@RestController
@RequestMapping("/home")
public class IndexController {
  @RequestMapping("/")
  String get(){
    return "Hello from get";
  }
  @RequestMapping("/index")
  String index(){
    return "Hello from index";
  }
}


/*

/home       请求,会调用 get()   方法,
/home/index 请求,会调用 index() 方法。

*/






二、一个 @RequestMapping ,匹配多个路径
@RestController
@RequestMapping("/home")
public class IndexController {

@RequestMapping(value={"", "/page", "page*","view/*,**/msg"})
  String indexMultipleMapping(){
    return "Hello from index multiple mapping.";
  }
}


/*

下面的路径都将匹配到 indexMultipleMapping() 方法进行处理:

  - localhost:8080/home
  - localhost:8080/home/
  - localhost:8080/home/page
  - localhost:8080/home/pageabc
  - localhost:8080/home/view/
  - localhost:8080/home/view/view


*/




三、@RequestMapping 中使用 @RequestParam 绑定参数
@RestController	
@RequestMapping("/home")
public class IndexController {
  


/*

匹配:localhost:8090/home/id?_id=5


*/
  @RequestMapping(value = "/id")								
  String getIdByValue(@RequestParam("_id") String personId){
    System.out.println("ID is "+personId);
    return "Get ID from query string of URL with value element";
  }


/*

匹配:localhost:8090/home/personId?personId=5

注意:如果方法参数的 personId 名称 与 请求路径中参数的名称一致,
     则可以省略 @RequestParam 后面小括号的内容
*/

  @RequestMapping(value = "/personId")							
  String getId(@RequestParam String personId){
    System.out.println("ID is "+personId);	
    return "Get ID from query string of URL without value element";	
  }
}


/*

@RequestParam 的 required 属性用法示例

*/
@RestController
@RequestMapping("/home")
public class IndexController {
  @RequestMapping(value = "/name")
  String getName(@RequestParam(value = "person", required = false) String personName){
    return "Required element of request param";
  }
}


/*

@RequestParam 的 defaultValue 属性用法示例

*/
@RestController	
@RequestMapping("/home")
public class IndexController {
  @RequestMapping(value = "/name")
  String getName(@RequestParam(value = "person", defaultValue = "John") String personName ){
    return "Required element of request param";
  }
}




四、@RequestMapping 结合 HTTP 请求方法使用

HTTP 请求方法包括:  GET, PUT, POST, DELETE, and PATCH.

@RestController
@RequestMapping("/home")
public class IndexController {
  @RequestMapping(method = RequestMethod.GET)
  String get(){
    return "Hello from get";									
  }
  @RequestMapping(method = RequestMethod.DELETE)
  String delete(){
    return "Hello from delete";
  }
  @RequestMapping(method = RequestMethod.POST)
  String post(){
    return "Hello from post";
  }
  @RequestMapping(method = RequestMethod.PUT)
  String put(){
    return "Hello from put";
  }
  @RequestMapping(method = RequestMethod.PATCH)
  String patch(){
    return "Hello from patch";
  }
}

/*
请求路径都是 /home,但是根据 http 发送的请求方法的不同,
匹配不同的处理方法。

*/




/*

@RequestMapping 根据请求方法,的简写

*/

@RequestMapping("/home")
public class IndexController {
  @GetMapping("/person")
  public @ResponseBody ResponseEntity<String> getPerson() {
    return new ResponseEntity<String>("Response from GET", HttpStatus.OK);
  }
  @GetMapping("/person/{id}")
  public @ResponseBody ResponseEntity<String> getPersonById(@PathVariable String id){
    return new ResponseEntity<String>("Response from GET with id " +id,HttpStatus.OK);				  }
  @PostMapping("/person")
  public @ResponseBody ResponseEntity<String> postPerson() {
    return new ResponseEntity<String>("Response from POST method", HttpStatus.OK);
  }
  @PutMapping("/person")
  public @ResponseBody ResponseEntity<String> putPerson() {
    return new ResponseEntity<String>("Response from PUT method", HttpStatus.OK);
  }
  @DeleteMapping("/person")
  public @ResponseBody ResponseEntity<String> deletePerson() {
    return new ResponseEntity<String>("Response from DELETE method", HttpStatus.OK); 
  }
  @PatchMapping("/person")
  public @ResponseBody ResponseEntity<String> patchPerson() {
    return new ResponseEntity<String>("Response from PATCH method", HttpStatus.OK);	
  }
}



五、@RequestMapping 缩小匹配范围:限制输入和输出格式


@RestController
@RequestMapping("/home")
public class IndexController {

/*

只匹配消费类型是 application/JSON 的请求
*/
  @RequestMapping(value = "/prod", produces = {"application/JSON"})
  @ResponseBody
  String getProduces(){
    return "Produces attribute";
  }


/*

只匹配输入类型是 application/JSON 或 application/XML 的请求
*/
  @RequestMapping(value = "/cons", consumes = {"application/JSON", "application/XML"})
  String getConsumes(){
    return "Consumes attribute";
  }
}







六、@RequestMapping 缩小匹配范围:限制 Headers 请求头

/*

只匹配 header 中含有:content-type=text/plain 的请求
*/
@RestController	
@RequestMapping("/home")
public class IndexController {
  @RequestMapping(value = "/head", headers = {"content-type=text/plain"})
  String post(){	
    return "Mapping applied along with headers";	
  }								
}


/*

只匹配 header 中含有:content-type=text/plain 或 content-type=text/html 的请求
*/
@RestController
@RequestMapping("/home")
public class IndexController {
  @RequestMapping(value = "/head", headers = {"content-type=text/plain", "content-type=text/html"})		  String post(){
    return "Mapping applied along with headers";
  }								
}






七、@RequestMapping 缩小匹配范围:限制请求参数

@RestController
@RequestMapping("/home")
public class IndexController {
  @RequestMapping(value = "/fetch", params = {"personId=10"})
  String getParams(@RequestParam("personId") String id){
    return "Fetched parameter using params attribute = "+id;
  }
  @RequestMapping(value = "/fetch", params = {"personId=20"})
    String getParamsDifferent(@RequestParam("personId") String id){
    return "Fetched parameter using params attribute = "+id;
  }
}






八、@RequestMapping 动态匹配 URL
@RestController
@RequestMapping("/home")
public class IndexController {
  @RequestMapping(value = "/fetch/{id}", method = RequestMethod.GET)
  String getDynamicUriValue(@PathVariable String id){
    System.out.println("ID is "+id);
    return "Dynamic URI parameter fetched";						
  } 
  @RequestMapping(value = "/fetch/{id:[a-z]+}/{name}", method = RequestMethod.GET)
    String getDynamicUriValueRegex(@PathVariable("name") String name){
    System.out.println("Name is "+name);
    return "Dynamic URI parameter fetched using regex";		
  } 
}




九、@RequestMapping 设定默认匹配的方法
@RestController
@RequestMapping("/home")
public class IndexController {

  /*
这个 @RequestMapping() 什么值也没设定,作为默认处理 /home 请求的方法。
*/
  @RequestMapping()
  String default(){									
    return "This is a default method for the class";
  }
}






转载请注明,
原文出处: http://lixh1986.iteye.com/blog/2429836













-

引用:
https://springframework.guru/spring-requestmapping-annotation/






  • SpringMVC之@RequestMapping用法
            
    
    博客分类: SpringMVC SpringMVC@RequestMapping用法 
  • 大小: 32.2 KB