1、引入問題
使用spring 自動的@RequestBody,可以很方便的將參數轉換成對象,然而在自動轉換中出現如果出現異常,會默認直接發送HTTP異常代碼和錯誤信息,如何才能自定義自己的異常呢。
2、解決方案
解答問題的方式有可以有很多,一種通用的解答方式是使用@ExceptionHandler
2.1 例如傳遞的請求體為JSON時
Spring 可以自動封裝成一個Map
1
2
3
4
|
@PostMapping (value = "/check" ,consumes = "application/json" ) public ApiResult check( @RequestBody Map<String,String> paramBody) { // ......... } |
2.2 如果請求體中是一個非正常的JSON格式
那么會出現異常,可以看到是com.fasterxml.jackson.core.JsonParseException類型的(jackson是spring boot默認的json解析庫)
14:29:40.891 [http-nio-9091-exec-3] WARN o.s.w.s.m.s.DefaultHandlerExceptionResolver - Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Unrecognized character escape '[' (code 91); nested exception is com.fasterxml.jackson.core.JsonParseException: Unrecognized character escape '[' (code 91)
返回給前端的可能如下格式的提示,默認的格式不是太好處理
1
2
3
4
5
6
7
|
{ "timestamp" : 1551680980906 , "status" : 400 , "error" : "Bad Request" , "message" : "JSON parse error: Unrecognized character escape '[' (code 91); nested exception is com.fasterxml.jackson.core.JsonParseException: Unrecognized character escape '[' (code 91)\n at [Source: (PushbackInputStream); line: 66, column: 29]" , "path" : "/check" } |
2.3 自定義錯誤格式輸出
1
2
3
4
|
@ExceptionHandler (value = JsonParseException. class ) public @ResponseBody ApiResult exceptionHandler(JsonParseException e){ return new ApiResult( 500 , "調用接口異常,解析請求體JSON格式錯誤" , null ); } |
2.4 如果還想獲取傳遞的請求體參數呢
因為請求體是流的形式,只能讀一次,在解析請求體后,流已經關閉了。再在上面的代碼中添加request獲取請求體,會得到一個已經關閉的流。下面是結合網上的例子和實踐過的方案
2.4.1 定義一個filter,緩存請求
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
/** * * @author Bob.chen * @date 2019年3月4日-下午2:10:01 * @desc 包裝下請求,是請求體可以在@ExceptionHandler中使用 */ @Component public class RequestWrapperFilter extends OncePerRequestFilter { @Override protected void doFilterInternal(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, FilterChain filterChain) throws ServletException, IOException { filterChain.doFilter( new ContentCachingRequestWrapper(httpServletRequest), httpServletResponse); } } |
2.4.2 在自定義錯誤格式中使用緩存的請求
1
2
3
4
5
6
7
8
9
|
@ExceptionHandler (value = JsonParseException. class ) public @ResponseBody ApiResult exceptionHandler(JsonParseException e, ServletRequest request) { if (request != null && request instanceof ContentCachingRequestWrapper) { ContentCachingRequestWrapper wrapper = (ContentCachingRequestWrapper) request; LOG.warn( "BAD_REQUEST_BODY:{}" , StringUtils.toEncodedString(wrapper.getContentAsByteArray(), Charset.forName(wrapper.getCharacterEncoding()))); } return new ApiResult( 500 , "調用接口異常,解析請求體JSON格式錯誤" , null ); } |
@RequestBody注解的一些注意事項
1.@RequestBody注解用來獲取請求體中的數據
直接使用得到的是key=value&key=value…結構的數據,因此get方式不適用(get方式下@RequestBody獲取不到任何數據)。
例:
1
2
3
|
public void test1( @RequestBody String body){ system.out.println(body); } |
輸出結果:
username=hehe&age=20
2.使用@RequestBody注解后
可以在方法中創建一個集合對象,前端提交的集合數據可以直接被注入到方法的集合對象中,而不需要創建一個pojo對象進行集合的封裝。
3.如果想要將前端提交的json字符串自動封裝到一個對象中
需要導入jackson的相關jar包,并使用@RequestBody注解。
注:springmvc默認使用MappingJacksonHttpMessageConverter對json數據進行轉換。
4.使用@RequestBody
前后端參數要匹配個數不能少,字段名字要一樣。
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持服務器之家。
原文鏈接:https://blog.csdn.net/changzhou0519/article/details/88116834