本文主要是介绍项目开发经验规范总结-时刻更新,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1、常用规范类
1.1、异常相关
1.1.1、业务异常类
package com.healerjean.proj.exception;import com.healerjean.proj.enums.ResponseEnum;/*** 系统业务异常*/
public class BusinessException extends RuntimeException {private int code;public BusinessException(int code) {this.code = code;}public BusinessException(String message) {super(message);this.code = ResponseEnum.逻辑错误.code;}public BusinessException(int code, String message) {super(message);this.code = code;}public BusinessException(ResponseEnum responseEnum) {super(responseEnum.msg);this.code = responseEnum.code ;}public BusinessException(ResponseEnum responseEnum,String message) {super(message);this.code = responseEnum.code ;}public BusinessException(String message, Throwable cause) {super(message, cause);this.code = ResponseEnum.逻辑错误.code;}public BusinessException(int code ,Throwable e) {super(e);this.code = code;}public BusinessException(ResponseEnum responseEnum, Throwable t) {super(responseEnum.msg, t);this.code = responseEnum.code;}public void setCode(int code) {this.code = code;}public int getCode() {return code;}}
1.1.2、参数异常类
package com.healerjean.proj.exception;import com.healerjean.proj.enums.ResponseEnum;/*** @author HealerJean* @ClassName ParameterErrorException* @date 2019/10/17 16:19.* @Description 参数错误*/
public class ParameterErrorException extends com.healerjean.proj.exception.BusinessException {public ParameterErrorException() {super(ResponseEnum.参数错误);}public ParameterErrorException(ResponseEnum responseEnum) {super(ResponseEnum.参数错误, responseEnum.msg);}public ParameterErrorException(String msg) {super(ResponseEnum.参数错误, msg);}}
1.1.3、接口异常类
package com.healerjean.proj.exception;import com.healerjean.proj.enums.ResponseEnum;/*** @author HealerJean* @ClassName HaoDanKuApiException* @date 2019/10/15 20:08.* @Description*/
public class HaoDanKuApiException extends BusinessException {public HaoDanKuApiException( ) {super(ResponseEnum.好单库口请求异常);}public HaoDanKuApiException(String msg) {super(ResponseEnum.好单库接口数据异常, msg);}public HaoDanKuApiException(Throwable e) {super(ResponseEnum.好单库口请求异常, e);}}
1.1.4、异常全局处理
package com.healerjean.proj.config;import com.healerjean.proj.dto.ResponseBean;
import com.healerjean.proj.enums.ResponseEnum;
import com.healerjean.proj.exception.BusinessException;
import com.healerjean.proj.exception.ParameterErrorException;
import com.healerjean.proj.utils.JsonUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageConversionException;
import org.springframework.validation.BindException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;import javax.servlet.http.HttpServletResponse;
import javax.validation.UnexpectedTypeException;
import java.util.HashMap;
import java.util.Map;/*** @author HealerJean* @version 1.0v* @ClassName ControllerHandleExceptionConfig* @date 2019/5/31 20:19.* @Description*/
@Slf4j
@ControllerAdvice
public class ControllerHandleConfig {/*** 不支持的请求方始*/@ExceptionHandler(value = HttpRequestMethodNotSupportedException.class)@ResponseStatus(value = HttpStatus.METHOD_NOT_ALLOWED)public ResponseBean methodNotSupportExceptionHandler(HttpRequestMethodNotSupportedException e) {log.error("不支持的请求方式", e);return ResponseBean.buildFailure(ResponseEnum.不支持的请求方式.code, e.getMessage());}/*** 参数类型错误* 1、(BindException : 比如 Integer 传入String )* Field error in object 'demoDTO' on field 'age': rejected value [fasdf]; codes [typeMismatch.demoDTO.age,typeMismatch.age,typeMismatch.java.lang.Integer,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [demoDTO.age,age]; arguments []; default message [age]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.lang.Integer' for property 'age'; nested exception is java.lang.NumberFormatException: For input string: "fasdf"]*/@ExceptionHandler(value = {BindException.class})@ResponseStatus(HttpStatus.BAD_REQUEST)@ResponseBodypublic ResponseBean bindExceptionHandler(BindException e) {log.error("====参数类型错误===", e);return ResponseBean.buildFailure(ResponseEnum.参数类型错误.code, e.getMessage());}/*** 参数格式问题*/@ExceptionHandler(value = {MethodArgumentTypeMismatchException.class, HttpMessageConversionException.class, UnexpectedTypeException.class})@ResponseStatus(HttpStatus.BAD_REQUEST)@ResponseBodypublic ResponseBean httpMessageConversionExceptionHandler(Exception e) {log.error("====参数格式异常===", e);return ResponseBean.buildFailure(ResponseEnum.参数格式异常.code, e.getMessage());}/*** 参数错误*/@ExceptionHandler(value = ParameterErrorException.class)@ResponseStatus(HttpStatus.BAD_REQUEST)@ResponseBodypublic ResponseBean parameterErrorExceptionHandler(ParameterErrorException e) {log.error("参数异常------------参数错误:code:{},message:{}", e.getCode(), e.getMessage());return ResponseBean.buildFailure(e.getCode(), e.getMessage());}/*** 业务异常,给前台返回异常数据*/@ExceptionHandler(value = BusinessException.class)@ResponseStatus(HttpStatus.BAD_REQUEST)@ResponseBodypublic ResponseBean businessExceptionHandler(BusinessException e) {log.error("业务异常------------异常信息:code:{},message{}" ,e.getCode(), e.getMessage());return ResponseBean.buildFailure(e.getCode(),e.getMessage());}/*** 所有异常报错*/@ExceptionHandler@ResponseBodypublic HttpEntity<ResponseBean> allExceptionHandler(HttpServletResponse response, Exception e) {log.error("====系统错误===", e);response.setStatus(ResponseEnum.系统错误.code);return returnMessage(ResponseBean.buildFailure(ResponseEnum.系统错误));}private HttpEntity<ResponseBean> returnMessage(ResponseBean responseBean) {HttpHeaders header = new HttpHeaders();header.add("Content-Type", "application/json");header.add("Charset", "UTF-8");return new HttpEntity<>(responseBean, header);}/*** 参数非法* 1、(BindException : 比如 Integer 传入abc )*/// @ExceptionHandler(value = {MethodArgumentTypeMismatchException.class, HttpRequestMethodNotSupportedException.class, HttpMessageConversionException.class, BindException.class, UnexpectedTypeException.class})// @ResponseBody// public HttpEntity<ResponseBean> httpMessageConversionExceptionHandler(HttpServletResponse response, Exception e) {// log.error("====参数格式异常===", e);// // 等同于 @ResponseStatus(HttpStatus.BAD_REQUEST)// // 但是setStatus 不能比随便设置,最好一般情况下不要和HttpStatus 有重复的,这样有可能会造成没有输出Response body// response.setStatus(ResponseEnum.参数格式异常.code);// return returnMessage(ResponseBean.buildFailure(ResponseEnum.参数格式异常));// }// @ExceptionHandler(value ={HttpMessageConversionException.class, BindException.class} )// @ResponseBody// public HttpEntity<ResponseBean> httpMessageConversionExceptionHandler(Exception e) {// log.error("====参数格式异常===", e);// return new ResponseEntity<>(ResponseBean.buildFailure(ResponseEnum.参数格式异常),HttpStatus.BAD_REQUEST);// }}
这篇关于项目开发经验规范总结-时刻更新的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!