一。标签
@PathVariable:获取url中的参数 例如:http://localhost:8090/girl/say/1
@RequestMapping(value="/say/{id}", method=RequestMethod.GET) public String say(@PathVariable(value = "id") Integer id) { return "id:"+id; }
@RequestParam: 获取http://localhost:8090/girl/say?id=1
@Value : 读取application.yml中的配置项
@ConfigurationProperties:从application.yml中的配置项封装到类
@Component @ConfigurationProperties(prefix = "girl") public class Girl { private String cupSize; private String age; public String getCupSize() { return cupSize; } public void setCupSize(String cupSize) { this.cupSize = cupSize; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } }
girl:
cupSize: F
age: 20
二。AOP拦截器
@Aspect,@Before,@After,@AfterReturning
@Aspect @Component public class HttpAspect { Logger log = LoggerFactory.getLogger(this.getClass()); @Pointcut("execution(public * com.yjm.test1.controller.GirlController.*(..))") public void log() { } @Before("log()") public void beforeLog(JoinPoint joinPoint) { ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); HttpServletRequest request = attributes.getRequest(); // url log.info("url={}", request.getRequestURL()); //method log.info("method={}", request.getMethod()); //ip log.info("ip={}", request.getRemoteAddr()); //class log.info("class={}", joinPoint.getClass()+"."+joinPoint.getSignature().getDeclaringTypeName()); //参数 log.info("args={}", joinPoint.getArgs()); log.info("beforeLog !!!"); } @After("log()") public void afterLog() { log.info("afterLog !!!"); } @AfterReturning(returning = "object", pointcut = "log()") public void returnLog(Object object) { log.info("return={}", object.toString()); }
三。表单校验
@Valid
首先实体类中使用@Size,@Max,@Min等条件约束
@Size(min = 1,max = 1)
private String cupSize;
再在control层 用@Valid和BindingResult 来校验并获取错误信息
@RequestMapping(value = "/girls", method = RequestMethod.POST) public Result addOne(@Valid Girl girl, BindingResult bindingResult){ if(bindingResult.hasErrors()) { logger.error(bindingResult.getFieldError().getDefaultMessage()); return ResultUtil.error(1, bindingResult.getFieldError().getDefaultMessage()); } return ResultUtil.success(girlRespository.save(girl)); }
四。统一异常处理
1.自定义异常
public class GirlException extends RuntimeException { private Integer code; public GirlException(Integer code, String msg) { super(msg); this.code = code; } public Integer getCode() { return code; } public void setCode(Integer code) { this.code = code; } }
2.定义异常处理类
@ControllerAdvice public class ExceptionHandle { Logger logger = LoggerFactory.getLogger(this.getClass()); @ExceptionHandler(Exception.class) @ResponseBody public Result handle(Exception e) { if(e instanceof GirlException) { return ResultUtil.error(((GirlException) e).getCode(),e.getMessage()); } else { logger.error("未知错误", e); return ResultUtil.error(-1, "未知错误"); } } }
@ControllerAdvice:controller增强处理
抛出异常都会先走这里
五。返回json中忽略null
5.1全局配置
sping:
jackson:
default-property-inclusion: non_null
5.2 单独类配置
@JsonInclude(Include.NON_NULL) public class WithdrawDetail implements Serializable { }