How to use LocalDateTime RequestParam in Spring? I get “Failed to convert String to LocalDateTime”

后端 未结 10 1455
小蘑菇
小蘑菇 2020-11-29 02:35

I use Spring Boot and included jackson-datatype-jsr310 with Maven:


    com.fasterxml.jackson.datatype         


        
10条回答
  •  悲&欢浪女
    2020-11-29 02:45

    For global configuration :

    public class LocalDateTimePropertyEditor extends PropertyEditorSupport {
    
        @Override
        public void setAsText(String text) throws IllegalArgumentException {
            setValue(LocalDateTime.parse(text, DateTimeFormatter.ISO_LOCAL_DATE_TIME));
        }
    
    }
    

    And then

    @ControllerAdvice
    public class InitBinderHandler {
    
        @InitBinder
        public void initBinder(WebDataBinder binder) { 
            binder.registerCustomEditor(OffsetDateTime.class, new OffsetDateTimePropertyEditor());
        }
    
    }
    

提交回复
热议问题