欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringController返回值和異常自動包裝的問題小結(jié)

 更新時間:2024年03月13日 10:44:15   作者:applebomb  
今天遇到一個需求,在不改動原系統(tǒng)代碼的情況下,將Controller的返回值和異常包裝到一個統(tǒng)一的返回對象中去,下面通過本文給大家介紹SpringController返回值和異常自動包裝的問題,需要的朋友可以參考下

今天遇到一個需求,在不改動原系統(tǒng)代碼的情況下。將Controller的返回值和異常包裝到一個統(tǒng)一的返回對象中去。

例如原系統(tǒng)的接口

public String myIp(@ApiIgnore HttpServletRequest request);

返回的只是一個IP字符串"0:0:0:0:0:0:0:1",目前接口需要包裝為:

{"code":200,"message":"","result":"0:0:0:0:0:0:0:1","success":true}

而原異常跳轉(zhuǎn)到error頁面,需要調(diào)整為

{
  "success": false,
  "message": "For input string: \"fdsafddfs\"",
  "code": 500,
  "result": "message"
}

因此就有了2個工作子項需要完成:

1)Exception的處理

2)controller return值的處理

Exception的自動包裝

返回的exception處理可以采用@RestControllerAdvice來處理。

建立自己的Advice類,注入國際化資源(異常需要支持多語言)

package org.ccframe.commons.mvc;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpStatus;
import org.ccframe.commons.filter.CcRequestLoggingFilter;
import org.ccframe.commons.util.BusinessException;
import org.ccframe.config.GlobalEx;
import org.ccframe.subsys.core.dto.Result;
import org.springframework.context.MessageSource;
import org.springframework.context.NoSuchMessageException;
import org.springframework.core.MethodParameter;
import org.springframework.http.ResponseEntity;
import org.springframework.orm.ObjectOptimisticLockingFailureException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodReturnValueHandler;
import org.springframework.web.method.support.ModelAndViewContainer;
import org.springframework.web.multipart.MaxUploadSizeExceededException;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.NoHandlerFoundException;
import javax.servlet.http.HttpServletRequest;
import java.util.Locale;
@RestControllerAdvice
@Log4j2
public class GlobalRestControllerAdvice{
    private MessageSource messageSource; //國際化資源
    private LocaleResolver localeResolver;
    private Object[] EMPTY_ARGS = new Object[0];
    public GlobalRestControllerAdvice(MessageSource messageSource, LocaleResolver localeResolver){
        this.messageSource = messageSource;
        this.localeResolver = localeResolver;
    }
    private Result<String> createError(HttpServletRequest request, Exception e,int code, String msgKey, Object[] args){
        Locale currentLocale = localeResolver.resolveLocale(request);
        String message = "";
        try {
            message = messageSource.getMessage(msgKey, args, currentLocale);
        }catch (NoSuchMessageException ex){
            message = e.getMessage();
        }finally {
            log.error(message);
            CcRequestLoggingFilter.pendingLog(); //服務器可以記錄出錯時的請求啦??
        }
        return Result.error(code, message, msgKey);
    }
    @ExceptionHandler(NoHandlerFoundException.class)
    public Result<?> handlerNoFoundException(HttpServletRequest request, Exception e) {
        return createError(request, e, HttpStatus.SC_NOT_FOUND, "error.mvc.uriNotFound", EMPTY_ARGS);
    }
    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    public Result<?> httpRequestMethodNotSupportedException(HttpServletRequest request, HttpRequestMethodNotSupportedException e){
        return createError(request,e, HttpStatus.SC_NOT_FOUND,"error.mvc.methodNotSupported",
            new Object[]{e.getMethod(), StringUtils.join(e.getSupportedMethods(), GlobalEx.DEFAULT_TEXT_SPLIT_CHAR)});
    }
    @ExceptionHandler(BusinessException.class)
    public Result<?> businessException(HttpServletRequest request, BusinessException e){
        return createError(request,e, HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMsgKey(), e.getArgs());
    }
    @ExceptionHandler(ObjectOptimisticLockingFailureException.class) //樂觀鎖異常
    public Result<?> objectOptimisticLockingFailureException(HttpServletRequest request, ObjectOptimisticLockingFailureException e){
        return createError(request,e, HttpStatus.SC_INTERNAL_SERVER_ERROR, "errors.db.optimisticLock", EMPTY_ARGS);
    }
    @ExceptionHandler(MaxUploadSizeExceededException.class) // 文件上傳超限,nginx請設置為10M
    public Result<?> handleMaxUploadSizeExceededException(HttpServletRequest request, MaxUploadSizeExceededException e) {
        return createError(request, e, HttpStatus.SC_INTERNAL_SERVER_ERROR, "error.mvc.fileTooLarge", EMPTY_ARGS);
    }
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Result<?> handleException(HttpServletRequest request, Exception e) {
        log.error(e);
        return createError(request,e, HttpStatus.SC_INTERNAL_SERVER_ERROR, "message", new Object[]{e.getMessage()});
    }
}
 

在Config類初始化該Bean(當然也可以使用@Component支持掃描,隨你喜歡)

@Bean
	public GlobalRestControllerAdvice globalRestControllerAdvice(MessageSource messageSource, LocaleResolver localeResolver){
		return new GlobalRestControllerAdvice(messageSource, localeResolver);
	}

controller return值的自動包裝

網(wǎng)上的例子有很多坑,包括使用HandlerMethodReturnValueHandler,看了源碼才發(fā)現(xiàn)。還是ResponseBodyAdvice好使。

建立自己的處理Bean

package org.ccframe.commons.mvc;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.TypeReference;
import org.apache.http.HttpStatus;
import org.ccframe.commons.util.JsonUtil;
import org.ccframe.subsys.core.dto.Result;
import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;
import springfox.documentation.swagger.web.ApiResourceController;
import java.util.regex.Pattern;
@ControllerAdvice
public class CcResponseBodyAdvice implements ResponseBodyAdvice<Object> {
    private static final Pattern CONTROLLER_PATTERN = Pattern.compile("^org\\.ccframe\\.(subsys|sdk)\\.[a-z0-9]+\\.controller\\.");
    @Override
    public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) {
        return 
    // 只有自己的cotroller類才需要進入,否則swagger都會掛了
CONTROLLER_PATTERN.matcher(returnType.getContainingClass().getName()).find();
    }
    @Override
    public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, Class<? extends HttpMessageConverter<?>> selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {
        System.out.println(returnType.getContainingClass());
        Result<Object> result = new Result<>();
        result.setResult(body);
        result.setCode(HttpStatus.SC_OK);
        if(body instanceof String){ //String返回要特殊處理
            return JSON.toJSONString(result);
        }else {
            return result;
        }
    }
}

如果你不需要根據(jù)正則來指定包,可以直接用RestControllerAdvice的basePackages屬性來過濾

注意這里有2個坑

1)String類型的返回被其它的轉(zhuǎn)換接口StringHttpMessageConverter處理,因此返回要進行JSON編碼而不能返回其他類型,否則會報cast類型錯,因此就有了String部分的特殊處理方法。

2)controller方法簽名返回是void時,不會被處理。為什么,有什么辦法?得看spring這段源碼:

當returnValue==null時,設置為RequestHandled,也就是提前結(jié)束了。后面任何返回的處理都不再進行。所以,如果一定要返回null值的話,可以在controller里返回一個
return new ResponseEntity<Void>(HttpStatus.OK);
這樣在返回的值里面就有詳細的結(jié)構(gòu)了。

最后要生效的話,在Config類初始它:

@Bean
	public CcResponseBodyAdvice ccResponseBodyAdvice() {
		return new CcResponseBodyAdvice();
	}

最后。上面兩個Bean也可以寫在一個,有興趣的自己嘗試。

---------------

null無法被BodyAdvice處理的問題。隨著源碼跟蹤,慢慢知道怎么回事了,我們嘗試根本來解決這個問題。從這個圖開始:

當返回為null時,mavContainer.isRequestHandled()為true導致了后面的沒有處理。

那么想當然的,mavContainer.isRequestHandled()為flase不久解決了嗎,向前跟蹤,基本到MVC invoke的核心代碼里了,發(fā)現(xiàn)在invoke前,mavContainer.isRequestHandled()變成了true,再繼續(xù)跟蹤,找到這個方法:

在HandlerMethodArgumentResolverComposite的argumentResolvers看到了上面這個。進行了setRequestHandled。HandlerMethodArgumentResolver是spring controller的參數(shù)自動注入機制??戳讼略创a也沒有太多的擴展點,于是只能換個思路。

到此這篇關(guān)于SpringController返回值和異常自動包裝的文章就介紹到這了,更多相關(guān)SpringController返回值內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Java中Socket設置連接超時的代碼分享

    Java中Socket設置連接超時的代碼分享

    在我們?nèi)粘_B接中,如果超時時長過長的話,在開發(fā)時會影響測試,下面這篇文章主要給大家分享了關(guān)于Java中Socket設置連接超時的代碼,需要的朋友可以參考借鑒,下面來一起看看吧。
    2017-06-06
  • 深入解析Session工作原理及運行流程

    深入解析Session工作原理及運行流程

    這篇文章主要介紹了深入解析Session工作原理及運行流程,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-07-07
  • Jenkins遠程部署war包過程圖解

    Jenkins遠程部署war包過程圖解

    這篇文章主要介紹了Jenkins遠程部署war包過程圖解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-05-05
  • JUnit測試控制@Test執(zhí)行順序的三種方式小結(jié)

    JUnit測試控制@Test執(zhí)行順序的三種方式小結(jié)

    這篇文章主要介紹了JUnit測試控制@Test執(zhí)行順序的三種方式小結(jié),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • Java如何將Excel數(shù)據(jù)導入到數(shù)據(jù)庫

    Java如何將Excel數(shù)據(jù)導入到數(shù)據(jù)庫

    這篇文章主要為大家詳細介紹了Java將Excel數(shù)據(jù)導入到數(shù)據(jù)庫的方法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-10-10
  • Java中==與equals()及hashcode()三者之間的關(guān)系詳解

    Java中==與equals()及hashcode()三者之間的關(guān)系詳解

    最近也是在讀Hollis的《深入理解Java核心技術(shù)》里面一節(jié)講到了equals()和hashcode()的關(guān)系,對于這個高頻面試點,咱們需要認真理清一下幾者之間的關(guān)系
    2022-10-10
  • Spring的Aware接口實現(xiàn)及執(zhí)行順序詳解

    Spring的Aware接口實現(xiàn)及執(zhí)行順序詳解

    這篇文章主要為大家介紹了Spring的Aware接口實現(xiàn)及執(zhí)行順序詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-12-12
  • Java和Python現(xiàn)在都挺火,我應該怎么選?

    Java和Python現(xiàn)在都挺火,我應該怎么選?

    這篇文章主要介紹了Java和Python現(xiàn)在都挺火,我應該怎么選?本文通過全面分析給大家做個參考,需要的朋友可以參考下
    2020-07-07
  • IDEA設置Tab選項卡快速的操作

    IDEA設置Tab選項卡快速的操作

    這篇文章主要介紹了IDEA設置Tab選項卡快速的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-02-02
  • mybatis?like模糊查詢特殊字符報錯轉(zhuǎn)義處理方式

    mybatis?like模糊查詢特殊字符報錯轉(zhuǎn)義處理方式

    這篇文章主要介紹了mybatis?like模糊查詢特殊字符報錯轉(zhuǎn)義處理方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-01-01

最新評論