SpringBoot Unified Date Format for Json Data Processing

SpringBoot Unified Date Format for Json Data Processing

1. Pseudo-code examples

User.java

@Getter
@Setter
@AllArgsConstructor
public class User {

	private Long id;
	private String name;
  	private Date birthday;
}

UserController.java

@RestController
public class UserController {

  @GetMapping("/user")
  public User getUser(){
    return new User(18L, "Zhang San", new Date());
  }
}

2. Return results

  • 2.1 When we access the service by default without any processing, the format returned is:
    {"id":18,"name":"Zhang San","birthday":"2019-09-25T08:22:31.287+0000"}
    
  • 2.2 Usually we use the @JsonFormat formatting method on the date type field.
    @JsonFormat(pattern = "yyyy-MM-dd")
    private Date birthday;
    
    {"id":18,"name":"Zhang San","birthday":"2019-09-25"}
    
  • 2.3 The problem is that if there are multiple date fields in multiple bean s, it will be very troublesome and difficult to achieve uniformity, and once the format is changed, it needs to be modified one by one.

2. Unified processing using springboot

  • Because springboot automatically configures Jackson and Gson, you can go through the following paths. springboot defaults to jackson, if users want to use gson, add dependencies directly.

    org.springframework.boot.autoconfigure.http.JacksonHttpMessageConvertersConfiguration
    org.springframework.boot.autoconfigure.http.GsonHttpMessageConvertersConfiguration
    
  • Enter the Jackson HttpMessageConverters Configuration configuration class

    @Bean
    	@ConditionalOnMissingBean(value = MappingJackson2HttpMessageConverter.class,
    			ignoredType = { "org.springframework.hateoas.mvc.TypeConstrainedMappingJackson2HttpMessageConverter",
    					"org.springframework.data.rest.webmvc.alps.AlpsJsonHttpMessageConverter" })
    	public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter(ObjectMapper objectMapper) {
    		return new MappingJackson2HttpMessageConverter(objectMapper);
    	}
    
  • From the @ConditionOnMissingBean, we can customize a configuration to override the message converter

  • Way 1: Redefine Mapping Jackson 2HttpMessageConverter

    @Configuration
    public class WebMvcConfig {
     	@Bean
    	MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter(){
    	MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
    	ObjectMapper mapper = new ObjectMapper();
    	mapper.setDateFormat(new SimpleDateFormat("yyyy year MM month dd day HH Time mm branch ss second"));
    	converter.setObjectMapper(mapper);
    	return converter;
    }
    
  • Mode 2: Redefining ObjectMapper

    public class WebMvcConfig {
    	@Bean
    	ObjectMapper jacksonObjectMapper(){
     		ObjectMapper mapper = new ObjectMapper();
    		mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd"));
    		return mapper;
     }
    }
    

    Because as soon as we know by way, the real format modification is achieved through the ObjectMapper class. We can enter the automatic configuration class.
    View: org. spring framework. boot. autoconfigure. jackson. Jackson AutoConfiguration

  • critical code

    @Bean
    @Primary
    @ConditionalOnMissingBean
    public ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder) {
    		return builder.createXmlMapper(false).build();
    }
    
    

Finally, if mode one and mode two are used simultaneously, is that okay? The answer is yes, with a priority:
At the same time, Mapping Jackson 2HttpMessageConverter has higher priority than ObjectMapper.

Keywords: SpringBoot Java JSON REST

Added by ottoman_ on Wed, 25 Sep 2019 12:54:11 +0300