【Java工具类】Java--全局API接口返回的ResponseModel类

大千世界,茫茫人海,相识就是一种缘分。若此篇文章对您有帮助,点个赞或点个关注呗!

全局API接口返回数据结构,在项目开发中必不可少。统一规范、统一约束

import com.test.enums.HttpStatus;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;
import java.util.Date;

/**
 * @全局API接口
 */
@Data
@ApiModel(description = "全局API接口")
public class ResponseModel<T> implements Serializable {

    @ApiModelProperty(value = "状态码,成功为2000", example = "2000")
    private int code;

    @ApiModelProperty(value = "成功标识", example = "true")
    private boolean success;

    @ApiModelProperty(value = "状态码为2000时,message为OK, 否则为错误信息", example = "OK")
    private String message;

    @ApiModelProperty(value = "返回数据")
    private T data;

    @ApiModelProperty(value = "时间戳", example = "1853558969352")
    private long timestamp;
}

Http状态码

package com.test.enums.enums;

/**
 * Http状态码
 */
public enum HttpStatus {
    JWT_EXPIRED(1001, "Expire JWT"),

    JWT_SIGNATURE_ERROR(1002, "Signature Error"),

    JWT_MALFORMED(1003, "Malformed JWT"),

    OK(2000, "OK"),

    USER_NOT_FOUND(3001, "Username Not Found"),

    BAD_CREDENTIALS(3002, "Bad Credentials"),

    USER_LOCKED(3003, "Account Has Been Disabled/Product Error"),

    USER_ALREADY_EXISTS(3004, "User Already Exists"),

    INCORRECT_ORIGINAL_PASSWORD(3005, "Incorrect Original Password"),

    FORBIDDEN(4001, "Forbidden"),

    UNAUTHORIZED(4002, "Unauthorized"),

    NOT_FOUND(4004, "Resources Not Fount"),

    UNKNOWN_ERROR(5000, "Unknown Error"),

    ARGS_VALIDATE_ERROR(5001, "Arguments Validate Error"),

    INTERNAL_ERROR(5002, "Internal Error"),

    SERVICE_ERROR(6000, "Service return error"),

    PRODUCT_TAG_FOUND(7001, "Product Not Found");
}

ResponseModel快捷构建类

package com.test.util;

import com.test.ResponseModel;
import com.test.enums.HttpStatus;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import org.springframework.http.MediaType;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.Writer;

/**
 * ResponseModel快捷构建类
 */
public final class Message {

    private Message() {
        throw new UnsupportedOperationException();
    }

    public static ResponseModel success() {
        return success("success");
    }

    public static <T> ResponseModel<T> success(T data) {
        return new ResponseModel(HttpStatus.OK, data);
    }

    public static <T> void success(T data, HttpServletResponse response) throws IOException {
        write(HttpStatus.OK, data, response);
    }

    public static <T> ResponseModel<T> error(String data) {
        return new ResponseModel(HttpStatus.SERVICE_ERROR, data);
    }

    public static <T> ResponseModel<T> error() {
        return error(HttpStatus.SERVICE_ERROR);
    }

    public static <T> ResponseModel<T> error(HttpStatus status) {
        return new ResponseModel(status, "error");
    }

    public static <T> ResponseModel<T> error(HttpStatus status, T data) {
        return new ResponseModel(status, data);
    }

    public static void error(HttpStatus status, HttpServletResponse response) throws IOException {
        write(status, "", response);
    }

    public static <T> void error(HttpStatus status, T data, HttpServletResponse response) throws IOException {
        write(status, data, response);
    }

    private static <T> void write(HttpStatus status, T data, HttpServletResponse response) throws IOException {
        ObjectMapper mapper = new ObjectMapper();
        response.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE);
        Writer writer = response.getWriter();
        ObjectWriter objectWriter = mapper.writer();
        objectWriter.writeValue(writer, new ResponseModel(status.code, status.message, data));
    }
}

你可能感兴趣的:(Java工具类)