创建返回的枚举类:
import lombok.*;
@Getter
@ToString
@AllArgsConstructor
public enum RespBeanEnum {
SUCCESS(200,"success"),
ERROR(500,"服务端异常"),
SESSION_ERROR(500210,"session不存在或者已经失效"),
LOGINVO_ERROR(500211,"用户名或者密码错误"),
MOBILE_ERROR(500212,"手机号码格式错误");
private final Integer code;
private final String message;
}
创建返回结果类
@Data
@NoArgsConstructor
@AllArgsConstructor
public class RespBean {
private long code;
private String message;
private Object obj;
public static RespBean success(){
return new RespBean(RespBeanEnum.SUCCESS.getCode(),
RespBeanEnum.SUCCESS.getMessage(),null);
}
public static RespBean success(Object obj){
return new RespBean(RespBeanEnum.SUCCESS.getCode()
,RespBeanEnum.SUCCESS.getMessage(),obj);
}
public static RespBean error(RespBeanEnum respBeanEnum){
return new RespBean(respBeanEnum.getCode(),respBeanEnum.getMessage(),null);
}
}
测试
@RequestMapping("test")
@ResponseBody
public RespBean test(){
return RespBean.error(RespBeanEnum.MOBILE_ERROR);
}