Spring MVC指南

发布时间 2023-04-05 10:12:43作者: ashet

@RestController

It is a convenience syntax for @Controller and @ResponseBody together,This indicates that the class is a controller,and that all the methods in the marked class will return a JSON response.

@ResponseBody

The @ResponseBody is a utility annotation that tells Spring to automatically seriarlize return value(s) of this classes methods into HTTP responses.When building a JSON endpoint,this is an amazing way to "magically" convert your objects into JSON for easier consumption. if we use the @RestController annotation on our class,we don't need this annotation at all,because @RestController inherits from it.

@RequestMapping(method = RequestMethod.GET,value = "/path")

The @RequestMapping(method = RequestMethod.GET,value = "path") annotation specifies a method in the controller that should be reponsible for serving the HTTP request to the given path,or endpoint,Spring handles the mechaincal details of how this is achieved for you. You simply specify the method and path parameters on the annotation and Spring will route the requests into the correct action methods.If you don't specify a method value,it will default to GET.

@GetMapping(value = "/path")

An abbreviated form of @RequestMapping specifically for HTTP GET requests,which only takes an optional value argument,no method argument.The read in CRUD.

@PostMapping(value = "/path")

An abbreviated form of @RequestMapping specifically for HTTP POST requests,which only takes an optional value argument,no method argument.The create in CRUD.

@PutMapping(value = "/path")

An abbreviated form of @RequestMapping specifically for HTTP PUT requests,which only takes an optional value argument,no method argument.The update in CRUD.

@DeleteMapping(value = "/path")

An abbreviated form of @RequestMapping specifically for HTTP DELETE requests,which only takes an optional value argument,no method argument.The delete in CRUD.

@RequestParam(value = "/path")

Naturally,the methods handling the requests might take parameters.To help you with binding the HTTP parameters into the action method arguments,you can use the @RequestParam(value="name",defaultValue="World") annotation.Spring will parse the request parameters and put the appropriate ones into your method arguments.