springboot接口统一规范封装

笔记参考:https://www.cnblogs.com/kaibindirver/p/16358813.html

目录:

 

 

 

 Constants     ---常量

 1 package com.全局捕获.common;
 2 
 3 public interface Constants {
 4 
 5     String CODE_200 = "200"; //成功
 6     String CODE_401 = "401";  // 权限不足
 7     String CODE_400 = "400";  // 参数错误
 8     String CODE_500 = "500"; // 系统错误
 9     String CODE_600 = "600"; // 其他业务异常
10 
11     String DICT_TYPE_ICON = "icon";
12 
13     String FILES_KEY = "FILES_FRONT_ALL";
14 
15 }

Result    ----扔出前端响应的模版

 1 package com.common;
 2 
 3 import lombok.AllArgsConstructor;
 4 import lombok.Data;
 5 import lombok.NoArgsConstructor;
 6 
 7 /**
 8  * @author: Amim
 9  * @year: 2022/9
10  **/
11 @Data
12 @NoArgsConstructor
13 @AllArgsConstructor
14 public class Result {
15 
16     private String code;
17     private String msg;
18     private Object data;
19 
20     public static Result success() {
21         return new Result(Constants.CODE_200, "", null);
22     }
23 
24     public static Result success(Object data) {
25         return new Result(Constants.CODE_200, "测试成功", data);
26     }
27 
28     public static Result error(String code, String msg) {
29         return new Result(code, msg, null);
30     }
31 
32     public static Result error(String s) {
33         return new Result(Constants.CODE_500, "系统错误", null);
34     }
35 
36 }

UserDTO

 1 package com.Control.dto;
 2 
 3 import lombok.Data;
 4 
 5 /**
 6  * @author: Amim
 7  * @year: 2022/9
 8  **/
 9 
10 @Data
11 public class UserDTO {
12     private String username;
13 }

CatFood

 1 package com.Control;
 2 
 3 import com.Service.CatFoodService;
 4 import com.common.Constants;
 5 import com.common.Result;
 6 import org.springframework.beans.factory.annotation.Autowired;
 7 import org.springframework.web.bind.annotation.GetMapping;
 8 import org.springframework.web.bind.annotation.PostMapping;
 9 import org.springframework.web.bind.annotation.RequestMapping;
10 import org.springframework.web.bind.annotation.RestController;
11 
12 /**
13  * @author: Amim
14  * @year: 2022/9
15  **/
16 
17 @RestController
18 @RequestMapping("/food")
19 public class CatFood {
20 
21     @Autowired
22     CatFoodService catFoodService;
23 
24     //给猫粮记录的接口
25     @PostMapping("/addfoodtime")
26     public Object GiveFood() {
27         try {
28             String ff = catFoodService.MarkFood();
29             //  return catFoodService.MarkFood();
30             return Result.success(ff);
31         } catch (Exception e) {
32             System.out.println("看这里" + e);
33             return Result.error(Constants.CODE_401, e.getClass().toString());
34         }
35     }
36 
37     //记录水的接口
38     @PostMapping("/addwatertime")
39     public Result GiveWater() {
40         try {
41             String dd = catFoodService.MarkWater();
42             // return  catFoodService.MarkWater();
43             return Result.success(dd);
44         } catch (Exception e) {
45             System.out.println("看这里" + e);
46             return Result.error(Constants.CODE_401, e.getClass().toString());
47         }
48     }
49 //获取给水的接口
50     @GetMapping("/getwatertime")
51     public Result GetWater(){
52         try {
53             String tt = catFoodService.GetWaterList();
54             //  return  catFoodService.GetWaterList();
55             return Result.success(tt);
56         } catch (Exception e) {
57             System.out.println("看这里" + e);
58             return Result.error(Constants.CODE_401, e.getClass().toString());
59         }
60     }
61 }

GlobalExceptionHandler     -----如果有逻辑调用 ServiceException 方法,那么这边会接收他对应的参数且去执行handle方

 1 package com.exception;
 2 
 3 import com.common.Result;
 4 import org.springframework.web.bind.annotation.ControllerAdvice;
 5 import org.springframework.web.bind.annotation.ExceptionHandler;
 6 import org.springframework.web.bind.annotation.ResponseBody;
 7 
 8 /**
 9  * @author: Amim
10  * @year: 2022/9
11  **/
12 @ControllerAdvice
13 public class GlobalExceptionHandler {
14 
15     /**
16      * 如果抛出的的是ServiceException,则调用该方法
17      * @param se 业务异常
18      * @return Result
19      */
20     @ExceptionHandler(ServiceException.class)
21     @ResponseBody
22     public Result handle(ServiceException se){
23         return Result.error(se.getCode(), se.getMessage());
24     }
25 
26 }

ServiceException

 1 package com.exception;
 2 
 3 import lombok.Getter;
 4 
 5 /**
 6  * @author: Amim
 7  * @year: 2022/9
 8  **/
 9 @Getter
10 public class ServiceException extends RuntimeException {
11     private String code;
12 
13     public ServiceException(String code, String msg) {
14         super(msg);
15         this.code = code;
16     }
17 
18 }

 

posted @ 2022-10-09 01:22  amim  阅读(392)  评论(0编辑  收藏  举报