Reputation: 5348
I have an endpoint using Spring:
@RequestMapping(value = {"/hello"}, method = RequestMethod.GET)
@ResponseBody
public String getContent(@RequestParam(value = "url", required = true) String url)
I would like this to return the exact same response I would get if I send a GET to url
. I'm using the Apache library to do my GET, which returns me a CloseableHttpResponse
. How do I return this response as my endpoint's response? My current code copies this response, which is not what I want. I would directly like to return the CloseableHttpResponse
. The reason I want to do this is because some websites have really huge data, and I would like to avoid having to copy those in place.
@RequestMapping(value = {"/hello"}, method = RequestMethod.GET)
@ResponseBody
public String getContent(@RequestParam(value = "url", required = true) String url, HttpServletResponse response)
CloseableHttpResponse httpResponse = useApacheLibraryAndSendGetToUrl(url);
for (Header header : httpResponse.getAllHeaders()) {
response.addHeader(header.getName(), header.getValue());
}
response.setStatus(httpResponse.getStatusLine().getStatusCode());
return EntityUtils.toString(entity, "UTF-8");
}
Upvotes: 2
Views: 5222
Reputation: 522
You could write a custom HttpMessageConverter
for the CloseableHttpResponse
type, which would allow you to simply return @ResponseBody CloseableHttpResponse
.
See Mapping the response body with the @ResponseBody annotation for details.
Upvotes: 3