英文:
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 ,我使用以下依赖项,
```
我使用以下配置,
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 = "YYYY-MM-dd HH:mm", 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'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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论