[英]Spring-boot @RequestParam annotation converts UTC timezone in request to local timezone
I have a spring-boot application with a controller that handles some requests with a date passed as a query parameter. 我有一个带有控制器的spring-boot应用程序,该控制器处理带有日期作为查询参数传递的某些请求。 Eg URI for request:
例如请求的URI:
http://localhost:8081/test/read?from=2018-01-01T00:00:00Z
However, using the @RequestParam annotation to parse the date from the URI, I am getting a date in the local timezone, not the UTC timezone (as indicated by the 'Z'). 但是,使用@RequestParam批注从URI中解析日期,我在本地时区而不是UTC时区(如“ Z”所示)获取日期。
I've tried setting the jdbctemplate time zone, the java.util.timezone and the spring.jackson.time_zone in application.properties, but that didn't help. 我尝试在application.properties中设置jdbctemplate时区,java.util.timezone和spring.jackson.time_zone,但这没有帮助。
@RequestMapping(value = "test/read", method = RequestMethod.GET)
public ResponseEntity<String> get(
@RequestParam(value="from") @DateTimeFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss'Z'") final Date from)
// value of date in 'from' is in local timezone
The value should be in the UTC timezone, due to the trailing 'Z' in the pattern passed to @DateTimeFormat. 由于传递给@DateTimeFormat的模式中的尾部“ Z”,该值应位于UTC时区。
Any ideas for what I'm missing here? 关于我在这里缺少什么的任何想法?
When you start your application, the VM automatically inherits the default timezone of your local machine. 启动应用程序时,VM会自动继承本地计算机的默认时区。 You can update the default timezone for your application as following.
您可以如下更新应用程序的默认时区。
@SpringBootApplication
public class Application {
public static void main(String[] args) {
TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
SpringApplication.run(Application.class, args);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.