zoukankan      html  css  js  c++  java
  • 自定义JSON返回类型返回给前端数据

    public class RestErrorResponse {
        private Integer errorCode;
        private String message;
        private Object details;
        private JSONObject result;
    
    
        private RestErrorResponse() {
        }
    
        public static RestErrorResponse build() {
            return new RestErrorResponse();
        }
    
        public Integer getErrorCode() {
            return errorCode;
        }
    
        public RestErrorResponse setErrorCode(Integer errorCode) {
            this.errorCode = errorCode;
            return this;
        }
    
        public String getMessage() {
            return message;
        }
    
        public RestErrorResponse setMessage(String message) {
            this.message = message;
            return this;
        }
    
        public Object getDetails() {
            return details;
        }
    
        public RestErrorResponse setDetails(Object details) {
            this.details = details;
            return this;
        }
    
        public void setResult(JSONObject result) {
            this.result = result;
        }
    
        @Override
        public String toString() {
            return getResult();
        }
    
        public String getResult() {
            result = new JSONObject();
            JSONObject errorJson = new JSONObject();
            errorJson.put("code", errorCode);
            errorJson.put("message", message);
            errorJson.put("details", details);
            result.put("error", errorJson);
            return result.toString();
        }
    
    
    }
    public enum RestResponseEnum {
        /* */
        SUCCESS(200, "成功"),
        UNKNOWN_ERROR(-1, "未知错误"),
    
        EQUIPMENT_ERROR(511, "操作设备失败"),
    
        EXCEL_DOWNLOAD_ERROR(516, "EXCEL下载失败"),
        EXCEL_UPLOAD_ERROR(517, "EXCEL上传失败"),
        /* error result about login*/
        LOGIN_ERROR(501, "用户登录失败"),
        ADMIN_LOGIN_ERROR(502, "管理员登录失败"),
    
        /*  error result about user*/
        USER_ERROR(506, "操作用户失败");
    
        private Integer code;
        private String msg;
    
        RestResponseEnum(Integer code, String msg) {
            this.code = code;
            this.msg = msg;
        }
    
        public Integer getCode() {
            return code;
        }
    
        public String getMsg() {
            return msg;
        }
    
    }
    @RestController
    public class EquipmentController {
    
        @Autowired
        private EquipmentService equipmentService;
    
        @DeleteMapping("/v1/equipment")
        public Object deleteEquipment(HttpServletRequest request, HttpServletResponse response, @RequestParam("equipmentId") String equipmentId) {
            boolean isSuccess = equipmentService.delete(equipmentId);
            if (isSuccess) {
                response.setStatus(RestResponseEnum.SUCCESS.getCode());
                return equipmentId;
            }
            return RestErrorResponse.build().setErrorCode(RestResponseEnum.EQUIPMENT_ERROR.getCode()).setMessage(RestResponseEnum.EQUIPMENT_ERROR.getMsg()).getResult();
        }
    
  • 相关阅读:
    Djnago中缓存配置(redis配置案例)
    HDU-4717 The Moving Points 三分
    HDU-4716 A Computer Graphics Problem 水题
    HDU-2686 Matrix 多进程DP
    [转]手动开平方的简易方法
    [转]树链剖分
    HUOJ-10857 最大的面积 凸包+DP
    Bnuoj-29359 Deal with numbers 线段树
    HDU-4283 You Are the One 区间DP
    BNUOJ-26586 Simon the Spider 最小生成树+枚举
  • 原文地址:https://www.cnblogs.com/zhanzhuang/p/11989423.html
Copyright © 2011-2022 走看看