欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

SpringBoot自定义返回code值

程序员文章站 2022-05-03 17:59:00
关于HttpStatus在SpringBoot中关于状态码有一个枚举类型,如下。public enum HttpStatus { CONTINUE(100, "Continue"), SWITCHING_PROTOCOLS(101, "Switching Protocols"), PROCESSING(102, "Processing"), CHECKPOINT(103, "Checkpoint"), OK(200, "OK"), ...}通...

关于HttpStatus

在SpringBoot中关于状态码有一个枚举类型,如下。

public enum HttpStatus {
    CONTINUE(100, "Continue"),
    SWITCHING_PROTOCOLS(101, "Switching Protocols"),
    PROCESSING(102, "Processing"),
    CHECKPOINT(103, "Checkpoint"),
    OK(200, "OK"),
    ...
}

通过以下三种方法均可实现

1. 通过@ResponseStatus注解

eg:

@ResponseStatus(code=HttpStatus.INTERNAL_SERVER_ERROR,reason="server error")
public class ServerException extends Exception {
	...
}

在全局异常捕捉的类中定义,可以自定义特定的异常来返回特定的状态码。

2. 修改responsestatus属性

eg:

@RequestMapping(value = "/index", method = RequestMethod.GET)
public void index(HttpServletResponse response) throws IOException{
    response.setStatus(500);
    response.getWriter().append("server error");
}

需要多加response参数,但这样的控制器都必须为void,有诸多限制。

3. ResponseEntity

eg:

@RequestMapping(value = "/user", method = RequestMethod.GET)
public ResponseEntity<Map<String,Object>> getUser() throws IOException{
    Map<String,Object> map = new HashMap<String,Object>();
    map.put("name", "user");
    return new ResponseEntity<Map<String,Object>>(map,HttpStatus.OK);
}

这样返回类型就只能是ResponseEntity


前后端分离开发一般都会自定义返回的数据格式。
都会包含msg,code,data三个属性。
可用上述思路实现以下工具类。实现自定义返回的状态码,需将返回格式设置为以下类的格式,将返回数据传入data

JSONResult.java

public class JSONResult<T> extends ResponseEntity<Message> {

    public JSONResult(HttpStatus status) {
        super(status);
    }

    public JSONResult(String code, String msg, T data) {
        super(Message.custom(code, msg, data), Message.num2HttpStatus(code));
    }

    public JSONResult(String code, String msg) {
        super(Message.custom(code, msg), Message.num2HttpStatus(code));
    }

    public static <T> JSONResult<T> success(T data) {
        return new JSONResult("200", "成功", data);
    }

    public static <T> JSONResult<T> failed(T data) {
        return new JSONResult("422", "失败", data);
    }

    public static <T> JSONResult<T> failMsg(String msg) {
        return new JSONResult("422", msg);
    }

    public static <T> JSONResult<T> custom(String code, String msg, T data) {
        return new JSONResult(code, msg, data);
    }
}

class Message<T> {

    String status;
    //向前端返回的内容
    String message;

    T data;

    public Message() {
    }

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

    public Message(String status, String message, T data) {
        this.data = data;
        this.status = status;
        this.message = message;
    }

    public static <T> Message<T> custom(String status, String message, T data) {
        return new Message(status, message, data);
    }

    public static <T> Message<T> custom(String status, String message) {
        return new Message(status, message);
    }

    public static HttpStatus num2HttpStatus(String code) {
        HttpStatus status = HttpStatus.NOT_FOUND;
        for (HttpStatus httpStatus : HttpStatus.values()) {
            boolean b = Integer.parseInt(code) == httpStatus.value();
            if (b) {
                return httpStatus;
            }
        }
        return status;
    }

    public String getStatus() {
        return status;
    }

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

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

}

 

通过继承ResponseEntity类来实现自定义返回类的自定义状态码返回。

#@Le.Hao#

帮助到您请点赞关注收藏谢谢!!

本文地址:https://blog.csdn.net/qq_18432653/article/details/110792336

相关标签: springboot