Java 8 LocalDate Jackson format

前端 未结 14 1052
傲寒
傲寒 2020-11-22 12:59

For java.util.Date when I do

@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = \"dd/MM/yyyy\")  
  private Date dateOfBirth;
<         


        
14条回答
  •  无人及你
    2020-11-22 13:34

    Just an update of Christopher answer.

    Since the version 2.6.0

    
        com.fasterxml.jackson.datatype
        jackson-datatype-jsr310
        2.9.0
    
    

    Use the JavaTimeModule instead of JSR310Module (deprecated).

    @Provider
    public class ObjectMapperContextResolver implements ContextResolver {  
        private final ObjectMapper MAPPER;
    
        public ObjectMapperContextResolver() {
            MAPPER = new ObjectMapper();
            MAPPER.registerModule(new JavaTimeModule());
            MAPPER.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
        }
    
        @Override
        public ObjectMapper getContext(Class type) {
            return MAPPER;
        }  
    }
    

    According to the documentation, the new JavaTimeModule uses same standard settings to default to serialization that does NOT use Timezone Ids, and instead only uses ISO-8601 compliant Timezone offsets.

    Behavior may be changed using SerializationFeature.WRITE_DATES_WITH_ZONE_ID

提交回复
热议问题