spring mvc 获取@Requestbody转换的异常处理

1、引入问题

​ 使用spring 自动的@RequestBody,可以很方便的将参数转换成对象,然而在自动转换中出现如果出现异常,会默认直接发送HTTP异常代码和错误信息,如何才能自定义自己的异常呢。

2 、解决方案

解答问题的方式有可以有很多,一种通用的解答方式是使用@ExceptionHandler

2.1 例如传递的请求体为JSON时,Spring 可以自动封装成一个Map

@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)

返回给前端的可能如下格式的提示,默认的格式不是太好处理

{
    "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 自定义错误格式输出

@ExceptionHandler(value = JsonParseException.class)
public @ResponseBody ApiResult exceptionHandler(JsonParseException e){
	return new ApiResult(500, "调用接口异常,解析请求体JSON格式错误", null);
}

2.4 如果还想获取传递的请求体参数呢,因为请求体是流的形式,只能读一次,在解析请求体后,流已经关闭了。再在上面的代码中添加request获取请求体,会得到一个已经关闭的流。下面是结合网上的例子和实践过的方案

2.4.1 定义一个filter,缓存请求

/**
 * 
 * @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 在自定义错误格式中使用缓存的请求

@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);
	}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值