{"id":18431075,"url":"https://github.com/purgeteam/unified-dispose-springboot","last_synced_at":"2025-04-07T18:32:23.867Z","repository":{"id":57726959,"uuid":"207714166","full_name":"purgeteam/unified-dispose-springboot","owner":"purgeteam","description":"springboot全局处理(异常+统一)","archived":false,"fork":false,"pushed_at":"2022-06-17T02:28:45.000Z","size":69,"stargazers_count":103,"open_issues_count":1,"forks_count":45,"subscribers_count":8,"default_branch":"master","last_synced_at":"2023-11-07T15:25:45.598Z","etag":null,"topics":["springboot2"],"latest_commit_sha":null,"homepage":null,"language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/purgeteam.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-09-11T03:22:36.000Z","updated_at":"2023-09-05T13:26:43.000Z","dependencies_parsed_at":"2022-09-11T17:03:05.476Z","dependency_job_id":null,"html_url":"https://github.com/purgeteam/unified-dispose-springboot","commit_stats":null,"previous_names":[],"tags_count":3,"template":null,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/purgeteam%2Funified-dispose-springboot","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/purgeteam%2Funified-dispose-springboot/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/purgeteam%2Funified-dispose-springboot/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/purgeteam%2Funified-dispose-springboot/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/purgeteam","download_url":"https://codeload.github.com/purgeteam/unified-dispose-springboot/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223288252,"owners_count":17120352,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["springboot2"],"created_at":"2024-11-06T05:23:25.297Z","updated_at":"2024-11-06T05:23:26.105Z","avatar_url":"https://github.com/purgeteam.png","language":"Java","funding_links":[],"categories":["REST错误处理"],"sub_categories":[],"readme":"# unified-dispose-springboot-starter\n\n[![Maven Central](https://img.shields.io/maven-central/v/com.purgeteam/unified-dispose-springboot-starter.svg?label=Maven%20Central)](https://search.maven.org/search?q=g:com.purgeteam%20AND%20a:unified-dispose-springboot-starter)\n![License](https://img.shields.io/badge/SpringBoot-2.1.8RELEASE-green.svg)\n![License](https://img.shields.io/badge/JAVA-1.8+-green.svg)\n![License](https://img.shields.io/badge/maven-3.0+-green.svg)\n[![License](https://img.shields.io/badge/license-Apache%202-4EB1BA.svg)](https://www.apache.org/licenses/LICENSE-2.0.html)\n\n\n🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀\n\n🔥🔥🔥相关文档请访问 [PurgeTeam docs](http://www.purgeteam.com)🔥🔥🔥\n\n新版项目已经迁移为：请访问使用 [spirng-cloud-purgeteam](https://github.com/purgeteam/spirng-cloud-purgeteam)\n\n🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀\n\n\n\n## 简介\n\n重复功能我来写。在 `SpringBoot` 项目里都有全局异常处理以及返回包装等，返回前端是带上`succ`、`code`、`msg`、`data`等字段。单个项目情况下很好解决，当微服务模块多的情况下，很多情况开发都是复制原有代码进行构建另外一个项目的，导致这些功能升级需要修改多个服务，在这个基础上，我们封装了一个组件 `unified-dispose-springboot-starter` 里面包含了一些基础的异常处理以及返回包装功能。\n\n## 依赖添加启动功能\n\n添加依赖\n**ps:** 实际version版本请使用最新版\n**最新版本:** [![Maven Central](https://img.shields.io/maven-central/v/com.purgeteam/unified-dispose-springboot-starter.svg?label=Maven%20Central)](https://search.maven.org/search?q=g:com.purgeteam%20AND%20a:unified-dispose-springboot-starter)\n\n[点击查看最新新版本](https://search.maven.org/search?q=g:com.purgeteam%20AND%20a:unified-dispose-springboot-starter)\n\n```\n\u003cdependency\u003e\n  \u003cgroupId\u003ecom.purgeteam\u003c/groupId\u003e\n  \u003cartifactId\u003eunified-dispose-springboot-starter\u003c/artifactId\u003e\n  \u003cversion\u003e0.1.1.RELEASE\u003c/version\u003e\n\u003c/dependency\u003e\n```\n\n启动类添加 `@EnableGlobalDispose` 注解开启以下功能。\n```\n@EnableGlobalDispose\n@SpringBootApplication\npublic class GlobalDisposeSpringBootApplication {\n\n  public static void main(String[] args) {\n    SpringApplication.run(GlobalDisposeSpringBootApplication.class, args);\n  }\n\n}\n```\n\n## One 异常处理⚠️\n\n在项目中经常出现系统异常的情况，比如`NullPointerException`等等。如果默认未处理的情况下，`springboot`会响应默认的错误提示，这样对用户体验不是友好，系统层面的错误，用户不能感知到，即使为`500`的错误，可以给用户提示一个类似`服务器开小差`的友好提示等。\n\n模块里以及包含了一些基本的异常处理方式(及不需要做任何代码编写已经具有基本异常处理)，以及一些常见的异常code，用户只需要关心业务异常处理即可，直接通过 `throw new 异常` 的方式抛出即可。\n\n### 异常处理包含类型\n\n```\n# 通用500异常\nException 类捕获 500 异常处理\n\n# Feign 异常\nFeignException 类捕获\nClientException 类捕获\n\n# 业务自定义\nBusinessException 类捕获 业务通用自定义异常\n\n# 参数校验异常\nHttpMessageNotReadableException 参数错误异常\nBindException 参数错误异常\n```\n\n### 程序主动抛出异常\n\n```\nthrow new BusinessException(BusinessErrorCode.BUSINESS_ERROR);\n// 或者\nthrow new BusinessException(\"CLOUD800\",\"没有多余的库存\");\n```\n\n通常不建议直接抛出通用的BusinessException异常，应当在对应的模块里添加对应的领域的异常处理类以及对应的枚举错误类型。\n\n如会员模块：\n创建`UserException`异常类、`UserErrorCode`枚举。\n\nUserException:\n\n继承 `BusinessException` \n\n```java\n/**\n * {@link RuntimeException} user 业务异常\n *\n * @author purgeyao\n * @since 1.0\n */\n@Getter\npublic class UserException extends BusinessException {\n\n  private String code;\n  private boolean isShowMsg = true;\n\n  /**\n   * 使用枚举传参\n   *\n   * @param errorCode 异常枚举\n   */\n  public UserException(UserErrorCode errorCode) {\n    super(errorCode.getCode(), errorCode.getMessage());\n    this.code = errorCode.getCode();\n  }\n\n}\n```\n\nUserErrorCode:\n\n```java\n@Getter\npublic enum UserErrorCode {\n    /**\n     * 权限异常\n     */\n    NOT_PERMISSIONS(\"CLOUD401\",\"您没有操作权限\"),\n    ;\n\n    private String code;\n\n    private String message;\n\n    UserErrorCode(String code, String message) {\n        this.code = code;\n        this.message = message;\n    }\n}\n```\n\n最后业务使用如下：\n\n```java\n// 判断是否有权限抛出异常\nthrow new UserException(UserErrorCode.NOT_PERMISSIONS);\n```\n\n**上述方式，抛出异常后会被模块处理。前台返回如下**：\n\n```\n{\n  \"succ\": false,        // 是否成功\n  \"ts\": 1566467628851,  // 时间戳\n  \"data\": null,         // 数据\n  \"code\": \"CLOUD800\",   // 错误类型\n  \"msg\": \"业务异常\"      // 错误描述\n}\n```\n\n## Tow 统一返回封装🗳\n\n在REST风格的开发中，避免通常会告知前台返回是否成功以及状态码等信息。这里我们通常返回的时候做一次`util`的包装处理工作，如：`Result`类似的类，里面包含`succ`、`code`、`msg`、`data`等字段。\n\n\n接口调用处理类似如下:\n\n```\n  @GetMapping(\"hello\")\n  public Result list(){\n    return Result.ofSuccess(\"hello\");\n  }\n```\n结果:\n```\n{\n  \"succ\": ture,         // 是否成功\n  \"ts\": 1566467628851,  // 时间戳\n  \"data\": \"hello\",      // 数据\n  \"code\": null,         // 错误类型\n  \"msg\": null           // 错误描述\n}\n```\n### 功能使用\n\n默认情况所有的 `web controller` 都会被封装为以下返回格式。\n\n接口：\n\n```java\n@GetMapping(\"test\")\npublic String test(){\n  return \"test\";\n}\n```\n\n返回\n\n```json\n{\n  \"succ\": true,             // 是否成功\n  \"ts\": 1566386951005,      // 时间戳\n  \"data\": \"test\",           // 数据\n  \"code\": null,             // 错误类型\n  \"msg\": null               // 错误描述         \n}\n```\n\n### 忽略封装注解:@IgnoreResponseAdvice\n\n`@IgnoreResponseAdvice`允许范围为：**类 + 方法**，标识在类上这个类下的所有方法的返回都将忽略返回封装。\n\n接口：\n\n```java\n@IgnoreResponseAdvice // 忽略数据包装 可添加到类、方法上\n@GetMapping(\"test\")\npublic String test(){\n  return \"test\";\n}\n```\n\n返回 `test`\n\n\n## 总结\n\n项目里很多重复的code，我们可以通过一定的方式去简化，以达到一定目的减少开发量。PurgeTeam 具有一些优秀的开源组件，减少日常的开发量。\n\n\u003e 示例代码地址:[unified-dispose-springboot](https://github.com/purgeteam/unified-dispose-springboot)\n\n\u003e 作者GitHub:\n[Purgeyao](https://github.com/purgeyao) 欢迎关注\n\n\u003e qq交流群: `812321371` 微信交流群: `MercyYao`\n\n\u003e 微信公众号:\n\n![微信公众号二维码](https://purgeyao.github.io/img/about-my-mp-8cm.jpg)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpurgeteam%2Funified-dispose-springboot","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpurgeteam%2Funified-dispose-springboot","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpurgeteam%2Funified-dispose-springboot/lists"}