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);
 }

@requestbody注解的一些注意事项

1.@requestbody注解用来获取请求体中的数据

直接使用得到的是key=value&key=value…结构的数据,因此get方式不适用(get方式下@requestbody获取不到任何数据)。

例:

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

前后端参数要匹配个数不能少,字段名字要一样。

以上为个人经验,希望能给大家一个参考,也希望大家多多支持www.887551.com。