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

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

相关文章

Spring 依赖注入与循环依赖总结

《Spring依赖注入与循环依赖总结》这篇文章给大家介绍Spring依赖注入与循环依赖总结篇,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录1. Spring 三级缓存解决循环依赖1. 创建UserService原始对象2. 将原始对象包装成工

Python实战之SEO优化自动化工具开发指南

《Python实战之SEO优化自动化工具开发指南》在数字化营销时代,搜索引擎优化(SEO)已成为网站获取流量的重要手段,本文将带您使用Python开发一套完整的SEO自动化工具,需要的可以了解下... 目录前言项目概述技术栈选择核心模块实现1. 关键词研究模块2. 网站技术seo检测模块3. 内容优化分析模

SpringBoot通过main方法启动web项目实践

《SpringBoot通过main方法启动web项目实践》SpringBoot通过SpringApplication.run()启动Web项目,自动推断应用类型,加载初始化器与监听器,配置Spring... 目录1. 启动入口:SpringApplication.run()2. SpringApplicat

基于Java开发一个极简版敏感词检测工具

《基于Java开发一个极简版敏感词检测工具》这篇文章主要为大家详细介绍了如何基于Java开发一个极简版敏感词检测工具,文中的示例代码简洁易懂,感兴趣的小伙伴可以跟随小编一起学习一下... 目录你是否还在为敏感词检测头疼一、极简版Java敏感词检测工具的3大核心优势1.1 优势1:DFA算法驱动,效率提升10

MySQL中查询和展示LONGBLOB类型数据的技巧总结

《MySQL中查询和展示LONGBLOB类型数据的技巧总结》在MySQL中LONGBLOB是一种二进制大对象(BLOB)数据类型,用于存储大量的二进制数据,:本文主要介绍MySQL中查询和展示LO... 目录前言1. 查询 LONGBLOB 数据的大小2. 查询并展示 LONGBLOB 数据2.1 转换为十

Springboot项目构建时各种依赖详细介绍与依赖关系说明详解

《Springboot项目构建时各种依赖详细介绍与依赖关系说明详解》SpringBoot通过spring-boot-dependencies统一依赖版本管理,spring-boot-starter-w... 目录一、spring-boot-dependencies1.简介2. 内容概览3.核心内容结构4.

MySQL 数据库表操作完全指南:创建、读取、更新与删除实战

《MySQL数据库表操作完全指南:创建、读取、更新与删除实战》本文系统讲解MySQL表的增删查改(CURD)操作,涵盖创建、更新、查询、删除及插入查询结果,也是贯穿各类项目开发全流程的基础数据交互原... 目录mysql系列前言一、Create(创建)并插入数据1.1 单行数据 + 全列插入1.2 多行数据

linux安装、更新、卸载anaconda实践

《linux安装、更新、卸载anaconda实践》Anaconda是基于conda的科学计算环境,集成1400+包及依赖,安装需下载脚本、接受协议、设置路径、配置环境变量,更新与卸载通过conda命令... 目录随意找一个目录下载安装脚本检查许可证协议,ENTER就可以安装完毕之后激活anaconda安装更

Python开发简易网络服务器的示例详解(新手入门)

《Python开发简易网络服务器的示例详解(新手入门)》网络服务器是互联网基础设施的核心组件,它本质上是一个持续运行的程序,负责监听特定端口,本文将使用Python开发一个简单的网络服务器,感兴趣的小... 目录网络服务器基础概念python内置服务器模块1. HTTP服务器模块2. Socket服务器模块

在ASP.NET项目中如何使用C#生成二维码

《在ASP.NET项目中如何使用C#生成二维码》二维码(QRCode)已广泛应用于网址分享,支付链接等场景,本文将以ASP.NET为示例,演示如何实现输入文本/URL,生成二维码,在线显示与下载的完整... 目录创建前端页面(Index.cshtml)后端二维码生成逻辑(Index.cshtml.cs)总结