1

I want to know if there is a way of removing those headers from my current response. I am using the @ResponseBody annotation and already have tried using a Filter to try to not add those headers, based on the following How do delete a HTTP response header?.

Ideally, the HTTP response should be like the one from this link: https://api.github.com/users/mralexgray/repos with no Content-Length and Transfer-Encoding headers.

Community
  • 1
  • 1
user1139406
  • 27
  • 1
  • 5
  • 1
    i dont want to be the guy who says "why do you want to do that?", but why do you want to do that? knowing might help find a solution to the overall problem – slipperyseal Jul 04 '16 at 02:51

1 Answers1

1

You could write directly to the HttpServletResponse's OutputStream. Spring will give you the HttpServletResponse (and the HttpServletRequest) if you want it, simply by adding it to your method signature.

This way you have (mostly) full control of headers. You would probably need to create the JSON yourself, but it's usually quite simple. For example...

private ObjectMapper mapper = new ObjectMapper();

@RequestMapping(value = "/getStuff", method = RequestMethod.GET)
public void getStuff(HttpServletResponse httpServletResponse) throws Exception {
    try {
        httpServletResponse.setHeader("Pragma","public");
        httpServletResponse.setHeader("Expires","0");
        httpServletResponse.setHeader("Cache-Control","must-revalidate, post-check=0, pre-check=0");
        httpServletResponse.setHeader("Cache-Control","public");
        OutputStream outputStream = httpServletResponse.getOutputStream();
        try {
            mapper.writeValue(outputStream, myObject);
        } finally {
            outputStream.close();
        }

This might not seem elegant, but by using @ResponseBody you are using that as a convenience to do all the hard work in creating the response. But if it is not creating the response as you would like it, you can take a step back and do it "manually" using HttpServletResponse.

slipperyseal
  • 2,728
  • 1
  • 13
  • 15
  • Thanks, I tried your example and got the following response : ==================================== HTTP/1.1 200 OK Server: Apache-Coyote/1.1 Pragma: public Expires: 0 Cache-Control: public **Content-Length: 72** Date: Mon, 04 Jul 2016 03:43:18 GMT ================================== It seems like that Header is added in another component by default. I guess if this must be solved via a configuration in the server or the project. – user1139406 Jul 04 '16 at 03:44
  • that's weird. try sending HEAPs of data, as a test. are you testing locally? maybe there is a reverse proxy involved somewhere. for a long content stream, the length shouldn't be determinable unless the entire content is cached somewhere. but again, the content length is a valid header and part of the HTTP protocol, so im not sure why you need to remove it. – slipperyseal Jul 04 '16 at 04:00
  • i noticed its using the Tomcat Coyote connector. check that compression is disabled. maybe the compression process is resulting in a content length which it's adding – slipperyseal Jul 04 '16 at 04:03
  • 2
    as a note, for HTTP1.1 with persistent connections, you need to send the content length, or the connector needs to use chunked encoding. otherwise the stream doesn't know when to stop reading, and it would have to close the connection at the end of each request. not sure what problem you are trying to solve, but chunked encoding might make it worse – slipperyseal Jul 04 '16 at 04:06
  • Currently I want to remove both headers as part of a test with another server that reads jsons from this project and validate an use case.I also think that the Content-Length should be included or the Transfer-Encoding must be set to "chunked", but I want to be sure if there is a way to do this before saying that it is impossible. I will try checking if the compression is enabled. – user1139406 Jul 04 '16 at 04:21