自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

我們一起聊聊 Spring 統(tǒng)一處理異常與響應(yīng)

開發(fā) 前端
定義統(tǒng)一的異常處理流程,通過@RestControllerAdvice與@ExceptionHandler注解可以對(duì)Controller中的異常統(tǒng)一處理。

在web開發(fā)中,規(guī)范所有請(qǐng)求響應(yīng)類型,不管是對(duì)前端數(shù)據(jù)處理,還是后端統(tǒng)一數(shù)據(jù)解析都是非常重要的。今天我們簡單的方式實(shí)現(xiàn)如何實(shí)現(xiàn)這一效果。

實(shí)現(xiàn)方式

  1. 定義響應(yīng)類型
public class ResponseResult<T> {

    private static final String SUCCESS_CODE = "000";
    private static final String FAILURE_CODE = "999";

    private String code;
    private String message;
    private T data;
    
    public static <T> ResponseResult<T> ok(T data){
        ResponseResult responseResult = new ResponseResult();
        responseResult.setCode(SUCCESS_CODE);
        responseResult.setData(data);
        return responseResult;
    }

    public static ResponseResult fail(String code, String message){
        if( code == null ){
            code = FAILURE_CODE;
        }
        ResponseResult responseResult = new ResponseResult();
        responseResult.setCode(code);
        responseResult.setMessage(message);
        return responseResult;
    }

    public static ResponseResult fail(String message){
        return fail(FAILURE_CODE, message);
    }
}
  1. 定義統(tǒng)一的異常處理流程,通過@RestControllerAdvice與@ExceptionHandler注解可以對(duì)Controller中的異常統(tǒng)一處理。
@RestControllerAdvice
public class ControllerAdviceHandle {
    
    @ExceptionHandler(Exception.class)
    public ResponseResult handleException(Exception exception) {
        BusException busException;
        if (exception instanceof BusException asException) {
            busException = asException;
        } else {
            busException = convertException(exception);
        }
        return ResponseResult.fail(busException.getCode(), busException.getMessage());
    }
}
  1. 定義統(tǒng)一響應(yīng)攔截,通過是實(shí)現(xiàn)接口ResponseBodyAdvice,這里可以和上面的異常一起處理。
public class ControllerAdviceHandle implements ResponseBodyAdvice {
    
    @Override
    public boolean supports(MethodParameter returnType, Class converterType) {
        return true;
    }

    @Override
    public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, Class selectedConverterType,
                                  ServerHttpRequest request, ServerHttpResponse response) {
        if( body instanceof ResponseResult){
            return body;
        }
        return ResponseResult.ok(body);
    }
}
  1. 定義spring配置,實(shí)現(xiàn)自動(dòng)裝配

在resource目錄添加自動(dòng)注入配置META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports,這樣通過引入jar就可以自動(dòng)使用該配置。

cn.cycad.web.response.ResponseConfig

應(yīng)用示例

  1. 比如現(xiàn)在有一個(gè)User實(shí)體,我們通過繼承基類。
@RestController
@RequestMapping("/test")
public class TestController {
    
    @GetMapping("/{val}")
    public Object get(@PathVariable("val") String val) throws BusException {
        if( "1".equals(val) ){
            throw new BusException("參數(shù)錯(cuò)誤");
        }
        return Map.of("val",val);
    }

}
  1. 通過調(diào)用請(qǐng)求,可以看到不管是否異常,結(jié)果都是下面的格式。
{
  "code": "999",
  "message": null,
  "data": null
}

責(zé)任編輯:武曉燕 來源: Java技術(shù)指北
相關(guān)推薦

2025-02-13 00:34:22

Spring對(duì)象系統(tǒng)

2019-08-22 14:02:00

Spring BootRestful APIJava

2022-04-06 08:23:57

指針函數(shù)代碼

2020-05-26 13:48:05

后端框架異常

2024-02-26 00:00:00

架構(gòu)老化重構(gòu)

2023-08-04 08:20:56

DockerfileDocker工具

2022-05-24 08:21:16

數(shù)據(jù)安全API

2023-08-10 08:28:46

網(wǎng)絡(luò)編程通信

2023-09-10 21:42:31

2023-06-30 08:18:51

敏捷開發(fā)模式

2021-08-27 07:06:10

IOJava抽象

2024-02-20 21:34:16

循環(huán)GolangGo

2023-12-05 14:10:00

接口可讀性

2023-12-06 08:26:19

Service數(shù)據(jù)庫

2023-07-04 08:06:40

數(shù)據(jù)庫容器公有云

2024-01-29 09:01:20

React列表模式

2023-05-09 07:51:28

Spring循環(huán)依賴

2022-02-23 08:41:58

NATIPv4IPv6

2022-09-22 08:06:29

計(jì)算機(jī)平板微信

2024-07-26 09:47:28

點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)