• springboot项目编码规范


    1.统一返回消息体规范

    /**
         * 用户信息接口
         * @return
         */
        @ApiOperation(value = "用户信息接口", notes = "用户信息接口")
        @GetMapping(value = "/{id}")
        public Resp getUser(@PathVariable("id") Integer id) {
            Resp resp=Resp.success();
            resp.setData(tUserRepository.findTUserEntityById(id));
            return resp;
        }
    
    /**
         * 自定义异常message接口,默认失败code=1
         * @return
         */
        @ApiOperation(value = "自定义异常message接口", notes = "自定义异常message接口")
        @GetMapping(value = "/error3/{id}")
        public Resp updateUser3(@PathVariable("id") Integer id) {
            try{
                tUserRepository.findByRoleId(id);
            }catch (Exception e){
                log.error("维修订单终止失败..." + e.getMessage(), e);
                return Resp.failed("维修订单终止失败...");
            }
            return Resp.success();
    
        }
    

      

    package com.dousnl.utils.response;
    
    import org.springframework.validation.BindingResult;
    import org.springframework.validation.FieldError;
    
    public class Resp {
    	private RespStatus status;
    
    	private Object data;
    	
    	public Resp(){
    		
    	}
    	
    	public Resp(RespStatus status, Object data){
    		this.status = status;
    		this.data = data;
    	}
    	
    
    	public Resp(RespStatus status) {
    		super();
    		this.status = status;
    	}
    
    	public Object getData() {
    		return data;
    	}
    
    	public Resp setData(Object data) {
    		this.data = data;
    		return this;
    	}
    
    	public RespStatus getStatus() {
    		return status;
    	}
    
    	public Resp setStatus(RespStatus status) {
    		this.status = status;
    		return this;
    	}
    
    	public static Resp success() {
    		return new Resp().setStatus(RespStatus.SUCCESSFUL);
    	}
    	
    	//添加 成功并返回信息   wulingchi 2016-11-01
    	public static Resp success(String msg) {
    		final String c = String.valueOf(RespStatus.CODE_SUCCESSFUL);
    		final RespStatus s = new RespStatus(c, msg);
    		return new Resp().setStatus(s);
    	}
    	
    	public static Resp failed() {
    		return new Resp().setStatus(RespStatus.FAILED);
    	}
    
    	public static Resp failed(String msg) {
    		final String c = String.valueOf(RespStatus.CODE_FAILED);
    		final RespStatus s = new RespStatus(c, msg);
    		return new Resp().setStatus(s);
    	}
    	
    	public static Resp failed(BindingResult bindingResult){
    		final FieldError fieldError = bindingResult.getFieldError();
    		final String code = RespStatus.FAILED.getCode();
    		final String msg = fieldError.getDefaultMessage();
    		final RespStatus s = new RespStatus(code, msg);
    		return new Resp().setStatus(s);
    	}
    	
    	public static Resp failed(String code, String msg){
    		final RespStatus s = new RespStatus(code, msg);
    		return new Resp().setStatus(s);
    	}
    	
    	
    }
    

      

    package com.dousnl.utils.response;
    
    public class RespStatus {
    
    	public static final long CODE_SUCCESSFUL = 0L;
    
    	public static final String MSG_SUCCESSFUL = "ok";
    
    	public static final long CODE_FAILED = 1L;
    
    	public static final String MSG_FAILED = "failed";
    
    	public static final RespStatus SUCCESSFUL = new RespStatus(String.valueOf(CODE_SUCCESSFUL), MSG_SUCCESSFUL);
    
    	public static final RespStatus FAILED = new RespStatus(String.valueOf(CODE_FAILED), MSG_FAILED);
    
    	private String code;
    
    	private String msg;
    
    	public String getCode() {
    		return code;
    	}
    
    	public String getMsg() {
    		return msg;
    	}
    	
    	public RespStatus() {
    		super();
    	}
    
    	public RespStatus(String code) {
    		super();
    		this.code = code;
    	}
    
    	public RespStatus(String code, String msg) {
    		this.code = code;
    		this.msg = msg;
    	}
    
    	public static long getCodeSuccessful() {
    		return CODE_SUCCESSFUL;
    	}
    
    	public static String getMsgSuccessful() {
    		return MSG_SUCCESSFUL;
    	}
    
    	public static long getCodeFailed() {
    		return CODE_FAILED;
    	}
    
    	public static String getMsgFailed() {
    		return MSG_FAILED;
    	}
    
    	public static RespStatus getSUCCESSFUL() {
    		return SUCCESSFUL;
    	}
    
    	public static RespStatus getFAILED() {
    		return FAILED;
    	}
    
    	public void setCode(String code) {
    		this.code = code;
    	}
    
    	public void setMsg(String msg) {
    		this.msg = msg;
    	}
    }
    

      2.全局异常返回信息

    package com.dousnl.config.exception;
    
    import com.dousnl.utils.enums.ErrorEnums;
    import com.dousnl.utils.exception.MyException;
    import com.dousnl.utils.response.Resp;
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.http.HttpStatus;
    import org.springframework.web.bind.annotation.ExceptionHandler;
    import org.springframework.web.bind.annotation.ResponseStatus;
    import org.springframework.web.bind.annotation.RestControllerAdvice;
    
    /**
     * 通用异常返回信息
     *
     * @author hanliang
     * @version 1.0
     * @date 2019/11/4 14:22
     */
    @Slf4j
    @RestControllerAdvice
    public class ControllerExceptionHandler {
    
    
        @ExceptionHandler(value = MyException.class)
        public Resp myErrorHandler(MyException ex){
            log.error("异常信息:" + ex.getMessage(), ex);
            return Resp.failed(ex.getErrorCode(),ex.getMessage());
        }
        /**
         * 默认的异常处理
         * @param ex
         * @return
         */
        @ExceptionHandler(value = Exception.class)
        @ResponseStatus(HttpStatus.OK)
        public Resp exceptionHandler(Exception ex) {
            log.error("异常信息:" + ex.getMessage(), ex);
            return Resp.failed(ErrorEnums.SYSTEM_EXCEPTION.getCode(),ErrorEnums.SYSTEM_EXCEPTION.getMessage());
        }
    }
    

      

    package com.dousnl.utils.exception;
    
    import com.dousnl.utils.enums.ErrorEnums;
    
    /**
     * 自定义异常
     *
     * @author hanliang
     * @version 1.0
     * @date 2019/11/4 14:25
     */
    public class MyException extends RuntimeException{
    
        private String errorCode;
    
        private String message;
    
        public MyException() {
        }
    
        public MyException(ErrorEnums enums){
            this.errorCode=enums.getCode();
            this.message=enums.getMessage();
        }
    
        public String getErrorCode() {
            return errorCode;
        }
    
        public void setErrorCode(String errorCode) {
            this.errorCode = errorCode;
        }
    
        @Override
        public String getMessage() {
            return message;
        }
    
        public void setMessage(String message) {
            this.message = message;
        }
    }
    

      

    package com.dousnl.utils.enums;
    
    import lombok.AllArgsConstructor;
    import lombok.Getter;
    
    /**
     * 异常返回信息枚举
     *
     * @author 韩亮
     * @version $ID:ErrorEnums, v0.1 2018/3/30 韩亮 Exp $
     */
    @Getter
    @AllArgsConstructor
    public enum ErrorEnums {
    
        //权限异常
        SYSTEM_NOT_AUTHENTICATION_ERROR("401", "用户暂未认证"),
        SYSTEM_UNAUTHORIZED_ERROR("403", "用户无权限"),
        SYSTEM_TOKEN_VALIDATOR_ERROR("998", "TOKEN验证错误"),
        SYSTEM_SQL_INJECT_ERROR("999", "参数存在sql注入风险"),
        SYSTEM_ALREADY_LOGIN("error_login","您的账号已在其他终端登录,如不是您本人操作,请及时联系后台"),
    
        //==================通用错误码定义====================
        SUCCESS("000000", "交易成功"),
        SYSTEM_BEAN_COPY_ERROR("000001", "对象拷贝异常"),
        SYSTEM_STRING_TO_DATE_ERROR("000002", "字符串转日期失败"),
        SYSTEM_PARAM_VALIDATOR_ERROR("000003", "参数校验失败"),
    
        DB_ERROR("000010", "数据库查询异常"),
        DB_INSERT_ERROR("000011", "数据库插入异常"),
        DB_UPDATE_ERROR("000012", "数据库更新异常"),
        DB_SELECT_ERROR("000013", "数据库查询异常"),
    
        LOGIN_ERROR("000020", "登录失败"),
        LOGIN_USER_NOT_EXIST_ERROR("000021", "用户不存在"),
        LOGIN_AUTHENTICATION_FAILURE("000022", "身份验证失败"),
        LOGIN_AD_CONNECT_ERROR("000023", "AD域连接失败"),
        LOGIN_TOKEN_CREATE_ERROR("000024", "token生成失败"),
        LOGIN_USER_DISABLED("000025", "用户状态为无效状态"),
    
    	EXPORT_FOUR_BANK_ERROR("000030", "四大银行报表导出失败"),
    	NO_PERMISSION_TO_DOWNLOAD("000031","没有权限下载或文件不存在"),
        NO_QUALIFICATION_UPDATE_TO_PROJECTTYPE("000032","B级卡信息未完善,请完善B级卡信息"),
        NO_DATA_SHIRO("000034","没有资格更新项目银行类型"),
        EXPORT_Daily_Monitoring_ERROR("000035", "日常监控报表导出失败"),
    	RONG_CLOUD_REST_ERROR("000408", "融云服务端请求失败"),
    
    
        SYSTEM_EXCEPTION("5000", "系统异常!"),
        ;
    	
    	
        private final String code;
        private final String message;
    
    }
    

      

  • 相关阅读:
    安卓热修复
    Activity四种启动模式
    11、网页制作Dreamweaver(补充:JS零碎知识点&&正则表达式)
    6、C#基础整理(for 语句经典习题--for循环嵌套、穷举)
    5、C#基础整理(for 语句经典习题--与 if 的嵌套)
    4、C#基础整理(if语句经典习题)
    3、C#基础整理(语句概述)
    2、C#基础整理(运算符、数据类型与转换、var关键字)
    1、C#基础整理(进制转换的方法)
    10、网页制作Dreamweaver(扩展:各浏览器对 onunload 事件的支持与触发条件实现有差异)
  • 原文地址:https://www.cnblogs.com/dousnl/p/11793724.html
Copyright © 2020-2023  润新知