Results 1 to 5 of 5

Thread: Spring MVC automatically appends charset to Content-Typ

  1. #1
    Join Date
    Jul 2006
    Posts
    5

    Default Spring MVC automatically appends charset to Content-Typ

    Spring MVC (tested with 3.0.4 and 2.5.6) automatically appends charset to Content-Type headers in HTTP responses, including responses with Content-Type application/*, e.g. application/java-archive. This causes issues for certain consuming clients.

    Raw servlets in Tomcat do not act in this way and a charset is not appended with raw servlets in Tomcat if the Content-Type is of type application/*.

    The code I used to test a Raw servlet is below:

    Code:
    public class TestServlet extends HttpServlet
    {
    	private static final long serialVersionUID = 1L;
    	
    	
    	@Override
    	protected void service(HttpServletRequest req, HttpServletResponse res)
    			throws ServletException, IOException {
    		
    		res.setContentType("application/java-archive");
    		res.setContentLength(2);
    		
    		res.getOutputStream().write(new byte[]{0x01,0x02});
    		
    		
    	}
    	
    }
    The response headers from the Raw tomcat servlet using the linux command:

    wget --save-headers http://localhost:8080/testservlet/TestServlet

    is:

    HTTP/1.1 200 OK
    Server: Apache-Coyote/1.1
    Content-Type: application/java-archive
    Content-Length: 2
    Date: Tue, 21 Sep 2010 12:29:31 GMT
    Connection: keep-alive


    The code for the spring view is below:


    Code:
    public class TestSpringView extends AbstractView{
    
    	@Override
    	protected void renderMergedOutputModel(Map<String, Object> model,
    			HttpServletRequest req, HttpServletResponse res) throws Exception {
    		
    		res.setContentType("application/java-archive");
    		res.setContentLength(2);
    		
    		res.getOutputStream().write(new byte[]{0x01,0x02});
    		
    	}
    
    }
    The response headers from the Spring controller using the linux command:

    wget --save-headers http://localhost:8080/testservlet/d/d/spring

    is:

    HTTP/1.1 200 OK
    Server: Apache-Coyote/1.1
    Content-Type: application/java-archive;charset=ISO-8859-1
    Content-Language: en-ZA
    Content-Length: 2
    Date: Tue, 21 Sep 2010 12:31:11 GMT
    Connection: keep-alive


    Note that the response from the spring controller automatically appends a default charset to the Content-Type header. How do I prevent this?

    I have created a complete project with all source/libs/ant build scripts that demonstrates the issue at:

    https://docs.google.com/fileview?id=...0ODJj&hl=en_GB

  2. #2
    Join Date
    Jul 2010
    Location
    Venice, Italy
    Posts
    709

    Default

    Have you tried configuring your Spring view like this?

    Code:
    <bean id="myId" class="com.mycompany.myproject.TestSpringView">
      <property name="contentType" value=""/>
    </bean>
    Beside this, extending AbstractView is not the recommended way to use Spring mvc...just annotate your class with @Controller and activate mvc:annotation-driven and, optionally, component-scan (so that you can avoid bean definition in xml).

  3. #3
    Join Date
    Jul 2006
    Posts
    5

    Default

    Thanks for the reply! I tried your suggestion, but unfortunately I still get the same response from the Spring controller.

    The project that we are experiencing this issue in is about 3 years old, it has not yet been updated to use annotations.

    Do you have any other suggestions that I could try?

  4. #4
    Join Date
    Jul 2010
    Location
    Venice, Italy
    Posts
    709

    Default

    Hi,

    by looking better at your code, I found another thing you could try. You shouldn't set the contentType on the response object you get as method attribute, so delete this:

    Code:
    res.setContentType("application/java-archive");
    and, instead, try to use the bean configuration:

    Code:
    <bean id="myId" class="com.mycompany.myproject.TestSpringView">
      <property name="contentType" value="application/java-archive"/>
    </bean>
    Give it a try, might work this way.

  5. #5
    Join Date
    Jul 2006
    Posts
    5

    Default

    Thanks for the further suggestion! Unfortunately, it did not solve my problem. Calling the reset method on the HttpServletResponse does solve the problem though. It seems that Spring sets the character encoding before delegating to the View and the only way I have found to reset the character encoding header is by calling HttpServletResponse.reset()


    Code:
    public class TestSpringView extends AbstractView{
    
    	@Override
    	protected void renderMergedOutputModel(Map<String, Object> model,
    			HttpServletRequest req, HttpServletResponse res) throws Exception {
    		 
                    res.reset();
    		res.setContentType("application/java-archive");
    		res.setContentLength(2);
    		
    		res.getOutputStream().write(new byte[]{0x01,0x02});
    		
    	}
    
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •