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 ,控制层通知器,这里用于统一拦截异常,进行响应处理。工作模式,如图:
https://github.com/JeffLi1993/springboot-learning-example:springboot-validation-over-json
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import javax.servlet.http.HttpServletRequest;
/**
* 统一错误码异常处理
*
* Created by bysocket on 14/03/2017.
*/
@RestControllerAdvice
public class GlobalErrorInfoHandler {
@ExceptionHandler(value = GlobalErrorInfoException.class)
public ResultBody errorHandlerOverJson(HttpServletRequest request,
GlobalErrorInfoException exception) {
ErrorInfoInterface errorInfo = exception.getErrorInfo();
ResultBody result = new ResultBody(errorInfo);
return result;
}
}
/**
* 错误码接口
*
* Created by bysocket on 13/03/2017.
*/
public interface ErrorInfoInterface {
String getCode();
String getMessage();
}
/**
* 应用系统级别的错误码
*
* Created by bysocket on 14/03/2017.
*/
public enum GlobalErrorInfoEnum implements ErrorInfoInterface{
SUCCESS("0", "success"),
NOT_FOUND("-1", "service not found");
private String code;
private String message;
GlobalErrorInfoEnum(String code, String message) {
this.code = code;
this.message = message;
}
public String getCode(){
return this.code;
}
public String getMessage(){
return this.message;
}
}
/**
* 统一错误码异常
*
* Created by bysocket on 14/03/2017.
*/
public class GlobalErrorInfoException extends Exception {
private ErrorInfoInterface errorInfo;
public GlobalErrorInfoException (ErrorInfoInterface errorInfo) {
this.errorInfo = errorInfo;
}
public ErrorInfoInterface getErrorInfo() {
return errorInfo;
}
public void setErrorInfo(ErrorInfoInterface errorInfo) {
this.errorInfo = errorInfo;
}
}
import org.spring.springboot.constant.CityErrorInfoEnum;
import org.spring.springboot.result.GlobalErrorInfoException;
import org.spring.springboot.result.ResultBody;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/**
* 错误码案例
*
* Created by bysocket on 16/4/26.
*/
@RestController
public class ErrorJsonController {
/**
* 获取城市接口
*
* @param cityName
* @return
* @throws GlobalErrorInfoException
*/
@RequestMapping(value = "/api/city", method = RequestMethod.GET)
public ResultBody findOneCity(@RequestParam("cityName") String cityName)
throws GlobalErrorInfoException {
// 入参为空
if (StringUtils.isEmpty(cityName)) {
throw new GlobalErrorInfoException(GlobalErrorInfoEnum.NOT_FOUND);
}
return new ResultBody(new City(1L,2L,"温岭","是我的故乡"));
}
}
- @ExceptionHandler 注解:标记了使用 errorHandlerOverJson() 方法来处理 GlobalErrorInfoException 异常。
- @RestControllerAdvice : @ControllerAdvice 和 @ResponseBody 的语义结合。是控制器增强,直接返回对象。这里用于统一拦截异常,然后返回错误码对象体。
- @ResponseBody 作用: 该注解用于将 Controller 的方法返回的对象,通过适当的 HttpMessageConverter 转换为指定格式后,写入到 Response 对象的 body 数据区。