1.特殊pom
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.10</version>
</dependency>
2.ResultVo
@Data
public class ResultVo {
private Boolean success;
private Integer code;
private String message;
private Map<String, Object> data = new HashMap<>();
private ResultVo() {
}
public static ResultVo ok() {
ResultVo r = new ResultVo();
r.setSuccess(ResultCodeEnum.SUCCESS.getSuccess());
r.setCode(ResultCodeEnum.SUCCESS.getCode());
r.setMessage(ResultCodeEnum.SUCCESS.getMessage());
return r;
}
public static ResultVo error() {
ResultVo r = new ResultVo();
r.setSuccess(ResultCodeEnum.UNKNOWN_ERROR.getSuccess());
r.setCode(ResultCodeEnum.UNKNOWN_ERROR.getCode());
r.setMessage(ResultCodeEnum.UNKNOWN_ERROR.getMessage());
return r;
}
public static ResultVo setResult(ResultCodeEnum result) {
ResultVo r = new ResultVo();
r.setSuccess(result.getSuccess());
r.setCode(result.getCode());
r.setMessage(result.getMessage());
return r;
}
public ResultVo data(Map<String, Object> map) {
this.setData(map);
return this;
}
public ResultVo data(String key, Object value) {
this.data.put(key, value);
return this;
}
public ResultVo message(String message) {
this.setMessage(message);
return this;
}
public ResultVo code(Integer code) {
this.setCode(code);
return this;
}
public ResultVo success(Boolean success) {
this.setSuccess(success);
return this;
}
}
3.ResultCodeEnum
@Getter
public enum ResultCodeEnum {
SUCCESS(true, 20000, "成功"),
UNKNOWN_ERROR(false, 20001, "未知错误"),
PARAM_ERROR(false, 20002, "参数错误"),
NULL_POINT(false, 20003, "空指针异常"),
HTTP_CLIENT_ERROR(false, 20004, "客户端连接异常"),
;
private Boolean success;
private Integer code;
private String message;
ResultCodeEnum(boolean success, Integer code, String message) {
this.success = success;
this.code = code;
this.message = message;
}
}
3.自定义异常和统一异常处理
@Data
public class CMSException extends RuntimeException {
private Integer code;
public CMSException(Integer code, String message) {
super(message);
this.code = code;
}
public CMSException(ResultCodeEnum resultCodeEnum) {
super(resultCodeEnum.getMessage());
this.code = resultCodeEnum.getCode();
}
@Override
public String toString() {
return "CMSException{" + "code=" + code + ", message=" + this.getMessage() + '}';
}
}
@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
@ExceptionHandler(NullPointerException.class)
@ResponseBody
public ResultVo error(NullPointerException e) {
e.printStackTrace();
return ResultVo.setResult(ResultCodeEnum.NULL_POINT);
}
@ExceptionHandler(HttpClientErrorException.class)
@ResponseBody
public ResultVo error(IndexOutOfBoundsException e) {
e.printStackTrace();
return ResultVo.setResult(ResultCodeEnum.HTTP_CLIENT_ERROR);
}
@ExceptionHandler(CMSException.class)
@ResponseBody
public ResultVo error(CMSException e) {
e.printStackTrace();
return ResultVo.error().message(e.getMessage()).code(e.getCode());
}
}
4.日志处理
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<property name="LOG_PATTERN"
value="[%d{yyyy-MM-dd HH\:mm\:ss}] [%p] [%c] %m%n" />
<property name="FILE_PATH" value="N:\\Demo_JAVA\\logs\\%d{yyyy-MM-dd}.%i.log" />
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>${LOG_PATTERN}</pattern>
</encoder>
</appender>
<appender name="rollingFile"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>
${FILE_PATH}
</fileNamePattern>
<maxHistory>30</maxHistory>
<timeBasedFileNamingAndTriggeringPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>10MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
<encoder>
<pattern>${LOG_PATTERN}</pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="console" />
<appender-ref ref="rollingFile" />
</root>
</configuration>
5.实例
@RestController
@RequestMapping("/checkParam")
@Slf4j
public class CheckParam {
private static final Logger logger = LoggerFactory.getLogger(CheckParam.class);
@GetMapping("/getBaseEntity")
public ResultVo getUserStr(@Validated BaseEntity baseEntity, BindingResult result) {
logger.info("11111111111111111111111111111");
return ResultVo.ok().data("baseEntity", baseEntity);
}
}