标签:server conf 异常处理 scan dem gap int efault 需要
1.rest风格
2.@Pathvariable注解
2.1配置正则实例
@AllArgsConstructor @NoArgsConstructor @Data @Accessors(chain = true) //lombok public class User implements Serializable { private String name; }
@RestController //responsebody +controller public class UserController { @GetMapping("/user/{name:.*}") public User testUser(@PathVariable(required = false,value = "name") User user){ System.out.println(user.getClass()); return user; } }
@SpringBootApplication public class MvcDemoApplication { public static void main(String[] args) { SpringApplication.run(MvcDemoApplication.class, args); } }
@Configuration @ComponentScan(value = "springmvc.demo")//扫描包 public class MVCConfig { }
spring.http.encoding.charset=UTF-8 #处理编码 spring.http.encoding.force=true spring.http.encoding.enabled=true server.tomcat.uri-encoding=UTF-8
3.@valid与@Validated
@valid与@Validated的区别,@Validated是spring中的,是在@valid基础上而来的,在@valid的基础上增加分组功能,这里就直接说@Validated,没有加分组都需要认证,加了分组只有符合分组的才需要认证,一般不使用分组
3.1基本使用
@AllArgsConstructor @NoArgsConstructor @Data @Accessors(chain = true) public class User implements Serializable { @NotNull(message="不能为空")//含有属性groups private String name; }
@RestController public class UserController { @GetMapping("/user") public User testUser(@Validated User user, BindingResult result){ result.getAllErrors().stream().forEach((x)->{ System.out.println(x.getObjectName()+":"+x.getDefaultMessage()); }); System.out.println(user.getClass()); return user; } }
输出结果:user:不能为空
备注:假如没有BindingResult result,那么在进入testUser之前就会被拦截,message="不能为空"并没有什么作用,加入BindingResult result之后,才会进入testUser方法,在没有进入方法时的异常页面见后该篇4
3.2自定义符合valid的注解
这里定义的是@NoNull注解
//会自动加载,不需要其他的配置 public class MyValidAnnotation implements ConstraintValidator<MyNotNull,Object> { /* obj:需要验证的参数 */ @Override public boolean isValid(Object obj, ConstraintValidatorContext constraintValidatorContext) { System.out.println(obj); return false; } @Override public void initialize(MyNotNull constraintAnnotation) { } }
@Target({ElementType.METHOD, ElementType.FIELD}) @Retention(RetentionPolicy.RUNTIME) @Constraint(validatedBy =MyValidAnnotation.class ) public @interface MyNotNull { String message() default "{javax.validation.constraints.NotNull.message}"; Class<?>[] groups() default {}; Class<? extends Payload>[] payload() default {}; }
@AllArgsConstructor @NoArgsConstructor @Data @Accessors(chain = true) public class User implements Serializable { @MyNotNull(message="不能为空---MyNotNull") private String name; }
@RestController public class UserController { @GetMapping("/user") public User testUser(@Validated User user, BindingResult result){ result.getAllErrors().stream().forEach((x)->{ System.out.println(x.getObjectName()+":"+x.getDefaultMessage()); }); return user; } }
4.异常消息的处理
4.1验证异常消息的处理上述已经说明
4.2直接被拦截的异常处理
springboot会相应的转向我们在resources下新建的resources的error的相应的错误代码的页面
还可以是如下这样,表示以4开头的错误页面
4.3运行过程中出现的异常
4.3.1直接抛出异常
@GetMapping("/user/error") public User testUser1(@Validated User user, BindingResult result) throws Exception { throw new Exception("出错了"); }
4.3.2 throw自定义异常类
@ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR,reason = "内部出错了!") public class MyException extends RuntimeException { }
@GetMapping("/user/error") public User testUser1(@Validated User user, BindingResult result) throws Exception { throw new MyException(); }
4.3.3 使用@ExceptionHandler
注解
@ResponseBody @GetMapping("/testExceptionHandler") public User testUser1(@Validated User user, BindingResult result) throws Exception { int i=10/0; return user; }
@ExceptionHandler({Exception.class}) public String testExceptionHandler(){ return "redirect:/error.html"; //重定向 }
4.3.4 修改状态码及原因,@ResponseStatus也能在
@ExceptionHandler({Exception.class}) @ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR,reason = "内部错误") public void testExceptionHandler(){ }
备注:这样之后不会去相应的页面,也就是加入有返回值例如return "redirect:/error.html";,会失效
4.3.5@ControllerAdvice定制全局的异常,类中的@ExceptionHandler优先级高于@ControllerAdvice中的@ExceptionHandler优先级
@ControllerAdvice public class MyExceptionHandler { @ExceptionHandler({Exception.class}) @ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR,reason = "内部错误") public void testExceptionHandler(){ } }
5.jsonview
标签:server conf 异常处理 scan dem gap int efault 需要
原文地址:https://www.cnblogs.com/gg128/p/9940941.html