[英]Timezone of ZonedDateTime changed to UTC during auto conversion of RequestBody with Spring Boot
I'm trying to keep ZoneId of ZonedDateTime which is set on front-end while performing POST/PUT to Spring Boot controller.我试图在对 Spring Boot 控制器执行 POST/PUT 时保留在前端设置的 ZonedDateTime 的 ZoneId。
The value I want to transfer is: 2019-05-01T00:00:00+01:00[Europe/Zagreb]
我要转移的值是:
2019-05-01T00:00:00+01:00[Europe/Zagreb]
After POST/PUT the ZoneId is converted to UTC and hours are adjusted.在 POST/PUT 之后,ZoneId 被转换为 UTC 并且小时数被调整。 Technically this updated value represents the same point on time line, but the original ZoneId is lost and I would like to have it stored to be able to show it back later to end user.
从技术上讲,这个更新后的值代表时间线上的同一点,但原始 ZoneId 丢失了,我希望将其存储起来以便稍后能够将其显示给最终用户。
// DTO
public class PriceInfoDTO {
@JsonFormat( pattern = "yyyy-MM-dd'T'HH:mm:ssXXX['['VV']']",
with = JsonFormat.Feature.WRITE_DATES_WITH_ZONE_ID )
@DateTimeFormat( pattern = "yyyy-MM-dd'T'HH:mm:ssXXX['['VV']']", iso = ISO.DATE_TIME )
private ZonedDateTime validFrom;
}
// Controller
@PutMapping(
path = PATH + "/{id}",
consumes = MediaType.APPLICATION_JSON_VALUE,
produces = MediaType.APPLICATION_JSON_VALUE
)
public ResponseEntity<PriceInfo> update(
@PathVariable("id") final Integer id,
@RequestBody final PriceInfoDTO dto
) {
System.out.println(dto);
...
}
Looking at Network tab in my browser, the request from browser to Spring Controller has this value (payload):查看我浏览器中的网络选项卡,从浏览器到 Spring Controller 的请求具有以下值(有效负载):
2019-05-01T00:00:00+01:00[Europe/Zagreb]
which is the same as format pattern.这与格式模式相同。
When I dump DTO to console, I get this result:当我将 DTO 转储到控制台时,我得到以下结果:
2019-04-30T22:00Z[UTC]
Is there any way to preserve ZoneId as it was received in a request?有什么方法可以保留在请求中收到的 ZoneId 吗? Should I write my own Serializer and Deserializer to achieve this?
我应该编写自己的序列化器和反序列化器来实现这一点吗?
Thanks!谢谢!
Add the following line to the application.properties
file:将以下行添加到
application.properties
文件:
spring.jackson.deserialization.ADJUST_DATES_TO_CONTEXT_TIME_ZONE = false
References:参考:
Can also be set programmatically with ObjectMapper
:也可以使用
ObjectMapper
以编程方式设置:
objectMapper.configure(SerializationFeature.WRITE_DATES_WITH_ZONE_ID, true);
objectMapper.configure(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE, false);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.