spring @ExceptionHandler+@ControllerAdvice异常拦截处理类

1.BadRequestExceptionHandler  通用异常拦截处理类

使用@ExceptionHandler,可以处理异常, 但是仅限于当前Controller中处理异常, @ControllerAdvice可以配置basePackage下的所有controller. 所以结合两者使用,就可以处理全局的异常了

/**
 * 通用异常拦截处理类(通过切面的方式默认拦截所有的controller异常)
 */
@ControllerAdvice
@ResponseBody
public class BadRequestExceptionHandler {
	private static final Logger logger = LoggerFactory.getLogger(BadRequestExceptionHandler.class);

	/**
	 * 校验错误拦截处理
	 *
	 * @param exception
	 *            错误信息集合
	 * @return 错误信息
	 */
	@ExceptionHandler(MethodArgumentNotValidException.class)
	public Object validationBodyException(MethodArgumentNotValidException exception) {
		Rsp rsp = new Rsp();
		BindingResult result = exception.getBindingResult();
		if (result.hasErrors()) {
			List<ObjectError> errors = result.getAllErrors();
			errors.forEach(p -> {
				FieldError fieldError = (FieldError) p;
				logger.error("数据校验失败 : 对象 {" + fieldError.getObjectName() + "},参数{" + fieldError.getField() + "},错误信息{"
						+ fieldError.getDefaultMessage() + "}");
				rsp.setRspDesc(fieldError.getDefaultMessage());
			});
		}
		return WebUtil.resp(rsp);
	}

	/**
	 * 参数类型转换错误
	 *
	 * @param exception
	 *            错误
	 * @return 错误信息
	 */
	@ExceptionHandler(HttpMessageConversionException.class)
	public Object parameterTypeException(HttpMessageConversionException exception) {

		logger.error(exception.getCause().getLocalizedMessage());
		// return ServiceResult.error("类型转换错误");
		return "类型转换错误";

	}

}

2.WebUtil类

public class WebUtil {

	private static Logger logger = LoggerFactory.getLogger(RenewalNoticeController.class);

	@SuppressWarnings("rawtypes")
	public static Object resp(Rsp rsp) {
		ServiceResponse<Object> serviceResponse = new ServiceResponse<>();
		serviceResponse.setRsp(rsp);
		String result = JSONObject.toJSONString(serviceResponse);
		logger.info("NNNN" + result);
		return serviceResponse;
	}
}

 3.ServiceResponse 服务应答类

/**
 * 服务应答类
 */
public class ServiceResponse<DATA> {
	private static Logger logger = LoggerFactory.getLogger(ServiceResponse.class);

	@ApiModelProperty(name = "STATUS", value = "服务请求结果编码", required = true, example = "0000")
	@JsonProperty("STATUS")
	@JSONField(name = "STATUS")
	private String status;
	@ApiModelProperty(name = "MSG", value = "服务请求结果描述", required = true, example = "服务调用成功!")
	@JsonProperty("MSG")
	@JSONField(name = "MSG")
	private String msg;
	@ApiModelProperty(name = "RSP", value = "服务返回业务数据", required = true)
	@JsonProperty("RSP")
	@JSONField(name = "RSP")
	private Rsp<DATA> rsp;


	/**
	 * 服务成功调用应答信息
	 * 
	 * @param rsp
	 * @return ServiceResponse
	 */
	public ServiceResponse getSuccessResponse(Rsp<DATA> rsp) {
		this.setStatus(ServiceConstant.STATUS_SUCCESS);
		this.setMsg(ServiceConstant.MSG_SUCCESS);
		this.setRsp(rsp);
		return this;
	}

	/**
	 * 服务其他错误返回结果
	 * 
	 * @param errorStatus
	 * @param errorMsg
	 * @param rsp
	 * @return
	 */
	public ServiceResponse getErrorResponse(String errorStatus, String errorMsg, Rsp<DATA> rsp) {
		this.setStatus(errorStatus);
		this.setMsg(errorMsg);
		this.setRsp(rsp);
		return this;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public Rsp<DATA> getRsp() {
		return rsp;
	}

	public void setRsp(Rsp<DATA> rsp) {
		this.rsp = rsp;
	}	
}

 

 

©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页