Uploaded image for project: 'Spring Framework'
  1. Spring Framework
  2. SPR-13600

Add MediaType.APPLICATION_JSON_UTF8 and improve documentation

    Details

    • Type: Task
    • Status: Closed
    • Priority: Trivial
    • Resolution: Complete
    • Affects Version/s: 4.2 GA
    • Fix Version/s: 4.2.3
    • Component/s: Web
    • Labels:
      None
    • Last commented by a User:
      true

      Description

      Hello

      I have the following declared:

      @JsonPropertyOrder(value={"id","nombre","apellido","fecha"})
      public class PersonaJson implements Serializable {
      	
      	private static final long serialVersionUID = 7212492147213027538L;
      	
      	private String id;
      	private String nombre;
      	private String apellido;
      	
      	private Date fecha;
       
      	public PersonaJson(){
       
      	}
       
      	@NotNull(message="{field.null}")
      	@JsonProperty(value="id")
      	public String getId() {
      		return id;
      	}
      	public void setId(String id) {
      		this.id = id;
      	}
      	
      	@NotNull(message="{field.null}")
      	@Size(min=5, max=20, message="{persona.nombre.size}")
      	@JsonProperty(value="nombre")
      	public String getNombre() {
      		return nombre;
      	}
      

      With:

       @Bean
          public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter(){
          	MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
          	converter.setPrettyPrint(true);		
          	return converter;
          }
      

      And with

      @EnableWebMvc
      @Configuration
      public class WebMvcConfig extends WebMvcConfigurerAdapter {
       
             @Autowired
      	private HttpMessageConverterConfig httpMessageConverterConfig;
      ….
       
              @Override
      	public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
      	    converters.add(httpMessageConverterConfig.mappingJackson2HttpMessageConverter());
      	}
      
      

      The data generated from the server is declared in this way (testing purpose)

      public static PersonaJson crearPersonaMix(){
      		PersonaJson persona = new PersonaJson();
      		persona.setId("100");
      		persona.setNombre("Jesús Você");
      		persona.setApellido("Mão Nuñez");
      		persona.setFecha(new Date());
      		return persona;
      	}
      

      Note: Observe these characters ú ê ã ñ

      I have two @Controller's

      For One

      @Controller
      @RequestMapping(value="….", method=RequestMethod.GET)
      public class PersonaJsonFindOneController {
       
      @RequestMapping(value="….")
      	public @ResponseBody PersonaJson findOneRequestParamById(@RequestParam String id){
      		logger.info("findOneRequestParamById called...");
      		return personas.get(id);
      	}
      
      

      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

      @Controller
      @RequestMapping(value="...", 
      			    method=RequestMethod.GET, 
      			    produces=MediaType.APPLICATION_JSON_VALUE)
      public class PersonaJsonFindOneWithProducesController
       
      @RequestMapping(value="….")
      	public @ResponseBody PersonaJson findOneRequestParamById(@RequestParam String id){
      		logger.info("findOneRequestParamById called...");
      		return personas.get(id);
      	}
      
      

      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:

      • JSONView same creator for FF now 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

      • If @Controller does not use produces=MediaType.APPLICATION_JSON_VALUE all works fine
      • If @Controller does use produces=MediaType.APPLICATION_JSON_VALUE
        • Only works on FF with JSONView, fails in the others Web Browser, with an equivalent add-on.

      Therefore 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.

      1. 01.png
        14 kB
      2. 02.png
        15 kB

        Issue Links

          Activity

          Hide
          sdeleuze Sébastien Deleuze added a comment -

          Some additional information about this behavior.

          application/json entry in IANA registry does not define a charset parameter ("No charset parameter is defined for this registration. Adding one really has no effect on compliant recipients."), and JSON encoding can be UTF-8, UTF-16, or UTF-32 (these can be auto-detected) with UTF-8 likely to be the default in most implementations. And I have found this Chrome bug that seems to match with the behavior describe in this issue.

          For XML, I think UTF-8 is just the default encoding.

          So the fact that this "charset=UTF-8" parameter is needed may come from some browsers not implementing correctly the spec ...

          Show
          sdeleuze Sébastien Deleuze added a comment - Some additional information about this behavior. application/json entry in IANA registry does not define a charset parameter ("No charset parameter is defined for this registration. Adding one really has no effect on compliant recipients."), and JSON encoding can be UTF-8 , UTF-16 , or UTF-32 (these can be auto-detected) with UTF-8 likely to be the default in most implementations. And I have found this Chrome bug that seems to match with the behavior describe in this issue. For XML, I think UTF-8 is just the default encoding. So the fact that this "charset=UTF-8" parameter is needed may come from some browsers not implementing correctly the spec ...
          Hide
          dr_pompeii Manuel Jordan added a comment -

          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 @Controller I use produces=MediaType.APPLICATION_XML_VALUE. And all work fine. Weird characters appears without any problem in xml format.

          Now thinking, wondered by there does not appears the overrding effect for XML how JSON has.

          -Manuel

          Show
          dr_pompeii Manuel Jordan added a comment - 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 @Controller I use produces=MediaType.APPLICATION_XML_VALUE . And all work fine. Weird characters appears without any problem in xml format. Now thinking, wondered by there does not appears the overrding effect for XML how JSON has. -Manuel
          Hide
          sdeleuze Sébastien Deleuze added a comment -

          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 application/json;charset=UTF-8 content type for a long time. After discussing with Rossen Stoyanchev, and as you suggested in one of your comments, I have added MediaType.APPLICATION_JSON_UTF8 and MediaType.APPLICATION_JSON_UTF8_VALUE constants to make it even easier to keep the encoding while overriding RequestMapping "produces" attribute, see this commit that will be part of Spring Framework 4.2.3.RELEASE.

          Show
          sdeleuze Sébastien Deleuze added a comment - 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 application/json;charset=UTF-8 content type for a long time. After discussing with Rossen Stoyanchev , and as you suggested in one of your comments, I have added MediaType.APPLICATION_JSON_UTF8 and MediaType.APPLICATION_JSON_UTF8_VALUE constants to make it even easier to keep the encoding while overriding RequestMapping "produces" attribute, see this commit that will be part of Spring Framework 4.2.3.RELEASE.
          Hide
          dr_pompeii Manuel Jordan added a comment - - edited

          Hello

          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.

          Yes, has sense. Even when Jackson reuses the JAXB 2 annotations to work around JSON. Of course, other history….

          I have added MediaType.APPLICATION_JSON_UTF8 and MediaType.APPLICATION_JSON_UTF8_VALUE constants to make it even easier to keep the encoding while overriding RequestMapping "produces" attribute

          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

          Show
          dr_pompeii Manuel Jordan added a comment - - edited Hello 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. Yes, has sense. Even when Jackson reuses the JAXB 2 annotations to work around JSON. Of course, other history…. I have added MediaType.APPLICATION_JSON_UTF8 and MediaType.APPLICATION_JSON_UTF8_VALUE constants to make it even easier to keep the encoding while overriding RequestMapping "produces" attribute 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
          Hide
          sdeleuze Sébastien Deleuze added a comment -

          You're welcome. I have already updated the reference documentation accordingly.

          Show
          sdeleuze Sébastien Deleuze added a comment - You're welcome. I have already updated the reference documentation accordingly.

            People

            • Assignee:
              sdeleuze Sébastien Deleuze
              Reporter:
              dr_pompeii Manuel Jordan
              Last updater:
              Sébastien Deleuze
            • Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:
                Days since last comment:
                2 years, 16 weeks, 4 days ago