FasterXML对象映射字符串到LocalDateTime问题

huangapple go评论79阅读模式
英文:

FasterXML Object Mapping String to LocalDateTime Issue

问题

以下是您要翻译的内容:

我有如下字符串,

value = {"name":"John","timeStamp":"2020-08-11T13:31:31"}


我的Pojo类如下,

@Getter
@ToString
@AllArgsConstructor
@NoArgsConstructor
class Person{

private String name;	

@JsonFormat(pattern = "YYYY-MM-dd HH:mm", shape = JsonFormat.Shape.STRING)
@JsonSerialize(using = LocalDateTimeSerializer.class)
private LocalDateTime timeStamp; 

}


根据 https://github.com/FasterXML/jackson-modules-java8 ,我使用以下依赖项,


com.fasterxml.jackson.module
jackson-module-parameter-names
2.11.1


com.fasterxml.jackson.datatype
jackson-datatype-jdk8
2.11.1


com.fasterxml.jackson.datatype
jackson-datatype-jsr310
2.11.1

```

我使用以下配置,

 final ObjectMapper objectMapper = JsonMapper.builder()
                        .addModule(new ParameterNamesModule())
                        .addModule(new Jdk8Module())
                        .addModule(new JavaTimeModule())
                        .build();

objectMapper.readValue(value, Person.class)

我得到以下异常,

Caused by: java.io.NotSerializableException: java.time.format.DateTimeFormatter

我也尝试了以下在timeStamp中,但没有运气

 @JsonFormat(pattern = "yyy-MM-ddThh:mm:ss")

<details>
<summary>英文:</summary>

I have String as below,

value = {"name":"John","timeStamp":"2020-08-11T13:31:31"}


My Pojo class is,

@Getter
@ToString
@AllArgsConstructor
@NoArgsConstructor
class Person{

private String name;	

@JsonFormat(pattern = &quot;YYYY-MM-dd HH:mm&quot;, shape = JsonFormat.Shape.STRING)
@JsonSerialize(using = LocalDateTimeSerializer.class)
private LocalDateTime timeStamp; 

}


I use the below artifact, as per https://github.com/FasterXML/jackson-modules-java8

<dependency>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-parameter-names</artifactId>
<version>2.11.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jdk8</artifactId>
<version>2.11.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<version>2.11.1</version>
</dependency>


I use the below config,

final ObjectMapper objectMapper = JsonMapper.builder()
.addModule(new ParameterNamesModule())
.addModule(new Jdk8Module())
.addModule(new JavaTimeModule())
.build();

objectMapper.readValue(value, Person.class)

I&#39;m getting the below exception,

Caused by: java.io.NotSerializableException: java.time.format.DateTimeFormatter

I tried with the below in the timeStamp also, but no luck

@JsonFormat(pattern = "yyy-MM-ddThh:mm:ss")



</details>


# 答案1
**得分**: 1

The ISO format you're showing is the default so no need to get complicated and it should work!

```java
class Person {
    private String name;
    private LocalDateTime timeStamp;
}

PS. suggest you use jackson-bom managed dependencies to avoid 2.11.1 appearing all over the place. See https://stackoverflow.com/questions/14874966/how-to-use-bom-file-with-maven for a BOM example.

英文:

The ISO format you're showing is the default so no need to get complicated and it should work!

class Person {
    private String name;    
    private LocalDateTime timeStamp; 
}

PS. suggest you use jackson-bom managed dependencies to avoid 2.11.1 appearing all over the place. See https://stackoverflow.com/questions/14874966/how-to-use-bom-file-with-maven for a BOM example.

huangapple
  • 本文由 发表于 2020年8月11日 20:19:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/63357981.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定