1.常用注解
@RequestMapping
@RequestMapping(
value = {"2", "3"},
method = {RequestMethod.GET, RequestMethod.POST},
params = {"name=admin"},
headers = {"Host=localhost:8080"}
)
@RequestHeader
@RequestHeader(name = "Host", required = false, defaultValue = "8081") String b
@CookieValue
@CookieValue(name = "JSESSIONID", required = false, defaultValue = "123456") String a
@PathVariable
@GetMapping("7/{id}/{username}")
public String test4(@PathVariable("id") long id,
@PathVariable String username) {
return "success";
}
@ModelAttribute
@ModelAttribute("test1")
public String getModel() {
return "test1的信息";
}
@GetMapping("8")
public String test5(@ModelAttribute("test1") String a) {
return a;
}
@RequestBody
@RequestBody(required = false) TestEntity testEntity
@SessionAttributes
@SessionAttributes是类注解,他用来在session中存储model
@SessionAttribute只是获取存储在session中的属性
@SessionAttributes(value = {"test1"})
@ResponseBody
将controller的方法返回的对象通过适当的转换器转换为指定的格式之后,写入到response对象的body区,通常用来返回JSON数据或者是XML数据
@ControllerAdvice
@ControllerAdvice配合 @ExceptionHandler 实现全局异常处理
@ControllerAdvice 配合 @ModelAttribute 预设全局数据
@ControllerAdvice 配合 @InitBinder 实现对请求参数的预处理
@ExceptionHandler(Exception.class)
@RequestParam
@RequestParam(name = "name", required = false, defaultValue = "admin") String a
2.参数解析
接口
//参数解析器要实现的接口
public interface HandlerMethodArgumentResolver {
//解析器是否支持给定参数
boolean supportsParameter(MethodParameter parameter);
//解析参数
@Nullable
Object resolveArgument(MethodParameter parameter, @Nullable ModelAndViewContainer mavContainer,
NativeWebRequest webRequest, @Nullable WebDataBinderFactory binderFactory) throws Exception;
}
HandlerMethodArgumentResolver 的实现类可以分为:
- xxxResolver:这就是一个普通的参数解析器。
- xxxProcessor:不仅可以当作参数解析器,还可以处理对应类型的返回值。
具体解析器
处理类型 | 对应解析器 |
---|---|
使用了 @PathVariable 注解并且参数类型不为 Map 的参数 | PathVariableMethodArgumentResolver |
处理 Map/ModelMap 类型的参数 | MapMethodProcessor |
使用了 @PathVariable 注解参数类型为 Map | PathVariableMapMethodArgumentResolver |
处理使用了 @RequestHeader 注解,并且参数类型不是 Map 的参数 | RequestHeaderMethodArgumentResolver |
处理使用了 @RequestHeader 注解,并且参数类型是 Map 的参数 | RequestHeaderMapMethodArgumentResolver |
处理使用了 @RequestAttribute 注解的参数 | RequestAttributeMethodArgumentResolver |
使用了 @RequestParam 注解的参数、文件上传的类型 MultipartFile、或者一些没有使用任何注解的基本类型(Long、Integer)以及 String 等 | RequestParamMethodArgumentResolver |
@RequestParam 注解的参数类型是 Map,且注解有 name 值 | RequestParamMapMethodArgumentResolver |
处理使用了 @CookieValue 注解的参数 | AbstractCookieValueMethodArgumentResolver |
处理使用了 @SessionAttribute 注解的参数 | SessionAttributeMethodArgumentResolver |
处理使用了 @ModelAttribute 注解的参数 | ModelAttributeMethodProcessor |
这个用来处理添加了 @RequestBody 注解的参数 | RequestResponseBodyMethodProcessor |
处理使用了 @Value 注解的参数 | ExpressionValueMethodArgumentResolver |
处理 Error 参数 | ErrorsMethodArgumentResolver |
自定义解析器
1.自定义注解
@Retention(RetentionPolicy.RUNTIME)
//注解使用在参数上
@Target(ElementType.PARAMETER)
public @interface TestAnnotation {
String value() default "";
}
@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
public @interface Test2Annotation {
//参数绑定的变量名
String name() default "test2";
//参数是否必须
boolean required() default true;
//没有获取到请求参数的时候的默认值
String defaultValue() default ValueConstants.DEFAULT_NONE;
}
2.自定义解析器,实现HandlerMethodArgumentResolver接口,或者继承AbstractNamedValueMethodArgumentResolver抽象类
//法1
public class TestResolver implements HandlerMethodArgumentResolver {
//是否启用该解析器
@Override
public boolean supportsParameter(MethodParameter parameter) {
//判断参数是否使用该注解
return parameter.hasParameterAnnotation(TestAnnotation.class);
}
//解析逻辑
@Override
public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
//获取HttpServletRequest
HttpServletRequest request = webRequest.getNativeRequest(HttpServletRequest.class);
//获取注解
TestAnnotation e = parameter.getParameterAnnotation(TestAnnotation.class);
//从请求中获取对应名称的参数
String s = request.getParameter(e != null ? e.value() : "s");
//返回处理结果
return s.toUpperCase();
}
}
public class Test2Resolver extends AbstractNamedValueMethodArgumentResolver {
/**
* 获取当前参数的注解信息
* @param parameter 需要被解析的Controller参数
*/
@Override
protected NamedValueInfo createNamedValueInfo(MethodParameter parameter) {
Test2Annotation test2Annotation = parameter.getParameterAnnotation(Test2Annotation.class);
return new NamedValueInfo(test2Annotation.name(), test2Annotation.required(), test2Annotation.defaultValue());
}
/**
* 在这里进行参数的类型转换
* @param name 要解析值的名称
* @param parameter 需要被解析的Controller参数
* @param request 当前request
* @return 转换后的参数值
*/
@Override
protected Object resolveName(String name, MethodParameter parameter, NativeWebRequest request) throws Exception {
return "resolver2 success";
}
/**
* 解析器是否支持当前参数
* @param parameter 需要被解析的Controller参数
* @return 是否使用该注解
*/
@Override
public boolean supportsParameter(MethodParameter parameter) {
return parameter.hasParameterAnnotation(Test2Annotation.class);
}
}
3.注册自定义解析器
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Override
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
//注册自定义解析器
resolvers.add(new TestResolver());
resolvers.add(new Test2Resolver());
}
}
4.使用
@GetMapping("1")
public String getParam1(@TestAnnotation(value = "s") String s,
@Test2Annotation String a) {
System.out.println("参数为:" + s);
System.out.println("参数为:" + a);
return "success";
}
法一从请求中获取参数字符串并转换为大写作为控制器参数,法二将写死的字符串作为控制器参数
3.参数校验
基础校验
@NotNull(message = "用户id不能为空")
private String userId;
//@NotEmpty不去空格,空格也能通过检验
@NotEmpty(message = "用户名称不能为空")
private String userName;
//@NotBlank会去掉字符串前后空格验证是否为空
@NotBlank(message = "用户密码不能为空")
@Length(min = 6, max = 16, message = "密码长度介于6到16位之间")
private String password;
@NotNull(message = "邮箱不能为空")
@Email(message = "请输入有效邮箱")
private String email;
@Min(value = 18, message = "不得小于18岁")
@Max(value = 60, message = "不得大于60岁")
private Integer age;
@Past(message = "生日不能为未来时间")
private Date birthday;
@Size(min = 1, message = "不能少于1个好友")
private List<@Valid UserInfo> friends;
@AssertFalse
@AssertTrue
@Null
@Digits(integer = 整数位数,fraction = 小数位数)
@Future
@Pattern(regex = 正则,flag = 标志的模式)
@URL
级联校验
private List<@Valid UserInfo> friends;
分组校验
//登录场景,验证登陆组时不会验证标注为其他场景的参数以及没有组的参数
public interface LoginGroup {
}
//注册场景
public interface RegisterGroup {
}
@NotNull(message = "用户id不能为空", groups = LoginGroup.class)
private String userId;
@NotNull(message = "邮箱不能为空", groups = RegisterGroup.class)
private String email;
@Test
public void groupValidation() {
//可以通过追加组,同时检验多个组
set = validator.validate(userInfo,
UserInfo.RegisterGroup.class,
UserInfo.LoginGroup.class);
}
组排序校验
@GroupSequence({
//优先校验登录场景
LoginGroup.class,
RegisterGroup.class,
//未添加组的属于默认组
Default.class
})
public interface Group{}
自定义参数校验
1.自定义校验注解
//注解的作用域
@Target({ElementType.FIELD})
//注解的保留策略
@Retention(RetentionPolicy.RUNTIME)
//与注解关联的验证器
@Constraint(validatedBy = PhoneValidator.class)
public @interface Phone {
//注解校验输出信息
String message() default "手机号校验错误";
//所属组别
Class<?>[] groups() default {};
//约束注解的有效负载
Class<? extends Payload>[] payload() default {};
}
2.自定义验证器,实现ConstraintValidator接口
public class PhoneValidator implements ConstraintValidator<Phone,String> {
@Override
public boolean isValid(String s, ConstraintValidatorContext constraintValidatorContext) {
String check = "158\\d{8}";
Pattern regex = Pattern.compile(check);
//空值处理
String phone = Optional.ofNullable(s).orElse("");
Matcher matcher = regex.matcher(phone);
return matcher.matches();
}
}
3.注解校验字段
@Phone(message = "手机号要以158开头")
private String phone;
4.异常处理
注解实现
法1
1.定义全局异常处理的Handler
2.类上面添加@ControllerAdvice
3.法上面添加@ExceptionHandler(value = 异常的种类.class),当出现当前异常或者当前异常的子类时,就会调用该方法
//全局异常处理的注解
@RestControllerAdvice
public class ExceptionHandlerController {
/**
* 400 - Bad Request 参数绑定出错
*/
@ExceptionHandler(BindException.class)
public Result handleBindException(BindException e) {
return Result.of(ResultCode.BIND_ERROR);
}
/**
* 500 - Internal Server Error
*/
@ExceptionHandler(Exception.class)
public Result handleException(Exception e) {
return Result.of(ResultCode.EXCEPTION_ERROR);
}
}
法2
1.实现HandlerExceptionResolver接口
2.复写该接口中的方法,在这个方法里面还需要设置异常出现后的视图
3.定义一个错误页面
@Component
public class ExceptionHandler implements HandlerExceptionResolver {
@Override
public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) {
ModelAndView modelAndView = new ModelAndView();
//设置出现异常后调整的页面
modelAndView.setViewName("error");
modelAndView.addObject("errorMsg","系统正在维护,请联系管理员...");
return modelAndView;
}
}
<body>
<h1>错误</h1>
<p th:text="${errorMsg}"></p>
</body>