SpringMVC常用注解
1. 基础注解
1.1 @Controller
1.1.1 注解分析
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Controller {
@AliasFor(annotation = Component.class)
String value() default "";
}
1.1.2 示例
@Controller
public class HelloController {
}
1.2 @RequestMapping
1.2.1 注解分析
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Mapping
public @interface RequestMapping {
String name() default "";
@AliasFor("path")
String[] value() default {};
@AliasFor("value")
String[] path() default {};
RequestMethod[] method() default {};
String[] params() default {};
String[] headers() default {};
String[] consumes() default {};
String[] produces() default {};
}
1.2.2 示例
@Controller
@RequestMapping("springmvc")
public class RequestMappingController {
@RequestMapping(value = "hello",method = RequestMethod.GET,name = "SpringMVC的第一个请求")
public String useRequestMapping(){
return "success";
}
1.2.3 衍生注解
@GetMapping @PostMapping @PutMapping @DeleteMapping
1.3 @RequestParam
1.3.1 注解分析
@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface RequestParam {
@AliasFor("name")
String value() default "";
@AliasFor("value")
String name() default "";
boolean required() default true;
String defaultValue() default ValueConstants.DEFAULT_NONE;
}
1.3.2 示例
@Controller
public class RequestParamController {
@RequestMapping("useRequestParam")
public String useRequestParam(@RequestParam(value = "username",required =false) String name, Integer age) {
System.out.println("控制器方法执行了" + name + "," + age);
return "success";
}
}
1.4 @InitBinder
1.4.1 注解分析
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface InitBinder {
String[] value() default {};
}
1.4.2 示例
@Controller
public class InitBinderController {
@RequestMapping("/saveUser")
public String saveUser(User user){
System.out.println("User is "+user);
return "success";
}
@InitBinder("user")
public void dateBinder(WebDataBinder dataBinder){
dataBinder.addCustomFormatter(new DateFormatter("yyyy-MM-dd"));
}
}
1.4.3 扩展:@DateTimeFormat
public class User implements Serializable {
private Integer id;
private String username;
private String password;
private Integer age;
private String gender;
@DateTimeFormat(pattern = "yyyy-MM-dd")
private Date birthday;
}
1.5 @ControllerAdvice
1.5.1 注解分析
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface ControllerAdvice {
@AliasFor("basePackages")
String[] value() default {};
@AliasFor("value")
String[] basePackages() default {};
Class<?>[] basePackageClasses() default {};
Class<?>[] assignableTypes() default {};
Class<? extends Annotation>[] annotations() default {};
}
1.5.2 示例
@ControllerAdvice(basePackageClasses=InitBinderController.class)
public class InitBinderAdvice {
@InitBinder("user")
public void dateBinder(WebDataBinder dataBinder){
dataBinder.addCustomFormatter(new DateFormatter("yyyy-MM-dd"));
}
}
1.6 @RequestHeader
1.6.1 注解分析
@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface RequestHeader {
@AliasFor("name")
String value() default "";
@AliasFor("value")
String name() default "";
boolean required() default true;
String defaultValue() default ValueConstants.DEFAULT_NONE;
}
1.6.2 示例
@Controller
public class RequestHeaderController {
@RequestMapping("/useRequestHeader")
public String useRequestHeader(@RequestHeader(value="Accept-Language",required=false)String requestHeader){
System.out.println(requestHeader);
return "success";
}
}
1.7 @CookieValue
1.7.1 注解分析
@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface CookieValue {
@AliasFor("name")
String value() default "";
@AliasFor("value")
String name() default "";
boolean required() default true;
String defaultValue() default ValueConstants.DEFAULT_NONE;
}
1.7.2 示例
@Controller
public class CookieValueController {
@RequestMapping("/useCookieValue")
public String useCookieValue(@CookieValue(value="JSESSIONID",required=false) String cookieValue){
System.out.println(cookieValue);
return "success";
}
}
1.8 @ModelAttribute
1.8.1 注解分析
@Target({ElementType.PARAMETER, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface ModelAttribute {
@AliasFor("name")
String value() default "";
@AliasFor("value")
String name() default "";
boolean binding() default true;
}
1.8.2 示例
@Controller
public class ModelAttributeController {
@ModelAttribute
public void showModel(String username) {
System.out.println("执行了showModel方法"+username);
}
@RequestMapping("/testModelAttribute")
public String testModelAttribute(String username) {
System.out.println("执行了控制器的方法"+username);
return "success";
}
}
1.9 @SessionAttribute与@SessionAttributes
1.9.1 注解分析
@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface SessionAttribute {
@AliasFor("name")
String value() default "";
@AliasFor("value")
String name() default "";
boolean required() default true;
}
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface SessionAttributes {
@AliasFor("names")
String[] value() default {};
@AliasFor("value")
String[] names() default {};
Class<?>[] types() default {};
}
1.9.2 示例
@Controller
@SessionAttributes(value ={"username","password"},types={Integer.class})
public class SessionAttributesController {
@RequestMapping("/testPut")
public String testPut(Model model){
model.addAttribute("username", "泰斯特");
model.addAttribute("password","123456");
model.addAttribute("age", 31);
return "success";
}
@RequestMapping("/testGet")
public String testGet(ModelMap model){
System.out.println(model.get("username")+";"+model.get("password")+";"+model.get("age"));
return "success";
}
@RequestMapping("/testClean")
public String complete(SessionStatus sessionStatus){
sessionStatus.setComplete();
return "success";
}
@RequestMapping("/testSessionAttribute")
public String testSessionAttribute(@SessionAttribute("username")String
username){
System.out.println("username is "+username);
return "success";
}
}
1.10 @ExceptionHandler
1.10.1 注解分析
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface ExceptionHandler {
Class<? extends Throwable>[] value() default {};
}
2. JSON数据交互相关注解
2.1 @RequestBody
2.1.1 注解分析
@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface RequestBody {
boolean required() default true;
}
2.1.2 示例
@Controller
public class RequestBodyController {
@RequestMapping("/useRequestBody")
public String useRequestBody(@RequestBody(required=false) String body){
System.out.println(body);
return "success";
}
}
2.2 @ResponseBody
2.2.1 注解分析
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface ResponseBody {
}
2.2.2 示例
@Controller
public class ResponseBodyController {
@RequestMapping("useResponseBody")
@ResponseBody
public String useResponseBody(String name){
return "success";
}
}
2.3 @RestController
2.3.1 注解分析
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Controller
@ResponseBody
public @interface RestController {
@AliasFor(annotation = Controller.class)
String value() default "";
}
2.4 @RestControllerAdvice
2.4.1 注解分析
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@ControllerAdvice
@ResponseBody
public @interface RestControllerAdvice {
@AliasFor("basePackages")
String[] value() default {};
@AliasFor("value")
String[] basePackages() default {};
Class<?>[] basePackageClasses() default {};
Class<?>[] assignableTypes() default {};
Class<? extends Annotation>[] annotations() default {};
}
3. Rest风格URL请求相关注解
3.1 @PathVariable
3.1.1 说明
@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface PathVariable {
@AliasFor("name")
String value() default "";
@AliasFor("value")
String name() default "";
boolean required() default true;
}
3.1.2 示例
@Controller
public class PathVariableController {
@RequestMapping("/usePathVariable/{id}")
public String usePathVariable(@PathVariable("id") Integer id){
System.out.println(id);
return "success";
}
}
4.跨域访问
4.1 @CrossOrigin
4.1.1 作用
用于指定是否支持跨域访问