赞
踩
=>common\ResponseEnum(R枚举)
- public enum ResponseEnum {
-
- SUCCESS(200, "正确返回"),
- PAGE_NOT_FOUND(404, "找不到页面"),
- CODE_LOGIC_ERROR(500, "程序出错");
-
- private Integer status;
- private String message;
-
- private ResponseEnum(Integer status, String message){
- this.status = status;
- this.message = message;
- }
-
- public Integer getStatus(){
- return status;
- }
-
- public String getMessage(){
- return message;
- }
- }
=>common\R(R类)
- import lombok.AllArgsConstructor;
- import lombok.Data;
- import lombok.NoArgsConstructor;
-
- @Data
- @NoArgsConstructor
- @AllArgsConstructor
- public class R {
-
- private Integer status;
- private String message;
- private Object Data;
-
- public static R success(Object data, String message){
- R r = new R();
- r.setData(data);
- r.setMessage(message==null?ResponseEnum.SUCCESS.getMessage():message);
- r.setStatus(ResponseEnum.SUCCESS.getStatus());
- return r;
- }
-
- public static R success(Object data){
- return success(data, null);
- }
-
- public static R fail(Integer status, String message){
- R r = new R();
- r.setStatus(status);
- r.setMessage(message);
- r.setData(null);
- return r;
- }
-
- public static R fail(ResponseEnum responseEnum){
- R r = new R();
- r.setStatus(responseEnum.getStatus());
- r.setMessage(responseEnum.getMessage());
- r.setData(null);
- return r;
- }
- }
=>exception\Error(Error枚举)
- package com.wanghai.exception;
-
- public enum ErrorEnum {
-
- SUCCESS(true, 200, "正常结束"),
- PAGE_NOT_FOUND(false, 404, "页面找不到"),
- LOGIC_ERROR(false, 500, "代码逻辑错误"),
- BUSSINESS_ERROR(false, 20001, "业务出错"),
- ORDER_ERROR(false, 20002, "订单出错");
-
- private Integer status;
- private String message;
- private boolean resultFlag;
-
- private ErrorEnum(boolean resultFlag, Integer status, String message){
- this.resultFlag = resultFlag;
- this.status = status;
- this.message = message;
- }
-
- public Integer getStatus() {
- return status;
- }
-
- public String getMessage() {
- return message;
- }
-
- public boolean isResultFlag() {
- return resultFlag;
- }
- }
=>exception\ErrorHandler(Error处理器)
- package com.wanghai.exception;
-
- import lombok.Data;
-
- @Data
- public class ErrorHandler {
-
- private Integer status;
- private String massage;
- private String ExceptionClass;
-
- public static ErrorHandler fail(ErrorEnum errorEnum, Throwable throwable){
- ErrorHandler handler = new ErrorHandler();
- handler.setStatus(errorEnum.getStatus());
- handler.setMassage(errorEnum.getMessage());
- handler.setExceptionClass(throwable.getClass().getName());
- return handler;
- }
-
- public static ErrorHandler fail(ErrorEnum errorEnum, Throwable throwable, String message){
- ErrorHandler handler = fail(errorEnum, throwable);
- handler.setMassage(message);
- return handler;
- }
-
- // 针对自定义异常的 Handler
- public static ErrorHandler fail(ErrorEnum errorEnum, String message, Class classname){
- ErrorHandler handler = new ErrorHandler();
- handler.setStatus(errorEnum.getStatus());
- handler.setMassage(message==null?errorEnum.getMessage():message);
- handler.setExceptionClass(classname.toString());
- return handler;
- }
- }
=>exception\BussinessError & exception\OrderError(自定义异常)
- package com.wanghai.exception;
-
- import lombok.Data;
-
- @Data
- public class BussinessError extends RuntimeException{
- private Integer status;
- private String message;
-
- public BussinessError(Integer status, String message){
- this.status = status;
- this.message = message;
- }
-
- public BussinessError(ErrorEnum errorEnum){
- this.status = errorEnum.getStatus();
- this.message = errorEnum.getMessage();
- }
- }
- package com.wanghai.exception;
-
- import lombok.Data;
-
- @Data
- public class OrderError extends RuntimeException{
- private Integer status;
- private String message;
-
- public OrderError(Integer status, String message){
- this.status = status;
- this.message = message;
- }
-
- public OrderError(ErrorEnum errorEnum){
- this.status = errorEnum.getStatus();
- this.message = errorEnum.getMessage();
- }
- }
=>config\GlobalExceptionHandler(全局异常处理)
- import com.wanghai.exception.BussinessError;
- import com.wanghai.exception.ErrorEnum;
- import com.wanghai.exception.OrderError;
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.web.bind.annotation.ExceptionHandler;
- import org.springframework.web.bind.annotation.ResponseStatus;
- import org.springframework.web.bind.annotation.RestControllerAdvice;
-
- import javax.servlet.http.HttpServletRequest;
- import com.wanghai.exception.ErrorHandler;
-
-
- @RestControllerAdvice
- @Slf4j
- public class GlobalExceptionHandler {
-
- @ExceptionHandler(Throwable.class)
- public ErrorHandler makeException(Throwable throwable, HttpServletRequest request){
- ErrorHandler handler = ErrorHandler.fail(ErrorEnum.LOGIC_ERROR, throwable);
- log.error("错误发生的位置为:" + request.getRequestURL());
- System.out.println(handler);
- return handler;
- }
-
- @ExceptionHandler(OrderError.class)
- public ErrorHandler makeOrderException(HttpServletRequest request){
- ErrorHandler handler = ErrorHandler.fail(ErrorEnum.ORDER_ERROR, "订单出错了-c", OrderError.class);
- log.error("错误发生的位置为:" + request.getRequestURL());
- System.out.println(handler);
- return handler;
- }
-
- @ExceptionHandler(BussinessError.class)
- public ErrorHandler makeBussinessException(HttpServletRequest request){
- ErrorHandler handler = ErrorHandler.fail(ErrorEnum.ORDER_ERROR, "业务出错了-c", OrderError.class);
- log.error("错误发生的位置为:" + request.getRequestURL());
- System.out.println(handler);
- return handler;
- }
- }
=>config\ResultResponseHandler(统一接口返回)
- import com.fasterxml.jackson.databind.ObjectMapper;
- import com.wanghai.common.R;
- import com.wanghai.exception.ErrorHandler;
- import lombok.SneakyThrows;
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.core.MethodParameter;
- import org.springframework.http.MediaType;
- import org.springframework.http.server.ServerHttpRequest;
- import org.springframework.http.server.ServerHttpResponse;
- import org.springframework.web.bind.annotation.ControllerAdvice;
- import org.springframework.web.bind.annotation.RestControllerAdvice;
- import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;
-
-
- @RestControllerAdvice
- @Slf4j
- public class ResultResponseHandler implements ResponseBodyAdvice<Object> {
-
- @Override
- public boolean supports(MethodParameter returnType, Class converterType) {
- return true;
- }
-
- @SneakyThrows
- @Override
- public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType,
- Class selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {
-
- if(body instanceof ErrorHandler){
- ErrorHandler handler = (ErrorHandler) body;
- return R.fail(handler.getStatus(), handler.getMassage());
- } else if (body instanceof String){
- ObjectMapper objectMapper = new ObjectMapper();
- R r = R.success(body);
- return objectMapper.writeValueAsString(r);
- } else if(body instanceof R){
- return body;
- }
- return R.success(body);
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。