Announcement Announcement Module
Collapse
No announcement yet.
@ResponseBody content type Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • @ResponseBody content type

    Hi

    How to specify (configure, change,..) the content type with the @ResponseBody anotation.

    This code for example woks fine except that the content type is "text/html"

    Code:
    	@ResponseBody
    	@RequestMapping("/list")
    	public String handleListRequest(...) throws Exception
    	{
    		String xmlContent = ...
    		return xmlContent ;
    	}
    I suposed that the content type comes from the request (json request=>json content type, text request=>text/html content type).
    This does not pas this junit test.

    Code:
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations = { "classpath:spring-*.xml" })
    public class RestControllerTest
    {
    	@Autowired
    	private RestControllerTest	restControllerTest;
    	
    	@Test
    	public void handleListRequest() throws Exception
    	{
    		MockHttpServletResponse response = new MockHttpServletResponse();
    		MockHttpServletRequest request = new MockHttpServletRequest();
    		request.setRequestURI("/user/list.xml");
    		request.setContentType("application/xml");
    		
    		new AnnotationMethodHandlerAdapter().handle(request, response, restControllerTest);
    		
    		Assert.assertEquals("The response content type is not application/xml", "application/xml", response.getContentType());
    	}
    }
    The response is always with the text/html content type.

    Is there a rest guru to solve this issue
    regards

  • #2
    I found the following docs useful
    http://blog.springsource.com/2010/01...in-spring-3-0/
    http://static.springsource.org/sprin...n-responsebody

    @ResponseBody is handled by an HttpMessageConverter and the choice of implementation is determined by the Accept header on the request message. You aren't setting that header on your mock request. You have used setContentType, which specifies the content type of the request, which is what @RequestBody would use.

    Try setting an Accept header. For JSON, use "application/json" and for XML use "application/xml" or "text/xml".
    Last edited by andyc; Jun 10th, 2010, 08:25 PM.

    Comment


    • #3
      Thanks andyc

      I have updated my junit test.
      It now works as expected.
      Here is the new code.

      Code:
      @RunWith(SpringJUnit4ClassRunner.class)
      @ContextConfiguration(locations = { "classpath:spring-*.xml" })
      public class RestControllerTest
      {
      	@Autowired
      	private RestControllerTest	restControllerTest;
      	
      	@Test
      	public void handleListRequest() throws Exception
      	{
      		MockHttpServletResponse response = new MockHttpServletResponse();
      		MockHttpServletRequest request = new MockHttpServletRequest();
      		request.setRequestURI("/user/list.xml");
      		request.addHeader("Accept", "application/xml");
      		
      		new AnnotationMethodHandlerAdapter().handle(request, response, restControllerTest);
      		
      		Assert.assertEquals("The response content type is not application/xml", "application/xml", response.getContentType());
      	}
      }
      The problem is that I can't update the header in firefox.
      I wanted to perform test by displaying the xml string in the browser.
      firefox try to display it as an html page and display nothing.

      Is it possible to overide somehow the content type returned?

      Thanks

      Comment


      • #4
        Sorry, I don't understand what you are asking. Are you saying that you are now trying to use this for real, so sending the request by typing the URL into the address bar in Firefox?

        If that is correct then I think the issue is that you can't specify the Accept header that way and by default FF gives you
        "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8". I suspect that this causes a different HttpMessageConverter to be selected ie you aren't getting XML back.
        Try doing a View Source and see what is there, or use "Page Info"/Firebug/HttpFox to see what the browser is actually getting back.

        If you are feeling really brave, you could go to about:config and change "network.http.accept.default" to whatever you want e.g. "application/xml" and see if that changes the behaviour. Don't forget to reset it afterwards!

        Comment


        • #5
          Test failed

          Hi

          There is an error in the junit test.
          they does not work.

          I forget to remove the response.setContentType(...) in the controller.
          So the request.addHeader("Accept", "application/xml") have no use.

          how to configure the HttpMessageConverter to have the contentType setted to application/xml

          Comment


          • #6
            I tried this but the response content type is still null

            Code:
            <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
            		<property name="messageConverters">
            			<util:list>
            				<bean class="org.springframework.http.converter.StringHttpMessageConverter">
            					<property name="supportedMediaTypes">
            						<util:list>
            							<bean class="org.springframework.http.MediaType">
            								<constructor-arg value="application"/>
            								<constructor-arg value="xml"/>
            							</bean>
            						</util:list>
            					</property>
            				</bean>
            			</util:list>
            		</property>
            	</bean>

            Comment


            • #7
              Solution

              Hi

              with request.addHeader("Accept", "application/xml");
              you get response.getHeader("Content-Type") == "application/xml"

              not response.getContentType() == "application/xml"

              Hope that can help somebody
              Regards

              Comment

              Working...
              X