SpringMVC @RestController、@RequestMapping、@RequestParam、@PathVariable 等常用注解及区别

SpringMVC 常用注解

@RestController

@RestController是一个组合注解 ,由 @Controller + @ResponseBody 指定一个控制器,并且该控制器的每一个方法都是继承了@ResponseBody注解。

@Controller

标注在控制器上面,用来处理Http请求

@Controller
public class HelloController {

    @GetMapping("/hello")
    public String handle(Model model) {
        model.addAttribute("message", "Hello World!");
        return "index";
    }
}

在这个例子里面,handle 方法接收了一个 Modle ( 视图 ),并返回了一个 String 类型的名为 index 的 Modle ( 视图 )。

@ResponseBody

将返回值通过合适的HttpMessageConverter转换并写入到响应体。

注在方法上

    @GetMapping("/accounts/{id}")
    @ResponseBody
    public Account handle() {
        // ...
    }

推荐使用 @RestController

@RestController
@RequestMapping("/api")
public class HelloController {

    @GetMapping("/hello")
    public String handle(Model model) {
        // ...
    }
}

@RequestBody

一般是Post请求的时候才会使用这个请求,把参数丢在requestBody里面

@RequestMapping

@RequestMapping将请求映射到控制器方法

@RequestMapping(
    path = "/coffee/{coffeeId}",  // 访问路径
    name = " ",  
    method = RequestMethod.GET,  // requestMethod
    params = "name",  // 参数
    headers = {"content-type=text/plain", "content-type=text/html"},
    consumes = MediaType.APPLICATION_JSON_UTF8_VALUE, // "application/json;charset=UTF-8"
    produces = MediaType.APPLICATION_JSON_UTF8_VALUE)  //"application/json;charset=UTF-8"
class PersonController {

    @GetMapping("/")
    public Person getPerson(Long id) {
        // ...
    }
}

consumes / produces用来限定请求 / 响应的格式

结合 requestMethod (Get / Post / Put / Delete)衍生了以下几个注解

@GetMapping / @PostMapping / @PutMapping / @DeleteMapping / @PatchMapping

@RestController
@RequestMapping("/persons")
class PersonController {

    @GetMapping("/{id}")
    @ResponseStatus(HttpStatus.CREATED) // 201
    public Person getPerson(@PathVariable Long id) {
        // ...
    }
}

例子中的 @ResponseStatus用于指定响应码

@RequestBody 与 @ResponseBody

@RequestBody注解表示将 HTTP请求正文插入方法中,使用合适的HttpMessageConverter将请求体写入某个对象

    @GetMapping("/")
    public void search(@RequestBody long coffeeId) {
            System.out.println(coffeeId);
    }

@Responsebody注解表示该方法的返回的结果直接写入HTTP响应正文

    @GetMapping("/")
    @ResponseBody
    public void search(long coffeeId) {
            System.out.println(coffeeId);
    }

@RequestParam 与 @PathVariable

@RequestParam

@RequestParam是从 request 中取值

http://localhost:8080/hello?name=zhangsan&age=20

public String getDetails(
    	@RequestParam(value="name", required=true) String name,
        @RequestParam(value="age", required=false) int age){
	// do something ...
}

required default true,必传

@PathVariable

@PathVariable 获取请求路径中的参数

http://localhost:8080/myCoffee/getById/19

    @GetMapping(path = "/getById/{id}")
    @ResponseBody // 将返回值绑定在响应体中返回
    public Integer getAll(@PathVariable("id") long id) {
        System.out.println(id);
        return id;
    }

URI 变量会自动转换为适当的类型或者抛出TypeMismatchException异常(类型不匹配)

@PathParam

@PathParam也是用于从绑定URL中占位符的参数,只不过其是 JBoss 下的实现,与 Spring 的@PathVariable是一样的

@QueryParam

@QueryParam是 JAX-RS 本来就提供的,和 Spring 的 @RequestParam作用一致

原文地址:https://www.cnblogs.com/hellojava404/p/13736378.html