飞道的博客

亿级高并发电商项目-- 实战篇 --万达商城项目 四(Dashboard服务、设置统一返回格式与异常处理、Postman测试接口 )

433人阅读  评论(0)

 

 

  

专栏:高并发---前后端分布式项目 

👏作者简介:大家好,我是小童,Java开发工程师,CSDN博客博主,Java领域新星创作者
📕系列专栏:前端、Java、Java中间件大全、微信小程序、微信支付、若依框架、Spring全家桶
📧如果文章知识点有错误的地方,请指正!和大家一起学习,一起进步👀
🔥如果感觉博主的文章还不错的话,请👍三连支持👍一下博主哦
🍂博主正在努力完成2023计划中:以梦为马,扬帆起航,2023追梦人 

 IDEA开启Dashboard

普通的IDEA面板只能管理一个服务,而分布式项目中,服务非常多,开启Dashboard可以更方便的管理服务。

在项目路径中的 .idea/workspace.xml 中添加


  
  1. <component name = "RunDashboard" >
  2. <option name = "ruleStates" >
  3. <list >
  4. <RuleState >
  5. <option name = "name" value = "ConfigurationTypeDashboardGroupingRule" / >
  6. < /RuleState >
  7. <RuleState >
  8. <option name = "name" value = "StatusDashboardGroupingRule" / >
  9. < /RuleState >
  10. < /list >
  11. < /option >
  12. <option name = "configurationTypes" >
  13. < set >
  14. <option value = "SpringBootApplicationConfigurationType" / >
  15. < / set >
  16. < /option >
  17. < /component >

效果如下: 

 

设置统一返回格式 

在前后端分离的项目中,为了方便前后端交互,后端往往需要给前端返回固定的数据格式,但不同的实体类返回格式不同,所以在真实开发中,我们将所有API接口设置返回统一的格式。

 

1、在通用模块创建统一返回结果实体类 


  
  1. / **
  2. * 统一结果集返回结果
  3. * /
  4. @ Data
  5. @AllArgsConstructor
  6. public class BaseResult <T > {
  7. / / 状态码(成功: 200 失败:其他)
  8. private Integer code;
  9. / / 提示消息
  10. private String message;
  11. / / 返回数据
  12. private T data;
  13. / / 构建成功结果
  14. public static <T > BaseResult <T > ok() {
  15. return new BaseResult(CodeEnum.SUCCESS.getCode(),CodeEnum.SUCCESS.getMessage(), null);
  16. }
  17. / / 构建带有数据的成功结果
  18. public static <T > BaseResult <T > ok(T data) {
  19. return new BaseResult(CodeEnum.SUCCESS.getCode(),CodeEnum.SUCCESS.getMessage(), data);
  20. }
  21. }
  22. / **
  23. * 返回状态码枚举类
  24. * /
  25. @Getter
  26. @AllArgsConstructor
  27. public enum CodeEnum {
  28. / / 正常
  29. SUCCESS( 200, "OK");
  30. private final Integer code;
  31. private final String message;
  32. }

2、修改API模块的控制器方法,所有方法都返回 BaseResult 对象


  
  1. / **
  2. * 品牌
  3. * /
  4. @RestController
  5. @RequestMapping( "/brand")
  6. public class BrandController {
  7. / / 远程注入
  8. @ Reference
  9. private BrandService brandService;
  10. / **
  11. * 根据id查询品牌
  12. *
  13. * @param id 品牌id
  14. * @return 查询结果
  15. */
  16. @GetMapping( "/findById")
  17. public BaseResult <Brand > findById(Long id) {
  18. Brand brand = brandService.findById(id);
  19. return BaseResult.ok(brand);
  20. }
  21. }

 

 统一异常处理

在前后端分离项目中,系统抛出异常时,不论是自定义异常还是程序异常,都要返回给前端一段JSON数据,以便其对用户进行提示, 且JSON数据的格式和正常结果相同。

 

接下来我们在 通用模块 为整个项目做统一异常处理: 

1、创建自定义异常类 


  
  1. / **
  2. * 自定义业务异常
  3. * /
  4. @ Data
  5. @AllArgsConstructor
  6. @NoArgsConstructor
  7. public class BusException extends
  8. RuntimeException implements Serializable {
  9. / / 状态码(成功: 200,失败:其他)
  10. private Integer code;
  11. / / 异常信息
  12. private String message;
  13. public BusException(CodeEnum codeEnum)
  14. {
  15. this. code = codeEnum.getCode();
  16. this.message = codeEnum.getMessage();
  17. }
  18. }

2、创建统一异常处理器


  
  1. / / 统一异常处理器
  2. @RestControllerAdvice
  3. public class GlobalExceptionHandler {
  4. / / 处理业务异常
  5. @ExceptionHandler(BusException. class)
  6. public BaseResult defaultExceptionHandler(HttpServletRequest req, HttpServletResponse resp,BusException e) {
  7. BaseResult baseResult = new BaseResult(e.getCode(),e.getMessage(), null);
  8. return baseResult;
  9. }
  10. / / 处理系统异常
  11. @ExceptionHandler( Exception. class)
  12. public BaseResult defaultExceptionHandler(HttpServletRequest req, HttpServletResponse resp, Exception e) {
  13. e.printStackTrace();
  14. BaseResult baseResult = new BaseResult(CodeEnum.SYSTEM_ ERROR.getCode(),CodeEnum.SYSTEM_ ERROR.getMessage(), null);
  15. return baseResult;
  16. }
  17. }

3 让所有项目都可以加载全局异常处理类,在通用模块创建文件

resources > META-INF > spring.factories ,添加如下内容:


  
  1. # 启动时自动扫描全局异常处理类
  2. org.springframework.boot.autoconfigure.Ena
  3. bleAutoConfiguration =com.itbaizhan.shoppin
  4. g_ common. exception.GlobalExceptionHandler

 4、修改返回状态码枚举类


  
  1. / **
  2. * 返回状态码枚举类
  3. * /
  4. @Getter
  5. @AllArgsConstructor
  6. public enum CodeEnum {
  7. / / 正常
  8. SUCCESS( 200, "OK"),
  9. / / 系统异常
  10. SYSTEM_ ERROR( 500, "系统异常"),
  11. / / 业务异常
  12. PARAMETER_ ERROR( 601, "参数异常");
  13. private final Integer code;
  14. private final String message;
  15. }

5、修改品牌服务代码


  
  1. @Service
  2. public class BrandServiceImpl implements BrandService {
  3. @Autowired
  4. private BrandMapper brandMapper;
  5. / **
  6. * 根据id查询品牌
  7. */
  8. public Brand findById(Long id){
  9. if (id = = 0){
  10. int i = 1 / 0; / / 模拟系统异常
  11. } else if (id = = - 1){
  12. throw new BusException(CodeEnum.PARAMETER_ ERROR); / / 模拟业务异常
  13. }
  14. return brandMapper.selectById(id);
  15. }
  16. }

6、分别访问


  
  1. http: / /localhost: 8001 /brand /findById?id = 1
  2. http: / /l ocalhost: 8001 /brand /findById?id = 0
  3. http: / /localhost: 8001 /b rand /findById?id =- 1

查看返回结果

Postman测试接口 

1、双击安装包安装Postman

2、创建请求集合

3、添加请求

4、保存请求到集合,以后可以随时发送该请求 

 

 


转载:https://blog.csdn.net/m0_58719994/article/details/129017237
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场