統一返回物件封裝和統一異常捕獲封裝springboot starter

2023-01-17 18:00:21

好久沒有更新文章了,高齡開發沒什麼技術,去了外包公司後沒怎麼更新文章了。今天分享下統一處理starter,相信開發web系統的時候都是會涉及到前後端的互動,而後端返回資料的時候一般都會統一封裝一個返回物件和統一處理異常,一般情況下都是在controller的每個方法中呼叫封裝的物件,把相應的資料塞到data欄位,然後返回給前端。而例外處理則是丟擲某個業務異常,然後利用spring切面進行攔截處理。每個專案都需要做這些重複的動作,所以我把這個處理封裝成了starter,下面介紹已下這個starter的使用,最後給出git庫供大家學習交流。

新增依賴

新增統一處理依賴

<dependency>  
    <groupId>io.gitee.javalaoniu</groupId>  
    <artifactId>jud-springboot-starter</artifactId>  
    <version>0.0.1</version>  
</dependency>

啟用統一處理

新增 @EnableUnifiedDisposal 註解

import io.gitee.javalaoniu.jud.annotation.EnableUnifiedDisposal;  
import org.springframework.boot.SpringApplication;  
import org.springframework.boot.autoconfigure.SpringBootApplication;  
  
@EnableUnifiedDisposal  
@SpringBootApplication  
public class JudDemoApplication {  
    public static void main(String[] args) {  
        SpringApplication.run(JudDemoApplication.class, args);  
    }  
}

攔截的處理

像平常一樣返回資料即可,不需要做其它

import io.gitee.javalaoniu.jud.annotation.IgnoreResponseAdvice;  
import io.gitee.javalaoniu.jud.common.Result;  
import io.gitee.javalaoniu.jud.exception.BusinessException;  
import io.gitee.javalaoniu.jud.exception.ExceptionCode;  
import org.springframework.web.bind.annotation.GetMapping;  
import org.springframework.web.bind.annotation.RestController;  
  
import java.util.ArrayList;  
import java.util.List;  
  
@RestController  
public class DemoController {  
  
    @GetMapping("test1")  
    public String stringTest() {  
        return "hello";  
        // {"code":200,"data":"hello","succ":true,"ts":1673943672244}
    }  
  
    @GetMapping("test2")  
    public String stringNullTest() {  
        return null;  
        // {"code":200,"data":"","succ":true,"ts":1673943691844}
    }  
  
    @GetMapping("test3")  
    public Object objectEntityTest() {  
        DemoEntity demoEntity = new DemoEntity();  
        demoEntity.setName("張三");  
        demoEntity.setAge(50);  
        demoEntity.setSex(false);  
        demoEntity.setSalary(4500000001542.26);  
        return demoEntity;  
        // {"succ":true,"ts":1673943709119,"data":{"name":"張三","age":50,"sex":false,"salary":4.50000000154226E12},"code":200,"msg":null}
    }  
  
    @GetMapping("test4")  
    public Object objectNotNullTest() {  
        return "hello Object";  
        // {"code":200,"data":"hello Object","succ":true,"ts":1673943726435}
    }  
  
    @GetMapping("test5")  
    public Object objectNullTest() {  
        return null;  
        // 啥也沒返回,但是如果設定了json轉換器的話會返回:{"code":200,"data":null,"succ":true,"ts":1673943726435}
    }  
  
    @GetMapping("test6")  
    public List<DemoEntity> listTest() {  
        DemoEntity demoEntity2 = new DemoEntity();  
        demoEntity2.setName("張三");  
        demoEntity2.setAge(50);  
        demoEntity2.setSex(false);  
        demoEntity2.setSalary(4500000001542.26);  
  
        DemoEntity demoEntity = new DemoEntity();  
        demoEntity.setName("張三");  
        demoEntity.setAge(50);  
        demoEntity.setSex(false);  
        demoEntity.setSalary(4500000001542.26);  
  
        List<DemoEntity> list = new ArrayList<>();  
        list.add(demoEntity);  
        list.add(demoEntity2);  
  
        return list;  
        // {"succ":true,"ts":1673943797079,"data":[{"name":"張三","age":50,"sex":false,"salary":4.50000000154226E12},{"name":"張三","age":50,"sex":false,"salary":4.50000000154226E12}],"code":200,"msg":null}
    }  
  
    @GetMapping("test7")  
    public List<String> listNullTest() {  
        return null;  
        // {"succ":true,"ts":1673943819382,"data":null,"code":200,"msg":null}
    }  
  
    @GetMapping("test8")  
    public Result resultTest() {  
        DemoEntity demoEntity = new DemoEntity();  
        demoEntity.setName("張三");  
        demoEntity.setAge(50);  
        demoEntity.setSex(false);  
        demoEntity.setSalary(4500000001542.2656564545);  
        return Result.success(demoEntity);  
        // {"succ":true,"ts":1673943832081,"data":{"name":"張三","age":50,"sex":false,"salary":4.500000001542266E12},"code":200,"msg":null}
    }  
  
    @IgnoreResponseAdvice  
    @GetMapping("test9")  
    public String ignoreResponseTest() {  
        return "IgnoreResponseAdvice";  
        // IgnoreResponseAdvice
    }  
  
    @GetMapping("test10")  
    public String businessExceptionTest() {  
        throw new BusinessException(ExceptionCode.EXCEPTION);  
        // {"succ":false,"ts":1673943862588,"data":null,"code":500,"msg":"伺服器開小差,請稍後再試(Internal Server Error)"}
    }  
}

不攔截處理

對不需要統一處理的controller或者方法使用下面註解

@IgnoreResponseAdvice  
@GetMapping("test9")  
public String ignoreResponseTest() {  
	// 在方法上使用,直接返回IgnoreResponseAdvice字串給前端
    return "IgnoreResponseAdvice";  
}

可以看到,使用統一處理starter後,統一返回物件和統一例外處理不需要自己在處理,非常方便。
git倉庫地址:https://gitee.com/javalaoniu/javalaoniu-jud