- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 OffsetDateTime 对象。
我想以 ISO 格式输出这种类型,所以我已将上述属性添加到我的 application.yml 中,当我在 Controller 中使用它时它工作正常。
@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"
}
HttpEntity<Schedule> httpEntity = new HttpEntity<>(bpTaskManagerRequest, headers);
ResponseEntity<String> answer = restTemplate.exchange(bpTaskManagerURL, HttpMethod.POST, httpEntity,
String.class);
{
"time": 1510498985.171000000,
"mode": "eSetTime"
}
@Autowired
private RestTemplate restTemplate;
spring:
jackson:
serialization:
write-dates-as-timestamps: false
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')
最佳答案
您的应用程序正在创建自己的 RestTemplate
bean 并且不对它应用任何定制。这意味着它将使用默认消息转换器和默认 Jackson 配置,而不是 Spring Boot 配置的任何内容。
如 described in the reference documentation , Spring Boot 提供了 RestTemplateBuilder
可用于创建 RestTemplate
.它“将确保将合理的 HttpMessageConverters
应用于 RestTemplate
实例”。您可以通过更改 WebConfiguration
来更新您的示例以使用它到以下几点:
package com.example.demo;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
import org.springframework.boot.web.client.RestTemplateBuilder;
@Configuration
public class WebConfiguration {
@Bean
public RestTemplate getRestTemplate(RestTemplateBuilder builder) {
return builder.build();
}
}
2017-11-17 12:35:02.892 INFO 28527 --- [nio-8080-exec-2] com.example.demo.ExampleController : Rest template: {"label":"test from controller","time":"2017-11-17T12:35:02.821Z"}
2017-11-17 12:35:02.905 INFO 28527 --- [nio-8080-exec-1] com.example.demo.DemoService : Object mapper:{"label":"test from controller","time":"2017-11-17T12:35:02.821Z"}
关于json - SpringBoot RestTemplate 忽略 spring.jackson.serialization.WRITE_DATES_AS_TIMESTAMPS = false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47222956/
经过大量搜索,我找到了在我的@RestController 中转换为 JSON 响应时如何阻止 java.util.Date 字段被序列化为时间戳的方法。 但是我无法让它工作。我发现的所有帖子都说禁用
我正在使用 OffsetDateTime 对象。 我想以 ISO 格式输出这种类型,所以我已将上述属性添加到我的 application.yml 中,当我在 Controller 中使用它时它工作正常
我设置了spring.jackson.serialization.WRITE_DATES_AS_TIMESTAMPS=false在 Spring Boot 配置中,但 Jackson 序列化程序仍然生
我是一名优秀的程序员,十分优秀!