Java 8 LocalDate Jackson格式

Java 8 LocalDate Jackson格式

对于java.util.Date,当我这样做

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

然后在我发送的JSON请求中

{ {"dateOfBirth":"01/01/2000"} }

有用。

我应该如何为Java 8的LocalDate字段执行此操作?

我试过了

@JsonDeserialize(using = LocalDateDeserializer.class)  @JsonSerialize(using = LocalDateSerializer.class)  private LocalDate dateOfBirth;

它没用。

有人可以让我知道这是正确的方法吗?

以下是依赖项

<dependency>
         <groupId>org.jboss.resteasy</groupId>
         <artifactId>jaxrs-api</artifactId>
         <version>3.0.9.Final</version>
      </dependency>
      <dependency>
         <groupId>com.fasterxml.jackson.jaxrs</groupId>
         <artifactId>jackson-jaxrs-json-provider</artifactId>
         <version>2.4.2</version>
      </dependency>
      <dependency>
         <groupId>com.wordnik</groupId>
         <artifactId>swagger-annotations</artifactId>
         <version>1.3.10</version>
      </dependency>
      <dependency>


肥皂起泡泡
浏览 488回答 3
3回答

临摹微笑

它们无需导入额外的jsr310模块:@JsonDeserialize(using&nbsp;=&nbsp;LocalDateDeserializer.class)&nbsp;&nbsp;@JsonSerialize(using&nbsp;=&nbsp;LocalDateSerializer.class)&nbsp;&nbsp;private&nbsp;LocalDate&nbsp;dateOfBirth;解串器:public&nbsp;class&nbsp;LocalDateDeserializer&nbsp;extends&nbsp;StdDeserializer<LocalDate>&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;private&nbsp;static&nbsp;final&nbsp;long&nbsp;serialVersionUID&nbsp;=&nbsp;1L; &nbsp;&nbsp;&nbsp;&nbsp;protected&nbsp;LocalDateDeserializer()&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;super(LocalDate.class); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;@Override &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;LocalDate&nbsp;deserialize(JsonParser&nbsp;jp,&nbsp;DeserializationContext&nbsp;ctxt) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;throws&nbsp;IOException,&nbsp;JsonProcessingException&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;LocalDate.parse(jp.readValueAs(String.class)); &nbsp;&nbsp;&nbsp;&nbsp;}}串行:public&nbsp;class&nbsp;LocalDateSerializer&nbsp;extends&nbsp;StdSerializer<LocalDate>&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;private&nbsp;static&nbsp;final&nbsp;long&nbsp;serialVersionUID&nbsp;=&nbsp;1L; &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;LocalDateSerializer(){ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;super(LocalDate.class); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;@Override &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;void&nbsp;serialize(LocalDate&nbsp;value,&nbsp;JsonGenerator&nbsp;gen,&nbsp;SerializerProvider&nbsp;sp)&nbsp;throws&nbsp;IOException,&nbsp;JsonProcessingException&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;gen.writeString(value.format(DateTimeFormatter.ISO_LOCAL_DATE)); &nbsp;&nbsp;&nbsp;&nbsp;}}

富国沪深

&nbsp;&nbsp;&nbsp;ObjectMapper&nbsp;mapper&nbsp;=&nbsp;new&nbsp;ObjectMapper(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mapper.registerModule(new&nbsp;JavaTimeModule()); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS,&nbsp;false);对我来说很好。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java