Changing Default JSON Time Format with RESTEasy 3.x

前端 未结 3 876
梦谈多话
梦谈多话 2021-02-13 22:35

I am using RESTEasy to implement a REST Service using JSON serialization. Currently, Dates are getting serialized to milliseconds since 1970. To improve compatibility, I would

3条回答
  •  温柔的废话
    2021-02-13 23:37

    Using with the JSR310 (new api date) - LocalDate, LocalDateTime, LocalTime

    Add dependence:

     
         com.fasterxml.jackson.datatype 
         jackson-datatype-jsr310 
         2.4.0 
    
    

    And create a provider to register the module:

    @Provider
    public class JacksonConfig implements ContextResolver {
    private final ObjectMapper objectMapper;
    
    public JacksonConfig() throws Exception {
    
        objectMapper = new ObjectMapper()
                    .disable( SerializationFeature.WRITE_DATES_AS_TIMESTAMPS )
                    .disable( SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS )
                    .setSerializationInclusion( JsonInclude.Include.NON_NULL )
                    .registerModule( new JSR310Module() );
    
    }
    
    @Override
    public ObjectMapper getContext( Class arg0 ) {
        return objectMapper;
    } }
    

提交回复
热议问题