【发布时间】:2018-04-23 16:38:03
【问题描述】:
我正在使用 OffsetDateTime 对象。
我想以 ISO 格式输出这种类型,所以我将上述属性添加到我的 application.yml 中,当我在控制器中使用它时它工作正常。
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Schedule
{
private OffsetDateTime time;
private String mode;
}
在我的控制器中使用:
public ResponseEntity taskManagerTest() {
Schedule bpTaskManagerRequest = new Schedule();
return ResponseEntity.status(HttpStatus.CREATED).headers(null).body(bpTaskManagerRequest);
}
返回对象时的示例结果:
{
"time": "2017-11-12T15:03:05.171Z",
"mode": "eSetTime"
}
但是,如果我在 Spring 服务中使用相同的对象使用 RestTemplate 进一步发送它:
HttpEntity<Schedule> httpEntity = new HttpEntity<>(bpTaskManagerRequest, headers);
ResponseEntity<String> answer = restTemplate.exchange(bpTaskManagerURL, HttpMethod.POST, httpEntity,
String.class);
序列化为:
{
"time": 1510498985.171000000,
"mode": "eSetTime"
}
我的 RestTemplate 定义为:
@Autowired
private RestTemplate restTemplate;
application.yml sn-p:
spring:
jackson:
serialization:
write-dates-as-timestamps: false
build.gradle sn-p:
buildscript {
ext {
springBootVersion = '1.5.4.RELEASE'
ext.kotlin_version = '1.1.51'
}
}
compile('com.fasterxml.jackson.module:jackson-module-parameter-names')
compile('com.fasterxml.jackson.datatype:jackson-datatype-jdk8')
compile('com.fasterxml.jackson.datatype:jackson-datatype-jsr310')
示例项目:https://github.com/deepres/OffsetDateTime-with-RestTemplate
【问题讨论】:
-
能否请您发布您的 application.yml 和其他相关配置?
-
我提供了更多信息
-
您的样本不完整,因此很难判断发生了什么。您能否分享一个完整的示例,显示接收 REST 模板发出的请求的代码以及您如何确定请求是如何被序列化的?
-
@AndyWilkinson 这是示例项目的链接:github.com/deepres/OffsetDateTime-with-RestTemplate
标签: json spring-mvc spring-boot jackson