dam*_*ian 5 java jackson java-8 spring-boot
我的实体类中有一个LocalDateTime属性,但是当它被序列化时,我看不到预期的格式.
这是班级:
public class MyEntity {
private Integer id;
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss'Z'")
private LocalDateTime changeDate;
// getters and setters
}
Run Code Online (Sandbox Code Playgroud)
这就是杰克逊格式化的方式:
{
"id": 56,
"changeDate": {
"hour":14, "minute":19,
"nano":797000000,
"second":7,
"dayOfMonth":24,
"dayOfWeek":"TUESDAY",
"dayOfYear":297, "month":"OCTOBER",
"monthValue":10,
"year":2017,
"chronology": {
"id":"ISO",
"calendarType":"iso8601"
}
}
}
Run Code Online (Sandbox Code Playgroud)
请注意,我将以下依赖项添加到我的pom:
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
</dependency>
Run Code Online (Sandbox Code Playgroud)
我没有包含该版本,因为spring boot负责这一点.顺便说一句,我正在使用春季启动1.5.2.RELEASE.
我还在application.properties中包含了以下属性:
spring.jackson.serialization.WRITE_DATES_AS_TIMESTAMPS = false
Run Code Online (Sandbox Code Playgroud)
知道为什么日期没有格式化,而不是使用我提供的模式?
我尝试重现你的例子并且效果很好。
MyEntity
public class MyEntity {
private Integer id;
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss'Z'")
private LocalDateTime time;
public MyEntity(Integer id, LocalDateTime time) {
this.id = id;
this.time = time;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public LocalDateTime getTime() {
return time;
}
public void setTime(LocalDateTime time) {
this.time = time;
}
}
Run Code Online (Sandbox Code Playgroud)
LocalDateTimeApplication
@SpringBootApplication
public class LocalDateTimeApplication {
public static void main(String[] args) {
SpringApplication.run(LocalDateTimeApplication.class, args);
}
}
Run Code Online (Sandbox Code Playgroud)
SomeController
@RestController
@RequestMapping("/SomeController")
public class SomeController {
@RequestMapping(method = RequestMethod.GET)
public ResponseEntity<Object> getMyEntity() {
MyEntity entity = new MyEntity(1, LocalDateTime.now());
return new ResponseEntity<Object>(entity, HttpStatus.OK);
}
}
Run Code Online (Sandbox Code Playgroud)
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.so.example</groupId>
<artifactId>localDateTime</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>localDateTime</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.8.RELEASE</version>
<relativePath/>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>RELEASE</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3834 次 |
| 最近记录: |