java 如何转换为@DateTimeFormat模式的@ParameterObject中的LocalDateTime字段而不是iso?

uz75evzq  于 2023-06-28  发布在  Java
关注(0)|答案(1)|浏览(156)
    • 更新:这甚至不是一个问题,这完全是我的DUMB。请投票反对这个问题。**

感谢@M Deinum指出,这不是一个Spring的事情。
我的IDEA的设置在今天早上的一次崩溃中搞砸了,并且在重新运行之前没有自动编译。

控制器

@GetMapping("/page")
public Response Page(@ParameterObject Page page, @ParameterObject QueryDTO param) {
    /*
     * irrelevant parts omitted
     */
}

当我在DTO中使用它时,它可以很好地处理传入的yyyy-MM-dd'T'HH:mm:ss模式。

@Data
public class QueryDTO {
    /*
     * irrelevant parts omitted
     */
    @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
    private LocalDateTime startTime;

    @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
    private LocalDateTime endTime;
}

但是当我想用“”而不是“T”时,我试过:

@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")

并将输入参数从“1999 - 02 - 11T00:00:00”更改为“1999 - 02 - 11 00:00:00”,我得到
endTime Failed to convert property value of type 'java.lang.String' to required type 'java.time.LocalDateTime' for property 'endTime'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@org.springframework.format.annotation.DateTimeFormat java.time.LocalDateTime] for value [1999-02-11 00:00:00]; nested exception is java.lang.IllegalArgumentException: Parse attempt failed for value [1999-02-11 00:00:00]
有没有使用注解而不是手动编写转换器的解决方案?
非常感谢。
我试着用关键字“LocalDateTime@ParameterObject@DateTimeFormat模式”搜索,没有得到太多帮助。

06odsfpq

06odsfpq1#

@DateTimeFormat注解通常用于在Spring MVC控制器中绑定请求参数时格式化和解析日期时间值。但是,它不支持直接使用空格的自定义模式。

要实现指定的模式,您可以使用@DateTimeFormat注解和模式,也可以使用@InitBinder

@GetMapping("/page")
public Response page(@ParameterObject Page page, @ParameterObject QueryDTO param) {
    // Your code
}

@ControllerAdvice
public class DateTimeFormatConfiguration extends WebMvcConfigurerAdapter {

    @Override
    public void initBinder(WebDataBinder binder) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        binder.addCustomFormatter(new LocalDateTimeFormatter(formatter));
    }

    private static class LocalDateTimeFormatter implements Formatter<LocalDateTime> {

        private final DateTimeFormatter formatter;

        public LocalDateTimeFormatter(DateTimeFormatter formatter) {
            this.formatter = formatter;
        }

        @Override
        public LocalDateTime parse(String text, Locale locale) throws ParseException {
            return LocalDateTime.parse(text, formatter);
        }

        @Override
        public String print(LocalDateTime object, Locale locale) {
            return formatter.format(object);
        }
    }
}
  • 通过应用此配置,您应该能够在QueryDTO类中使用模式**“yyyy-MM-dd HH:mm:ss”**和@DateTimeFormat注解。
    如果您想直接在annotation中应用模式,而不需要全局配置,可以使用其他方法:
@Retention(RetentionPolicy.RUNTIME)
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
public @interface CustomDateTimeFormat {
}

public class QueryDTO {
    // Your code

    @CustomDateTimeFormat
    private LocalDateTime startTime;

    @CustomDateTimeFormat
    private LocalDateTime endTime;

    // Getters and setters
}

@Controller
public class YourController {

    @GetMapping("/page")
    public Response page(@ParameterObject Page page, @ParameterObject QueryDTO param) {
        // Your code
    }

    @InitBinder
    public void initBinder(WebDataBinder binder) {
        binder.addCustomFormatter(new LocalDateTimeFormatter());
    }

    private static class LocalDateTimeFormatter implements Converter<String, LocalDateTime> {

        @Override
        public LocalDateTime convert(String source) {
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            return LocalDateTime.parse(source, formatter);
        }
    }
}
  • 我添加了一个自定义注解,封装了所需的模式,并在您的**QueryDTO中使用它。然后,您可以编写一个自定义的Converter,使用指定的模式将注解字符串转换为LocalDateTime对象**。

请让我知道它是否解决了你的问题,如果是的话,请留下你的意见。^^

相关问题