79129

Remove Content-Length and Transfer-Encoding Headers from spring servlet HTTP Response

Question:

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 <a href="https://stackoverflow.com/questions/7895196/how-do-delete-a-http-response-header" rel="nofollow">How do delete a HTTP response header?</a>.

Ideally, the HTTP response should be like the one from this link: <a href="https://api.github.com/users/mralexgray/repos" rel="nofollow">https://api.github.com/users/mralexgray/repos</a> with no Content-Length and Transfer-Encoding headers.

Answer1:

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.

Recommend

  • How to redefine a function in php?
  • How do I integrate a Polymer + Node.JS JavaScript app with React + TypeScript
  • C# Excel interop merge cells in fast way
  • The API package 'channel' or call 'CreateChannel()' was not found
  • Problems with show dialog on Background task in windows phone 8.1
  • File Upload handler
  • ASP.NET Capture and replace output in Global.asax
  • How to connect to a GitHub repo using libgit2?
  • How to send image as base64 string in JSON using HTTP POST in Android?
  • How find PID by port in windows and kill found tasks using java
  • How do I deploy application on sourcevoid with private git package dependencies?
  • ExtJS 4 Spring 3 file upload. Server sends bad response content type
  • How to silently drop a request in Tomcat?
  • How can I configure HTTP Response Headers in a Struts2 Interceptor?
  • Where does the file get saved using “File file = new file(filename)” in Android
  • jQuery and Uploadify session in the php file
  • How to get file download speed (transfer rate) with php?
  • How to select table rows/complete table?
  • How to convert workspace coordinates to screen coordinates?
  • How can I extract results of aggregate queries in slick?
  • Declaring variable dynamically in VB.net
  • Why Encoding.ASCII != ASCIIEncoding.Default in C#?
  • copying resource to sdcard gives a damaged file in android
  • How do I alternate colors in Flat List (React Native)
  • Setting up SourceTree to merge unity3d scenes with UnityYAMLMerge
  • Email format validation in mvc3 view
  • Installing Hadoop, Java Exception about illegal characters at index 7?
  • Encrypt data by using a public key in c# and decrypt data by using a private key in php
  • C# - Is there a limit to the size of an httpWebRequest stream?
  • DotNetZip - Calculate final zip size before calling Save(stream)
  • AES padding and writing the ciphertext to a disk file
  • How to add date and time under each post in guestbook in google app engine
  • SSO with signing and signature validation doesn't work
  • Deserializing XML into class C#
  • retrieve vertices with no linked edge in arangodb
  • coudnt use logback because of log4j
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • Change div Background jquery
  • apache spark aggregate function using min value
  • costura.fody for a dll that references another dll