项目开发经验规范总结-时刻更新

2024-08-22 20:48

本文主要是介绍项目开发经验规范总结-时刻更新,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1、常用规范类

1.1、异常相关

1.1.1、业务异常类
package com.healerjean.proj.exception;import com.healerjean.proj.enums.ResponseEnum;/*** 系统业务异常*/
public class BusinessException extends RuntimeException {private int code;public BusinessException(int code) {this.code = code;}public BusinessException(String message) {super(message);this.code = ResponseEnum.逻辑错误.code;}public BusinessException(int code, String message) {super(message);this.code = code;}public BusinessException(ResponseEnum responseEnum) {super(responseEnum.msg);this.code = responseEnum.code ;}public BusinessException(ResponseEnum responseEnum,String message) {super(message);this.code = responseEnum.code ;}public BusinessException(String message, Throwable cause) {super(message, cause);this.code = ResponseEnum.逻辑错误.code;}public BusinessException(int code ,Throwable e) {super(e);this.code = code;}public BusinessException(ResponseEnum responseEnum, Throwable t) {super(responseEnum.msg, t);this.code = responseEnum.code;}public void setCode(int code) {this.code = code;}public int getCode() {return code;}}
1.1.2、参数异常类
package com.healerjean.proj.exception;import com.healerjean.proj.enums.ResponseEnum;/*** @author HealerJean* @ClassName ParameterErrorException* @date 2019/10/17  16:19.* @Description 参数错误*/
public class ParameterErrorException extends com.healerjean.proj.exception.BusinessException {public ParameterErrorException() {super(ResponseEnum.参数错误);}public ParameterErrorException(ResponseEnum responseEnum) {super(ResponseEnum.参数错误, responseEnum.msg);}public ParameterErrorException(String msg) {super(ResponseEnum.参数错误, msg);}}
1.1.3、接口异常类
package com.healerjean.proj.exception;import com.healerjean.proj.enums.ResponseEnum;/*** @author HealerJean* @ClassName HaoDanKuApiException* @date 2019/10/15  20:08.* @Description*/
public class HaoDanKuApiException extends BusinessException {public HaoDanKuApiException( ) {super(ResponseEnum.好单库口请求异常);}public HaoDanKuApiException(String msg) {super(ResponseEnum.好单库接口数据异常, msg);}public HaoDanKuApiException(Throwable e) {super(ResponseEnum.好单库口请求异常, e);}}
1.1.4、异常全局处理
package com.healerjean.proj.config;import com.healerjean.proj.dto.ResponseBean;
import com.healerjean.proj.enums.ResponseEnum;
import com.healerjean.proj.exception.BusinessException;
import com.healerjean.proj.exception.ParameterErrorException;
import com.healerjean.proj.utils.JsonUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageConversionException;
import org.springframework.validation.BindException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;import javax.servlet.http.HttpServletResponse;
import javax.validation.UnexpectedTypeException;
import java.util.HashMap;
import java.util.Map;/*** @author HealerJean* @version 1.0v* @ClassName ControllerHandleExceptionConfig* @date 2019/5/31  20:19.* @Description*/
@Slf4j
@ControllerAdvice
public class ControllerHandleConfig {/*** 不支持的请求方始*/@ExceptionHandler(value = HttpRequestMethodNotSupportedException.class)@ResponseStatus(value = HttpStatus.METHOD_NOT_ALLOWED)public ResponseBean methodNotSupportExceptionHandler(HttpRequestMethodNotSupportedException e) {log.error("不支持的请求方式", e);return ResponseBean.buildFailure(ResponseEnum.不支持的请求方式.code, e.getMessage());}/*** 参数类型错误* 1、(BindException : 比如 Integer 传入String  )* Field error in object 'demoDTO' on field 'age': rejected value [fasdf]; codes [typeMismatch.demoDTO.age,typeMismatch.age,typeMismatch.java.lang.Integer,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [demoDTO.age,age]; arguments []; default message [age]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.lang.Integer' for property 'age'; nested exception is java.lang.NumberFormatException: For input string: "fasdf"]*/@ExceptionHandler(value = {BindException.class})@ResponseStatus(HttpStatus.BAD_REQUEST)@ResponseBodypublic ResponseBean bindExceptionHandler(BindException e) {log.error("====参数类型错误===", e);return ResponseBean.buildFailure(ResponseEnum.参数类型错误.code, e.getMessage());}/*** 参数格式问题*/@ExceptionHandler(value = {MethodArgumentTypeMismatchException.class, HttpMessageConversionException.class, UnexpectedTypeException.class})@ResponseStatus(HttpStatus.BAD_REQUEST)@ResponseBodypublic ResponseBean httpMessageConversionExceptionHandler(Exception e) {log.error("====参数格式异常===", e);return ResponseBean.buildFailure(ResponseEnum.参数格式异常.code, e.getMessage());}/*** 参数错误*/@ExceptionHandler(value = ParameterErrorException.class)@ResponseStatus(HttpStatus.BAD_REQUEST)@ResponseBodypublic ResponseBean parameterErrorExceptionHandler(ParameterErrorException e) {log.error("参数异常------------参数错误:code:{},message:{}", e.getCode(), e.getMessage());return ResponseBean.buildFailure(e.getCode(), e.getMessage());}/*** 业务异常,给前台返回异常数据*/@ExceptionHandler(value = BusinessException.class)@ResponseStatus(HttpStatus.BAD_REQUEST)@ResponseBodypublic ResponseBean businessExceptionHandler(BusinessException e) {log.error("业务异常------------异常信息:code:{},message{}" ,e.getCode(), e.getMessage());return ResponseBean.buildFailure(e.getCode(),e.getMessage());}/*** 所有异常报错*/@ExceptionHandler@ResponseBodypublic HttpEntity<ResponseBean> allExceptionHandler(HttpServletResponse response, Exception e) {log.error("====系统错误===", e);response.setStatus(ResponseEnum.系统错误.code);return returnMessage(ResponseBean.buildFailure(ResponseEnum.系统错误));}private HttpEntity<ResponseBean> returnMessage(ResponseBean responseBean) {HttpHeaders header = new HttpHeaders();header.add("Content-Type", "application/json");header.add("Charset", "UTF-8");return new HttpEntity<>(responseBean, header);}/*** 参数非法* 1、(BindException : 比如 Integer 传入abc  )*/// @ExceptionHandler(value = {MethodArgumentTypeMismatchException.class, HttpRequestMethodNotSupportedException.class, HttpMessageConversionException.class, BindException.class, UnexpectedTypeException.class})// @ResponseBody// public HttpEntity<ResponseBean> httpMessageConversionExceptionHandler(HttpServletResponse response, Exception e) {//     log.error("====参数格式异常===", e);//     // 等同于 @ResponseStatus(HttpStatus.BAD_REQUEST)//     // 但是setStatus 不能比随便设置,最好一般情况下不要和HttpStatus 有重复的,这样有可能会造成没有输出Response body//     response.setStatus(ResponseEnum.参数格式异常.code);//     return returnMessage(ResponseBean.buildFailure(ResponseEnum.参数格式异常));// }// @ExceptionHandler(value ={HttpMessageConversionException.class, BindException.class} )// @ResponseBody// public HttpEntity<ResponseBean> httpMessageConversionExceptionHandler(Exception e) {//     log.error("====参数格式异常===", e);//     return new ResponseEntity<>(ResponseBean.buildFailure(ResponseEnum.参数格式异常),HttpStatus.BAD_REQUEST);// }}

这篇关于项目开发经验规范总结-时刻更新的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1097359

相关文章

深度解析Java项目中包和包之间的联系

《深度解析Java项目中包和包之间的联系》文章浏览阅读850次,点赞13次,收藏8次。本文详细介绍了Java分层架构中的几个关键包:DTO、Controller、Service和Mapper。_jav... 目录前言一、各大包1.DTO1.1、DTO的核心用途1.2. DTO与实体类(Entity)的区别1

JavaSE正则表达式用法总结大全

《JavaSE正则表达式用法总结大全》正则表达式就是由一些特定的字符组成,代表的是一个规则,:本文主要介绍JavaSE正则表达式用法的相关资料,文中通过代码介绍的非常详细,需要的朋友可以参考下... 目录常用的正则表达式匹配符正则表China编程达式常用的类Pattern类Matcher类PatternSynta

如何在Spring Boot项目中集成MQTT协议

《如何在SpringBoot项目中集成MQTT协议》本文介绍在SpringBoot中集成MQTT的步骤,包括安装Broker、添加EclipsePaho依赖、配置连接参数、实现消息发布订阅、测试接口... 目录1. 准备工作2. 引入依赖3. 配置MQTT连接4. 创建MQTT配置类5. 实现消息发布与订阅

MySQL追踪数据库表更新操作来源的全面指南

《MySQL追踪数据库表更新操作来源的全面指南》本文将以一个具体问题为例,如何监测哪个IP来源对数据库表statistics_test进行了UPDATE操作,文内探讨了多种方法,并提供了详细的代码... 目录引言1. 为什么需要监控数据库更新操作2. 方法1:启用数据库审计日志(1)mysql/mariad

springboot项目打jar制作成镜像并指定配置文件位置方式

《springboot项目打jar制作成镜像并指定配置文件位置方式》:本文主要介绍springboot项目打jar制作成镜像并指定配置文件位置方式,具有很好的参考价值,希望对大家有所帮助,如有错误... 目录一、上传jar到服务器二、编写dockerfile三、新建对应配置文件所存放的数据卷目录四、将配置文

SpringBoot开发中十大常见陷阱深度解析与避坑指南

《SpringBoot开发中十大常见陷阱深度解析与避坑指南》在SpringBoot的开发过程中,即使是经验丰富的开发者也难免会遇到各种棘手的问题,本文将针对SpringBoot开发中十大常见的“坑... 目录引言一、配置总出错?是不是同时用了.properties和.yml?二、换个位置配置就失效?搞清楚加

怎么用idea创建一个SpringBoot项目

《怎么用idea创建一个SpringBoot项目》本文介绍了在IDEA中创建SpringBoot项目的步骤,包括环境准备(JDK1.8+、Maven3.2.5+)、使用SpringInitializr... 目录如何在idea中创建一个SpringBoot项目环境准备1.1打开IDEA,点击New新建一个项

Python中对FFmpeg封装开发库FFmpy详解

《Python中对FFmpeg封装开发库FFmpy详解》:本文主要介绍Python中对FFmpeg封装开发库FFmpy,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐... 目录一、FFmpy简介与安装1.1 FFmpy概述1.2 安装方法二、FFmpy核心类与方法2.1 FF

基于Python开发Windows屏幕控制工具

《基于Python开发Windows屏幕控制工具》在数字化办公时代,屏幕管理已成为提升工作效率和保护眼睛健康的重要环节,本文将分享一个基于Python和PySide6开发的Windows屏幕控制工具,... 目录概述功能亮点界面展示实现步骤详解1. 环境准备2. 亮度控制模块3. 息屏功能实现4. 息屏时间

springboot项目中整合高德地图的实践

《springboot项目中整合高德地图的实践》:本文主要介绍springboot项目中整合高德地图的实践,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一:高德开放平台的使用二:创建数据库(我是用的是mysql)三:Springboot所需的依赖(根据你的需求再