SpringBoot 统一返回数据格式及异常统一捕获返回数据格式

统一返回数据格式

import com.alibaba.druid.support.json.JSONUtils;
import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;

/**
 * 对Rest请求的返回结果进行统一包装
 */
@ControllerAdvice(annotations = {Controller.class, RestController.class})
public class ApiResponseHandler implements ResponseBodyAdvice<Object> {
    @Override
    public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) {
        return true;
    }

    @Override
    public Object beforeBodyWrite(Object body, MethodParameter methodParameter, MediaType selectedContentType, Class<? extends HttpMessageConverter<?>> selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {
        String name = methodParameter.getParameterType().getName();
        if ("void".equals(name)) {
            return BaseResponse.ok();
        }
        if (body instanceof String) {
			
            return JSONUtils.toJSONString(BaseResponse.ok(body));
        }
        return BaseResponse.ok(body);

    }
}

注意:如果返回的格式为String类型,那么需要先进行组装,然后使用JSONUtils.toJSONString()转换下。

  • 效果
{
    "data": [
        {
            "id": "1001T11000000000170U",
            "code": "YC002",
            "name": "YC1.2",
            "displayName": "YC1.2",
            "abbrName": null
        }
    ],
    "status": 200,
    "message": "操作成功",
    "srvTime": 1647500769523
}

异常统一捕获返回数据格式

@ControllerAdvice
public class MyControllerAdvice {

    @ResponseBody
    @ExceptionHandler(Exception.class)
    public BaseResponse error(Exception e){
        return  BaseResponse.error();
    }
}
  • 效果:
{
    "data": null,
    "status": 500,
    "message": "失败",
    "srvTime": 1647500908548
}

统一返回包装类

@Data
@AllArgsConstructor
@NoArgsConstructor
public class BaseResponse<T> {

    private T data;
    private int status = 200;
    private String message;
    private long srvTime = System.currentTimeMillis();

    public BaseResponse(String message) {
        this.message = message;
    }

    public BaseResponse(int status) {
        this.status = status;
    }

    public BaseResponse(String message,int status) {
        this.message = message;
        this.status = status;
    }

    public BaseResponse<T> setData(T data) {
        this.data = data;
        return this;
    }

    public static <T> BaseResponse<T> ok() {
        return new BaseResponse<>("操作成功");
    }

    public static <T> BaseResponse<T> ok(T data) {
        return new BaseResponse<T>("操作成功").setData(data);
    }

    public static <T> BaseResponse<T> error() {

        return new BaseResponse<>("失败",500);
    }

}

你可能感兴趣的:(SpringBoot,spring,boot)