1. 定义响应状态码枚举类 ResponseEnum
枚举类用来定义项目中要使用到的状态码
package com.project.enums;
/**
* @author 孟亚辉
* @time 2022/10/6 13:51
*/
public enum ResponseEnum {
SUCCESS(200, "操作成功"),
NEED_LOGIN(401, "需要登录后操作"),
NO_PERMISSION(403,"无操作权限"),
NO_INTERFACE(404,"接口不存在"),
SYSTEM_ERROR(500, "系统错误"),
USERNAME_EXIT(501, "用户名已存在"),
PHONE_EXIST(502, "手机号已存在"),
EMAIL_EXIST(503, "邮箱已存在"),
NICKNAME_EXIST(504, "该昵称已经存在"),
REQUIRE_USERNAME(505, "必需填写用户名"),
FILE_TYPE_ERROR(506, "请上传jpg|png格式的文件"),
PASSWORD_NOT_NULL(507, "密码不能为空"),
EMAIL_NOT_NULL(508, "邮箱不能为空"),
LOGIN_ERROR(509, "用户名或密码错误");
final int code;
final String msg;
ResponseEnum(int code, String message) {
this.code = code;
this.msg = message;
}
public int getCode() {
return code;
}
public String getMsg() {
return msg;
}
}
2. 定义响应实体类 ResponseResult
在这里引用 ResponseEnum 枚举,定义了响应消息和响应数据对象若干方法(也可以将上面的 ResponseEnum 枚举直接定义在该类内部,看个人喜好)
package com.project.domain;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.project.enums.ResponseEnum;
import lombok.Getter;
import lombok.Setter;
import java.io.Serializable;
/**
* @author 孟亚辉
* @time 2022/10/6 13:50
*/
@Getter
@Setter
@JsonInclude(JsonInclude.Include.NON_NULL)// 为 null 的字段不序列化
public class ResponseResult<T> implements Serializable {
private Integer code;
private String msg;
private T data;
public ResponseResult() {
}
public ResponseResult(Integer code, String msg) {
this.code = code;
this.msg = msg;
}
public static ResponseResult<?> error(ResponseEnum responseEnum) {
return setErrorResponse(responseEnum);
}
public static ResponseResult<?> error(Integer code, String msg) {
return new ResponseResult<>(code, msg);
}
public static ResponseResult<?> error() {
return setErrorResponse();
}
public static <T> ResponseResult<T> success(T data) {
return setSuccessResponse(data);
}
public static ResponseResult<?> success() {
return setSuccessResponse();
}
private static <T> ResponseResult<T> setSuccessResponse(T data) {
ResponseResult<T> result = new ResponseResult<>();
result.setCode(ResponseEnum.SUCCESS.getCode());
result.setMsg(ResponseEnum.SUCCESS.getMsg());
result.setData(data);
return result;
}
private static ResponseResult<?> setSuccessResponse() {
ResponseResult<?> result = new ResponseResult<>();
result.setCode(ResponseEnum.SUCCESS.getCode());
result.setMsg(ResponseEnum.SUCCESS.getMsg());
return result;
}
private static ResponseResult<?> setErrorResponse(ResponseEnum responseEnum) {
ResponseResult<?> result = new ResponseResult<>();
result.setCode(responseEnum.getCode());
result.setMsg(responseEnum.getMsg());
return result;
}
private static ResponseResult<?> setErrorResponse() {
ResponseResult<?> result = new ResponseResult<>();
result.setCode(ResponseEnum.SYSTEM_ERROR.getCode());
result.setMsg(ResponseEnum.SYSTEM_ERROR.getMsg());
return result;
}
}
3. 测试结果
在 controller 中使用响应实体类。
package com.project.controller;
/**
* @author 孟亚辉
* @time 2022/10/6 14:48
*/
import com.project.domain.ResponseResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;
@RestController
public class IndexController {
@GetMapping("/index")
public ResponseResult<?> index(){
Map<String, Object> map = new HashMap<>();
map.put("username","zs");
map.put("password","1234");
return ResponseResult.success(map);
}
@GetMapping("/success")
public ResponseResult<?> test(){
return ResponseResult.success();
}
@GetMapping("/error")
public ResponseResult<?> hello(){
return ResponseResult.error();
}
}