英文:
Spring trucantes time part from a Date GET RequestParam
问题
以下是您提供的代码的中文翻译:
我有一个Spring MVC REST控制器,其中一个对象作为参数:
public List<AccessResponse> getAccesses(AccessRequestParams params) throws Exception {
log.debug("接收到GET请求");
}
参数是一个简单的Bean,其中包含一些属性,我在这里只复制了重要的日期属性:
public class AccessRequestParams {
Date since = null;
...
public AccessRequestParams() {
super();
}
public Date getSince() {
return since;
}
public void setSince(Date since) {
this.since = since;
}
}
当我调用此URL时:
http://localhost:8080/v1/accesses?since=2017-05-04T12:08:56
since
属性被填充为2017-05-04 00:00:00,为什么会截断时间部分?
我还尝试过:
- 用空格而不是"T"在日期和时间之间调用此URL
http://localhost:8080/v1/accesses?since=2017-05-04%2012:08:56
- 为
Date since
属性添加了@DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")注解。
但是我没有找到任何解决方案。
我的Spring配置中有这个BEAN:
@Bean
public FormattingConversionService mvcConversionService() {
DefaultFormattingConversionService conversionService = new DefaultFormattingConversionService(false);
DateTimeFormatterRegistrar dateTimeRegistrar = new DateTimeFormatterRegistrar();
dateTimeRegistrar.setDateFormatter(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
dateTimeRegistrar.setDateTimeFormatter(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
dateTimeRegistrar.registerFormatters(conversionService);
DateFormatterRegistrar dateRegistrar = new DateFormatterRegistrar();
dateRegistrar.setFormatter(new DateFormatter("yyyy-MM-dd"));
dateRegistrar.registerFormatters(conversionService);
return conversionService;
}
请注意,这是您提供的代码的翻译部分,没有包含其他内容。
英文:
I have a Spring MVC REST controller with an object as param:
public List<AccessResponse> getAccesses(AccessRequestParams params) throws Exception {
log.debug("Received get request");
}
The param is a simple bean with some properties, I copy here just the important date property:
public class AccessRequestParams {
Date since=null;
...
public SincePageFilter() {
super();
}
public Date getSince() {
return since;
}
public void setSince(Date since) {
this.since = since;
}
}
When I call this URL:
http://localhost:8080/v1/accesses?since=2017-05-04T12:08:56
The since
property is filled with 2017-05-04 00:00:00, Why it truncates the time part?
I also tried to:
- Call this URL with space instead of "T" between date and time
http://localhost:8080/v1/accesses?since=2017-05-04%2012:08:56
- Added @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss") annotation for
Date since
property.
But I did not find any solution.
My Spring config has this BEAN:
@Bean
public FormattingConversionService mvcConversionService() {
DefaultFormattingConversionService conversionService = new DefaultFormattingConversionService(false);
DateTimeFormatterRegistrar dateTimeRegistrar = new DateTimeFormatterRegistrar();
dateTimeRegistrar.setDateFormatter(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
dateTimeRegistrar.setDateTimeFormatter(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
dateTimeRegistrar.registerFormatters(conversionService);
DateFormatterRegistrar dateRegistrar = new DateFormatterRegistrar();
dateRegistrar.setFormatter(new DateFormatter("yyyy-MM-dd"));
dateRegistrar.registerFormatters(conversionService);
return conversionService;
}
答案1
得分: 0
你已为类型java.util.Date
注册了全局的DateFormatter
,格式为yyyy-MM-dd
。这按预期工作。
-
您可以更改它以包含时间部分。
-
或者您可以在请求bean中使用类型
LocalDateTime
。然后另一种格式将有效:yyyy-MM-dd HH:mm:ss
(带空格)。 -
或者您可以删除配置,让Spring使用默认格式。
英文:
You have registered a global DateFormatter
for the type java.util.Date
with format yyyy-MM-dd
. This works as expected.
-
You can change it to include the time part.
-
Or you can use a type
LocalDateTime
in your request bean. Then the other format will be valid:yyyy-MM-dd HH:mm:ss
(with space). -
Or you can remove the configuration and let Spring use the default formats.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论