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.

        Attachments

          Issue Links

            Activity

              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, 29 weeks ago