Skip to content

Latest commit

 

History

History
49 lines (43 loc) · 1.03 KB

readme.md

File metadata and controls

49 lines (43 loc) · 1.03 KB

@Valid 参数校验

  • 实体或者值对象的field加@xxx
  • 方法入口参数加@Valid注解
  • 自定义的注解需实现ConstraintValidator<IsPhoneNumber, String>接口

异常统一拦截

  • 根据类型捕获异常,最后返回前端
@ControllerAdvice  
@ResponseBody  
public class GlobalExceptionHandler {  
    @ExceptionHandler(value = Exception.class)

拦截器Interceptor

  • 实现
@Component
public class LoginRequiredInterceptor implements HandlerInterceptor {
  • 注册拦截器
@Component
public class ToutiaoWebConfiguration extends WebMvcConfigurer{
    @Override
    public void addInterceptors(InterceptorRegistry registry) {

方法参数注入

  • 实现
@Service
public class UserArgumentResolver implements HandlerMethodArgumentResolver {
}
  • 注册,属于参数解析
@Configuration
public class WebConfig implements WebMvcConfigurer {
    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers){