Spring Boot Start Web 的依赖项可以被分为

  • Spring - core,beans,context,aop
  • Web MVC - (Spring MVC)
  • Jackson - for JSON Binding
  • Validation - Hibernate,Validation API
  • Enbedded Servlet Container - Tomcat
  • Logging - logback,slf4j

任何经典的 Web 应用程序都会使用所有这些依赖项。Spring Boot Starter Web 预先打包了这些依赖项。

Validation

HTTP over JSON 形式中很多涉及到返回码,错误码相关的处理。比如xxx参数不完整,权限不足,用户不存在等。

怎么统一处理认为是异常的场景呢?
利用的是 Spring 4.x 提供的 RestControllerAdvice。这里做下说明,也可以根据 ControllerAdvice 去实现。这里案例是 HTTP over JSON 模式,所以直接利用
RestControllerAdvice ,控制层通知器,这里用于统一拦截异常,进行响应处理。工作模式,如图:
image.png

https://github.com/JeffLi1993/springboot-learning-examplespringboot-validation-over-json

  1. import org.springframework.web.bind.annotation.ExceptionHandler;
  2. import org.springframework.web.bind.annotation.RestControllerAdvice;
  3. import javax.servlet.http.HttpServletRequest;
  4. /**
  5. * 统一错误码异常处理
  6. *
  7. * Created by bysocket on 14/03/2017.
  8. */
  9. @RestControllerAdvice
  10. public class GlobalErrorInfoHandler {
  11. @ExceptionHandler(value = GlobalErrorInfoException.class)
  12. public ResultBody errorHandlerOverJson(HttpServletRequest request,
  13. GlobalErrorInfoException exception) {
  14. ErrorInfoInterface errorInfo = exception.getErrorInfo();
  15. ResultBody result = new ResultBody(errorInfo);
  16. return result;
  17. }
  18. }
  1. /**
  2. * 错误码接口
  3. *
  4. * Created by bysocket on 13/03/2017.
  5. */
  6. public interface ErrorInfoInterface {
  7. String getCode();
  8. String getMessage();
  9. }
  10. /**
  11. * 应用系统级别的错误码
  12. *
  13. * Created by bysocket on 14/03/2017.
  14. */
  15. public enum GlobalErrorInfoEnum implements ErrorInfoInterface{
  16. SUCCESS("0", "success"),
  17. NOT_FOUND("-1", "service not found");
  18. private String code;
  19. private String message;
  20. GlobalErrorInfoEnum(String code, String message) {
  21. this.code = code;
  22. this.message = message;
  23. }
  24. public String getCode(){
  25. return this.code;
  26. }
  27. public String getMessage(){
  28. return this.message;
  29. }
  30. }
  31. /**
  32. * 统一错误码异常
  33. *
  34. * Created by bysocket on 14/03/2017.
  35. */
  36. public class GlobalErrorInfoException extends Exception {
  37. private ErrorInfoInterface errorInfo;
  38. public GlobalErrorInfoException (ErrorInfoInterface errorInfo) {
  39. this.errorInfo = errorInfo;
  40. }
  41. public ErrorInfoInterface getErrorInfo() {
  42. return errorInfo;
  43. }
  44. public void setErrorInfo(ErrorInfoInterface errorInfo) {
  45. this.errorInfo = errorInfo;
  46. }
  47. }
  1. import org.spring.springboot.constant.CityErrorInfoEnum;
  2. import org.spring.springboot.result.GlobalErrorInfoException;
  3. import org.spring.springboot.result.ResultBody;
  4. import org.springframework.util.StringUtils;
  5. import org.springframework.web.bind.annotation.RequestMapping;
  6. import org.springframework.web.bind.annotation.RequestMethod;
  7. import org.springframework.web.bind.annotation.RequestParam;
  8. import org.springframework.web.bind.annotation.RestController;
  9. /**
  10. * 错误码案例
  11. *
  12. * Created by bysocket on 16/4/26.
  13. */
  14. @RestController
  15. public class ErrorJsonController {
  16. /**
  17. * 获取城市接口
  18. *
  19. * @param cityName
  20. * @return
  21. * @throws GlobalErrorInfoException
  22. */
  23. @RequestMapping(value = "/api/city", method = RequestMethod.GET)
  24. public ResultBody findOneCity(@RequestParam("cityName") String cityName)
  25. throws GlobalErrorInfoException {
  26. // 入参为空
  27. if (StringUtils.isEmpty(cityName)) {
  28. throw new GlobalErrorInfoException(GlobalErrorInfoEnum.NOT_FOUND);
  29. }
  30. return new ResultBody(new City(1L,2L,"温岭","是我的故乡"));
  31. }
  32. }
  • @ExceptionHandler 注解:标记了使用 errorHandlerOverJson() 方法来处理 GlobalErrorInfoException 异常。
  • @RestControllerAdvice : @ControllerAdvice 和 @ResponseBody 的语义结合。是控制器增强,直接返回对象。这里用于统一拦截异常,然后返回错误码对象体。
  • @ResponseBody 作用: 该注解用于将 Controller 的方法返回的对象,通过适当的 HttpMessageConverter 转换为指定格式后,写入到 Response 对象的 body 数据区。