Spring从GET请求参数中截取日期的时间部分

huangapple go评论84阅读模式
英文:

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&lt;AccessResponse&gt; getAccesses(AccessRequestParams params) throws Exception {
    log.debug(&quot;Received get request&quot;);
}

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(&quot;yyyy-MM-dd&quot;));
    dateTimeRegistrar.setDateTimeFormatter(DateTimeFormatter.ofPattern(&quot;yyyy-MM-dd HH:mm:ss&quot;));
    dateTimeRegistrar.registerFormatters(conversionService);

    DateFormatterRegistrar dateRegistrar = new DateFormatterRegistrar();
    dateRegistrar.setFormatter(new DateFormatter(&quot;yyyy-MM-dd&quot;));
    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.

huangapple
  • 本文由 发表于 2023年5月17日 21:20:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76272575.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定