spring-projects / spring-framework

Spring Framework
https://spring.io/projects/spring-framework
Apache License 2.0
56.8k stars 38.17k forks source link

Why Content type is null when produces=MediaType.TEXT_HTML_VALUE is used [SPR-13823] #18396

Closed spring-projects-issues closed 8 years ago

spring-projects-issues commented 8 years ago

Manuel Jordan opened SPR-13823 and commented

Hello

This post mostly how a consult about internal work of Spring Framework about Spring MVC.

If I have a @RequestMapping method with produces for XML and/or JSON, thanks to Spring MVC Test with the print() method I can confirm the following:

ModelAndView:
        View name = null
             View = null
            Model = null

FlashMap:
       Attributes = null

MockHttpServletResponse:
           Status = 200
    Error message = null
          Headers = {Content-Type=[application/xml]}
     Content type = application/xml
             Body = <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<persona>
    <id>100</id>
    <nombre>Something</nombre>
    <apellido>Something</apellido>
    <fecha>1977-12-08T00:00:00-05:00</fecha>
</persona>

    Forwarded URL = null
   Redirected URL = null
          Cookies = []

I can see ModelAndView empty and MockHttpServletResponse with Content type = application/xml and it is expected. Until here all is normal.

But if I have other @RequestMapping with produces=MediaType.TEXT_HTML_VALUE and of course returning a String (view-name) and Model. Again with print() method now I can see the following:

ModelAndView:
        View name = persona/findOne
             View = null
        Attribute = persona
            value = Persona [id=100, nombre=Jesús Você, apellido=Mão Nuñez, fecha=Thu Dec 08 00:00:00 PET 1977]
           errors = []

FlashMap:
       Attributes = null

MockHttpServletResponse:
           Status = 200
    Error message = null
          Headers = {}
     Content type = null
             Body = 
    Forwarded URL = /WEB-INF/view/jsp/persona/findOne.jsp
   Redirected URL = null
          Cookies = []

For me all practically have sense, but I am with the doubt about why Content type is null and not text/html?. it mostly according with the produces=MediaType.TEXT_HTML_VALUE established.

Since I am returning a model I am ok that the Body is empty and value has the object Persona, but wondered in some way why Content type is null.

I did realise about this behaviour when in two different test methods (through Spock) I have:

(1) I ask for the explanation of this behaviour (2) What could be the correct way to test or check if the content is text/html

Note: consider your answer to be included in Spring Reference documentation. (Of course, if has sense)

Thanks in advance.


Affects: 4.2 GA, 4.2.4

spring-projects-issues commented 8 years ago

Sam Brannen commented

Generally speaking, whenever you have a question, please follow the guidelines laid out in CONTRIBUTING.md.

Having said that, ...

Since the View in question is a JstlView (i.e., for a JSP), the body is never rendered.

Note that javax.servlet.ServletResponse.setContentType(String) is only invoked if the body is actually rendered. Thus, for a JSP you can only assert the forwardedUrl; you cannot assert the content type in the response.

spring-projects-issues commented 8 years ago

Manuel Jordan commented

Thanks by the explanation

And you are right. But in this case I thought than an answer from the source would be more accurate.

Thanks by your understanding.