-
Notifications
You must be signed in to change notification settings - Fork 38.5k
Add MediaType.APPLICATION_JSON_UTF8 and improve documentation [SPR-13600] #18178
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Sébastien Deleuze commented Hi Manuel Jordan, If you specify I am going to update the Javadoc to document that the default charset is |
Manuel Jordan commented Hello Sébastien Deleuze Thanks a lot by the reply. It works, but I am confused About this:
With the best intentions, do a detailed expansion (explanation) for above due the following (links to Github code for each class): MappingJackson2HttpMessageConverter is a subclass of AbstractJackson2HttpMessageConverter The super class has the following declared: Line 63: public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8"); And in the subclass has the following: Lines 63-68: /**
* Construct a new {@link MappingJackson2HttpMessageConverter} with a custom {@link ObjectMapper}.
* You can use {@link Jackson2ObjectMapperBuilder} to build it easily.
* @see Jackson2ObjectMapperBuilder#json()
*/
public MappingJackson2HttpMessageConverter(ObjectMapper objectMapper) {
super(objectMapper, new MediaType("application", "json", DEFAULT_CHARSET),
new MediaType("application", "*+json", DEFAULT_CHARSET));
} Note: unique place where that DEFAULT_CHARSET is used in that class. And how you can see, practically there is located your new MediaType("application", "json", Charset.forName("UTF-8") So me question is (in bold soon) If I am using in my code the MappingJackson2HttpMessageConverter's no-args constructor @Bean
public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter(){
MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
converter.setPrettyPrint(true);
return converter;
} where in the MappingJackson2HttpMessageConverter's no-args constructor source code we have Lines 52-58: /**
* Construct a new {@link MappingJackson2HttpMessageConverter} using default configuration
* provided by {@link Jackson2ObjectMapperBuilder}.
*/
public MappingJackson2HttpMessageConverter() {
this(Jackson2ObjectMapperBuilder.json().build());
} which calls the other MappingJackson2HttpMessageConverter's args constructor (this()), where DEFAULT_CHARSET is used, why fails my code?. I am very confused… Pls expand about this too:
Now, like I said, your solution works: @Controller
@RequestMapping(value=PersonaUrlJsonHelperComplement.ROOT_URL_PRODUCES_JSON,
method=RequestMethod.GET,
//produces=MediaType.APPLICATION_JSON_VALUE
produces = "application/json; charset=UTF-8") Now, even when it works (thanks again by your valuable support) I don't want use static or raw String. Is possible have something like this: @Controller
@RequestMapping(value=PersonaUrlJsonHelperComplement.ROOT_URL_PRODUCES_JSON,
method=RequestMethod.GET,
produces=MediaType.APPLICATION_JSON_VALUE
charset=SomethingNewClass.UTF-8) Thanks a lot in advance. For me I would be happy to work with the current: @Controller
@RequestMapping(value=PersonaUrlJsonHelperComplement.ROOT_URL_PRODUCES_JSON,
method=RequestMethod.GET,
produces=MediaType.APPLICATION_JSON_VALUE
) and have the charset=UTF-8" already working by default behind the scenes… With the best intentions: please share your best explanation and share it in the Reference documentation. Because It would be problematic for other users/developers. Thanks again |
Sébastien Deleuze commented You code is "failing" because your are overriding the default Since the charset information is already included in the @RequestMapping(value = "...", method = RequestMethod.GET, produces = MyClass.APPLICATION_JSON_UTF_8) With public class MyClass {
public static final String APPLICATION_JSON_UTF_8 = "application/json; charset=UTF-8";
} I agree that it is easy to override unexpectedly the media type charset. That said, I think changing the current behavior even slightly could cause some regression, so I would like to avoid it especially in a minor revision. In addition to the Javadoc improvements already commited, my proposal would be to improve the reference documentation "Producible Media Types" section by using Does it sound like a reasonable improvement? |
Manuel Jordan commented Hello Again thanks by your support.
it about public MappingJackson2HttpMessageConverter(ObjectMapper objectMapper) {
super(objectMapper, new MediaType("application", "json", DEFAULT_CHARSET),
new MediaType("application", "*+json", DEFAULT_CHARSET));
} Am I correct?
Yes, it about @RequestMapping(value="/something",
method=RequestMethod.GET,
produces=MediaType.APPLICATION_JSON_VALUE
) Until here: Alpha: - that situation would be added to the reference documentation? About
What do you mean with that? It has been overridden … I am confused... About public class MyClass {
public static final String APPLICATION_JSON_UTF_8 = "application/json; charset=UTF-8";
} Yes has sense. That's what I thought. But why not handle from the source (Spring) see below. Beta /**
* A String equivalent of {@link MediaType#APPLICATION_JSON}.
*/
public final static String APPLICATION_JSON_VALUE = "application/json"; so, why not change to public final static String APPLICATION_JSON_VALUE = "application/json; charset=UTF-8"; With that the overriding effect disappears. Delta: Even if Beta is not viable for some reason (give me details of that reason please) why not create a new attribute named charset for the @Controller
@RequestMapping(value=PersonaUrlJsonHelperComplement.ROOT_URL_PRODUCES_JSON,
method=RequestMethod.GET,
produces=MediaType.APPLICATION_JSON_VALUE
charset=SomethingNewClass.UTF-8) According with my understanding UTF-8 is the best recommended value about encoding. So it should be used always…
Yes, I did realize about this "weird" behaviour just doing my own experiments.
(1) What do you mean with regression? What could be the problem?
Pls, could you share that link? I want to see it.
Yes, has sense. I am agree. I think is wise explain why that "charset=UTF-8" is added. Perfect place to share the Alpha behaviour. Just curious if Beta fits how the solution here. If Beta is approved then Alpha should be not exists in the reference documentation.
Your note is good, but does not indicate the problem about Alpha (the overriding effect). Omicron So thinking like reader (according with your current note) produces="application/json; charset=UTF-8" if MappingJackson2HttpMessageConverter already does that I hope you see my point here.
Yes, has sense. But consider Omicron. Kind Regards -Manuel |
Sébastien Deleuze commented Alpha What I mean is that a Beta That would be a breaking change, and we want to avoid it. Delta I understand the idea behind adding a Omicron You are likely to declare |
Sébastien Deleuze commented Rossen Stoyanchev Brian Clozel Are you ok with the documentation improvements contained in this commit for our upcoming 4.2.3 release? Do you think it is worth creating another issue in 4.3 or 5.0 backlog to discuss what we could change to make it more easy to specify a |
Juergen Hoeller commented Yep, makes sense. Let's document this for the time being and create a follow-up issue for 4.3. Juergen |
Manuel Jordan commented Thanks a lot by your explanation. Now has more sense. I am going to wait the feedback in peace. BTW: This overriding behaviour does not happen with XML (MediaType.APPLICATION_XML_VALUE). Therefore same data using the weird characters are represented through XML in peace. -Manuel |
Sébastien Deleuze commented Manuel Jordan Does your XML document contains a prolog with the encoding specified like |
Sébastien Deleuze commented Documentation has been improved in 4.2.x, and I have created #18209 to try to provide a "better" default behavior in 4.3.x if possible. |
Manuel Jordan commented Hello Sébastien Deleuze
No… When I startup the app through Tomcat I can see the following for example in the Console/Terminal
How you can see "application/xml" In the browser I get the following: This XML file does not appear to have any style information associated with it. The document tree is shown below.
<persona>
<id>100</id>
<nombre>Jesús Você</nombre>
<apellido>Mão Nuñez</apellido>
<fecha>2015-11-02T07:53:53.048-05:00</fecha>
</persona> Therefore the <?xml version="1.0" encoding="UTF-8"?> line does not appear. Let me know If I should look that line in other place. Thanks by create the other Jira Post -Manuel |
Sébastien Deleuze commented Some additional information about this behavior.
For XML, I think So the fact that this |
Manuel Jordan commented Thanks by the valuable information About XML, I have the following: @Bean
public MarshallingHttpMessageConverter marshallingMessageConverter() {
MarshallingHttpMessageConverter converter = new MarshallingHttpMessageConverter();
converter.setMarshaller(jaxbMarshaller());
converter.setUnmarshaller(jaxbMarshaller());
return converter;
}
@Bean
public Jaxb2Marshaller jaxbMarshaller() {
Jaxb2Marshaller jaxbMarshaller = new Jaxb2Marshaller();
jaxbMarshaller.setClassesToBeBound(PersonaXml.class,
PersonaCollection.class,
GenericCollection.class,
PersonaHibrid.class);
Map<String, Object> props = new HashMap<>();
props.put(Marshaller.JAXB_ENCODING,"UTF-8");
props.put(Marshaller.JAXB_FORMATTED_OUTPUT, true);
jaxbMarshaller.setMarshallerProperties(props);
return jaxbMarshaller;
} Where we have "props.put(Marshaller.JAXB_ENCODING,"UTF-8");" In the Now thinking, wondered by there does not appears the overrding effect for XML how JSON has. -Manuel |
Sébastien Deleuze commented I guess the different behaviors between XML and JSON regarding encoding are coming from this "strange" browser Webkit/Blink behavior regarding JSON encoding described here. In any case, we will have to keep using this default |
Manuel Jordan commented Hello
Yes, has sense. Even when Jackson reuses the JAXB 2 annotations to work around JSON. Of course, other history….
Excellent, I've checked the commits.. Thanks a lot!, more easier for us (developers) Being polite: Do not forget add a special note in the Reference Documentation about this situation. I think is wise advice this useful feature for the community… -Manuel |
Sébastien Deleuze commented You're welcome. I have already updated the reference documentation accordingly. |
Uh oh!
There was an error while loading. Please reload this page.
Manuel Jordan opened SPR-13600 and commented
Hello
I have the following declared:
With:
And with
The data generated from the server is declared in this way (testing purpose)
Note: Observe these characters ú ê ã ñ
I have two
@Controller
'sFor One
Observe there is no a produces=MediaType.APPLICATION_JSON_VALUE
When I use the URL in any browser Safari, FF, Chrome, Opera I can see the data and the "weird" characters in peace.
It how 02.png
For Two
Observe now there is a produces=MediaType.APPLICATION_JSON_VALUE
When I use the URL (the other for that controller) in any browser Safari, FF, Chrome, Opera I can't see the data and the "weird" characters in peace. It how 01.png
In FF if I enable JSONView I can see now all ok… It how 02.png
Conclusion: Just thinking I need just install that add-on to each web browser (of course using an equivalent).
For Opera: I just used
For Chrome:
or (not both)
The problem is that for that browsers (non FF) I still get the not desired results, it how 01.png
Therefore
@Controller
does not use produces=MediaType.APPLICATION_JSON_VALUE all works fine@Controller
does use produces=MediaType.APPLICATION_JSON_VALUETherefore I don't know if :
(A) I have any kind of missing configuration in some
@Configuration
/@Bean
(B) I am using the wrong add-ons for the non-FF browsers.
Assuming is (B) I think is wise share the solution to the community in the reference documentation.
Affects: 4.2 GA
Attachments:
Issue Links:
Referenced from: commits 09cb286, 994a11d, 62cd6ad
The text was updated successfully, but these errors were encountered: