简体   繁体   English

我在springboot中使用Interceptor,但是在这里我得到了错误

[英]I am using the Interceptor in springboot , but there I am getting the error as

I have to use the interceptor in springboot to to do some processing before & after request gets proceed. 我必须在springboot中使用拦截器在请求得到处理之前和之后进行一些处理。 But while using it I am getting one error: As of now I just tried to use to pre-handle method and there I am facing this issue. 但是在使用它时,我遇到了一个错误:到目前为止,我只是试图使用pre-handle方法,而现在我正面临这个问题。

org.springframework.http.converter.HttpMessageNotReadableException: Required request body is missing"
at org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.readWithMessageConverters(RequestResponseBodyMethodProcessor.java:160)
    at org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.resolveArgument(RequestResponseBodyMethodProcessor.java:130)
    at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:124)
    at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:161)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:131)

I thought there might be some problem in Json conversion so I tried other solutions like Jackson library there also I am facing the same issue. 我以为Json转换可能会出现问题,所以我尝试了其他解决方案,例如Jackson库,那里也遇到了同样的问题。

Here I have to convert the HttpServletRequest to jsonObject but i am getting the mentioned error. 在这里,我必须将HttpServletRequest转换为jsonObject,但是我遇到了上述错误。 When I removed the below logic of BufferedReader to jsonObject conversion and just return true from prehandle method it works properly. 当我删除下面的BufferedReader到jsonObject转换的逻辑,并从prehandle方法返回true时,它可以正常工作。

Code: 码:

@Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {

        StringBuffer jb = new StringBuffer();
        String line = null;
        try {
            BufferedReader reader = request.getReader();
            while ((line = reader.readLine()) != null)
                jb.append(line);

            JSONObject obj = new JSONObject(jb.toString());
            System.out.println(obj);
        } catch (Exception e) {
        }

        return super.preHandle(request, response, handler);
    }

I am getting 400 while testing the above call from postman. 测试邮递员的上述通话时,我得到400。

When you read a body from the HttpServletRequest the body is consumed so when it reaches the controller , where you might have mentioned @RequestBody , there will be no data to be received. 当您从HttpServletRequest读取主体时,主体将被消耗,因此当它到达controller (您可能已提到@RequestBody ,将不会接收任何数据。 That's why you are receiving Status Code : 400 Required request body is missing . 这就是为什么您收到Status Code : 400 Required request body is missing

In simpler terms body in the request can be read only once. 简单来说,请求中的主体只能读取一次。

But If you want to process before the business logic you have to cache that in the interceptor for using it (May be using ThreadLocal) or look into RequestBodyAdviceAdapter 但是,如果您想在业务逻辑之前进行处理,则必须将其缓存在拦截器中以使用它(可能正在使用ThreadLocal)或查看RequestBodyAdviceAdapter

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM