如何在RequestDispatcher转发后获取请求和响应作为字符串。

huangapple go评论61阅读模式
英文:

How to get request and response as string after RequestDispatcher forward

问题

In my Springboot application, I use RequestDispatcher forward to send the request and response to a remote service. Now I need to get that response and request as string to log it into the database. The request and response both contain JSON data.

RequestDispatcher requestDispatcher = request.getRequestDispatcher(configAPI.getUserByPhoneUrl());
requestDispatcher.forward(request, response);

The RequestDispatcher will modify the response in-place. So after the forward() function, I'm unable to get the JSON from the response created by requestDispatcher.forward(request, response).

@RequestMapping(value = "/getUserByPhone", method = {RequestMethod.GET}, produces = "application/json;charset=utf-8")
public void getUserbyIsdn(HttpServletRequest request, HttpServletResponse response) {
    // Forward the request to the remote service
    RequestDispatcher requestDispatcher = request.getRequestDispatcher(configAPI.getUserByPhoneUrl());
    requestDispatcher.forward(request, response);
    // response now has the result from the remote service
    // TODO: print out JSON in response here
}

Any idea on how to get the response as JSON?

I've tried using HttpServletResponseWrapper to access the response but was unable to. Example code using HttpServletResponseWrapper:

HttpServletRequestWrapper requestWrapper = new HttpServletRequestWrapper(request);
HttpServletResponseWrapper responseWrapper = new HttpServletResponseWrapper(response);

// Forward the request to the remote service
request.getRequestDispatcher(configAPI.getUserByPhoneUrl()).forward(requestWrapper, responseWrapper);

But it resulted in a NullPointerException because the ApplicationContext request is not set.

英文:

In my Springboot application, I use RequestDispatcher forward to send the request and response to a remote service. Now I need to get that response and request as string to log it into database.
The request and response both contain JSON data.

RequestDispatcher requestDispatcher = request.getRequestDispatcher(configAPI.getUserByPhoneUrl());
requestDispatcher.forward(request, response);

The RequestDispatcher will modify the response in-place. So after the forward() function, I'm unable to get the json from the response created by requestDispatcher.forward(request, response).

@RequestMapping(value = "/getUserByPhone", method = {RequestMethod.GET}, produces = "application/json;charset=utf-8")
    public void getUserbyIsdn(HttpServletRequest request, HttpServletResponse response) {
            // Forward the request to the remote service
            RequestDispatcher requestDispatcher = request.getRequestDispatcher(configAPI.getUserByPhoneUrl());
            requestDispatcher.forward(request, response);
            // response now has result from remote service
            // TODO: print out json in response here
    }

Any idea on how to get response as json?

I've try using HttpServletResponseWrapper to access the response but unable to.
Example code using HttpServletResponseWrapper:

HttpServletRequestWrapper requestWrapper = new HttpServletRequestWrapper(request);
            HttpServletResponseWrapper responseWrapper = new HttpServletResponseWrapper(response);

            // Forward the request to the remote service
            request.getRequestDispatcher(configAPI.getUserByPhoneUrl()).forward(requestWrapper, responseWrapper);

but is result in NullPointerException because ApplicationContext request is not set.

答案1

得分: 1

以下是您要翻译的内容:

"After some trying out, I was able to get response and request json string using ContentCachingRequestWrapper and ContentCachingResponseWrapper. The controller now become like this:

@RequestMapping(value = "/getUserByPhone", method = {RequestMethod.GET}, produces = "application/json;charset=utf-8")
    public void getUserbyIsdn(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        ContentCachingRequestWrapper requestWrapper = new ContentCachingRequestWrapper(request);
        ContentCachingResponseWrapper responseWrapper = new ContentCachingResponseWrapper(response);
        requestWrapper.getRequestDispatcher(configAPI.getApi_getUserbyIsdn_user()).forward(requestWrapper, responseWrapper);
        // do what ever you want here with response and request
        apiLogService.addApiLog(requestWrapper, responseWrapper, "getUserByIsdn", timer);

        // have to has this to return original response of other service
        responseWrapper.copyBodyToResponse();
    }

Function addApiLog:

public void addApiLog(ContentCachingRequestWrapper request, ContentCachingResponseWrapper response) {
    String requestJson = IOUtils.toString(request.getContentAsByteArray());
    String responseJson = IOUtils.toString(response.getContentAsByteArray());
    // handle json String using JSONObject or GSON as needed
}
```"

<details>
<summary>英文:</summary>

After some trying out, I was able to get response and request json string using `ContentCachingRequestWrapper` and `ContentCachingResponseWrapper`. The controller now become like this:
```java
@RequestMapping(value = &quot;/getUserByPhone&quot;, method = {RequestMethod.GET}, produces = &quot;application/json;charset=utf-8&quot;)
    public void getUserbyIsdn(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        ContentCachingRequestWrapper requestWrapper = new ContentCachingRequestWrapper(request);
        ContentCachingResponseWrapper responseWrapper = new ContentCachingResponseWrapper(response);
        requestWrapper.getRequestDispatcher(configAPI.getApi_getUserbyIsdn_user()).forward(requestWrapper, responseWrapper);
        // do what ever you want here with response and request
        apiLogService.addApiLog(requestWrapper, responseWrapper, &quot;getUserByIsdn&quot;, timer);

        // have to has this to return original response of other service
        responseWrapper.copyBodyToResponse();
    }

Function addApiLog:

public void addApiLog(ContentCachingRequestWrapper request, ContentCachingResponseWrapper response) {
    String requestJson = IOUtils.toString(request.getContentAsByteArray());
    String responseJson = IOUtils.toString(response.getContentAsByteArray());
    // handle json String using JSONObject or GSON as needed
}

huangapple
  • 本文由 发表于 2023年4月13日 18:00:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76004120.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定