Back to Javatutorial

SpringMVC常见注解

docs/Spring全家桶/SpringMVC/SpringMVC常见注解.md

1.0.03.9 KB
Original Source

1.

ڱ̳Уǽ̽ org.springframework.web.bind.annotation е Spring Web ע͡

2. @RequestMapping

򵥵˵@RequestMapping @Controller ڲ򷽷 ʹã

·ƺֵӳ䵽ĸ URL ݵ HTTP params HTTP Ĵڡڻֵ headers HTTP ͷĴڡڻֵ ģ÷ HTTP Щý produces÷ HTTP ӦЩý һ򵥵ʾ

@Controller
class VehicleController {

    @RequestMapping(value = "/vehicles/home", method = RequestMethod.GET)
    String home() {
        return "home";
    }
}

༶ӦôעͣǿΪ @Controller ед򷽷ṩĬá Ψһ Spring ʹ÷øǵḽ·ֵ URL

磬úЧһģ

@Controller
@RequestMapping(value = "/vehicles", method = RequestMethod.GET)
class VehicleController {

    @RequestMapping("/home")
    String home() {
        return "home";
    }
}

⣬@GetMapping@PostMapping@PutMapping@DeleteMapping @PatchMapping @RequestMapping IJͬ壬HTTP ѷֱΪGETPOSTPUTDELETE PATCH

Щ Spring 4.3 汾ʼá

3 @RequestBody

Ǽ@RequestBody HTTP ӳ䵽һ

@PostMapping("/save")
void saveVehicle(@RequestBody Vehicle vehicle) {
// ...
}

лԶģȡ͡

4 @PathVariable

˵˵@PathVariable

עָʾ󶨵 URI ģ ǿʹ @RequestMapping עָ URI ģ壬ʹ @PathVariable 󶨵ģ岿֮һ

ǿʹƻֵʵһ㣺

@RequestMapping("/{id}")
Vehicle getVehicle(@PathVariable("id") long id) {
// ...
}

ģвֵ뷽ƥ䣬ǾͲעָ

@RequestMapping("/{id}")
Vehicle getVehicle(@PathVariable long id) {
// ...
}

⣬ǿͨIJΪ false ·Ϊѡ

@RequestMapping("/{id}")
Vehicle getVehicle(@PathVariable(required = false) long id) {
// ...
}

5. @RequestParam

We use @RequestParam for accessing HTTP request parameters:

@RequestMapping
Vehicle getVehicleByParam(@RequestParam("id") long id) {
// ...
}

@PathVariable עͬѡ

Щ֮⣬ Spring зûֵΪֵʱǿʹ @RequestParam ָעֵ ΪˣDZ defaultValue

ṩĬֵʽ required Ϊ false

@RequestMapping("/buy")
Car buyCar(@RequestParam(defaultValue = "5") int seatCount) {
// ...
}

˲֮⣬ǻԷ HTTP 󲿷֣cookie ͱͷ

ǿԷֱʹע@CookieValue @RequestHeader ǡ

6. Response Handling Annotations

ڽIJУǽ Spring MVC в HTTP Ӧע͡

6.1 @ResponseBody

@ResponseBody 򷽷Spring ὫĽΪӦ

@ResponseBody
@RequestMapping("/hello")
String hello() {
return "Hello World!";
}

עע @Controller ࣬򷽷ʹ

6.2 @ExceptionHandler

ʹôעͣǿһԶ򷽷 򷽷׳κָ쳣ʱSpring ô˷

쳣Ϊݸ

@ExceptionHandler(IllegalArgumentException.class)
void onIllegalArgumentException(IllegalArgumentException exception) {
// ...
}

6.3 @ResponseStatus

ʹôעͶ򷽷עָͣӦ HTTP ״̬ ǿʹ code value ״̬롣

⣬ǿʹ reason ṩԭ

ҲԽ@ExceptionHandler һʹã

@ExceptionHandler(IllegalArgumentException.class) @ResponseStatus(HttpStatus.BAD_REQUEST) void onIllegalArgumentException(IllegalArgumentException exception) { // ... }

й HTTP Ӧ״̬ĸϢʱġ

7 Webע

һЩעͲֱӹ HTTP Ӧ ڽIJУǽġ

7.1 @Controller

ǿʹ@Controller һSpring MVC йظϢǹ Spring Bean Annotations ¡

7.2 @RestController

@RestController @Controller @ResponseBody

ˣǵЧģ

@Controller
@ResponseBody
class VehicleRestController {
// ...
}
@RestController
class VehicleRestController {
// ...
}

7.3 @ModelAttribute

ͨע⣬ǿͨṩģͼѾ MVC @Controller ģеԪأ

@PostMapping("/assemble")
void assembleVehicle(@ModelAttribute("vehicle") Vehicle vehicleInModel) {
// ...
}

@PathVariable @RequestParam һͬƣDzָģͼ

@PostMapping("/assemble")
void assembleVehicle(@ModelAttribute Vehicle vehicle) {
// ...
}

⣬@ModelAttributeһ;עһSpringԶķֵӵģУ

@ModelAttribute("vehicle")
Vehicle getVehicle() {
// ...
}

ǰһDzָģͼSpring Ĭʹ÷ƣ

@ModelAttribute
Vehicle vehicle() {
// ...
}

Spring 򷽷֮ǰ @ModelAttribute ע͵ķ

й @ModelAttribute ĸϢıġ

7.4 @CrossOrigin

@CrossOrigin Ϊע͵򷽷ÿͨţ

@CrossOrigin
@RequestMapping("/hello")
String hello() {
return "Hello World!";
}

һ࣬е򷽷

ǿʹôע͵IJ΢ CORS Ϊ

йϸϢʱġ

ο

https://www.baeldung.com/spring-annotations