Java pojo的Spring REST API注册日期转换器

Java pojo的Spring REST API注册日期转换器,java,spring,spring-boot,http-request-parameters,path-variables,Java,Spring,Spring Boot,Http Request Parameters,Path Variables,SpringREST默认情况下从路径变量和url参数提供构建pojo功能 就我而言,我有一个pojo: public class MyCriteria { private String from; private String till; private Long communityNumber; private String communityName; } 这是在我的控制器中使用的。Url是http://localhost:8080/community/{community

SpringREST默认情况下从路径变量和url参数提供构建pojo功能

就我而言,我有一个pojo:

public class MyCriteria {
  private String from;
  private String till;
  private Long communityNumber;
  private String communityName;
}
这是在我的控制器中使用的。Url是
http://localhost:8080/community/{communityNumber}/app
。请求结果

curl "http://localhost:8080/community/1/app?from=2018-11-14&till=2019-05-13&communityName=myCOm"
是:

它似乎很好用。在pojo中使用具有所需类型的数据更好。所以我希望有
LocalDate
类型的
from
till
字段。使用spring,我希望这个解决方案几乎是现成的。但由于生命周期的原因,任何spring或jackson日期转换器都无法解决我的问题

Spring在注入日期之前验证pojo字段的类型,我遇到了类型不匹配异常。我认为主要原因是spring使用了特殊的builder,它试图按名称查找所需的参数,而忽略了要在pojo内部为字段应用的注释

问题是: 在spring之前构建pojo是否有任何优雅的解决方案,其中一些字段在默认情况下将从
String
转换为
LocalDate
格式

附笔。 强制性条件包括:

  • 请求方法是
    GET
  • 所需pojo为:
  • 允许跳过自定义AOP实现或使用
    getter
    s(其中注入了转换逻辑)的想法
  • 既没有正文(请求正文为空)也没有json(所有必需的数据都是路径变量或路径参数)
P.S.2。
  • 可用于实验的控制器示例如下:
  • 此外,该模块还包含所需的
    控制器
    标准
    ,以使您的理解和实验更有用

    • 这可能会对您有所帮助。我有类似的情况,我使用这种方法将数据转换为特定的需求

      public class MyCriteria {
        public MyCriteria(LocalDate from, LocalDate till, Long communityNumber, String communityName){
         // assignement of variables
      }
        private LocalDate from;
        private LocalDate till;
        private Long communityNumber;
        private String communityName;
      }
      
      因此,无论何时从JSON创建对象,它都会根据需求创建对象

      public class MyCriteria {
        public MyCriteria(LocalDate from, LocalDate till, Long communityNumber, String communityName){
         // assignement of variables
      }
        private LocalDate from;
        private LocalDate till;
        private Long communityNumber;
        private String communityName;
      }
      

      当我实现这一点时,我使用“Jackson”的ObjectMapper类来做这件事。希望您也在使用相同的工具。

      在pojo类中使用java.sql.Date。如private Date from。我希望它能用于JSON转换。当您从UI接收日期JSON字段时,请始终使用Java.sql.date进行Jackson日期转换。

      为此,您需要添加jsr310依赖项

      compile("com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.8.10")
      

      我希望这对您很有用。

      问题的答案是使用init binder在指定条件内注册类型映射。它需要有
      属性编辑器支持
      实现以用于指定目的

      简短的代码示例是:

          @InitBinder
          public void initBinder(WebDataBinder binder) {
              binder.registerCustomEditor(LocalDate.class, new PropertyEditorSupport() {
                  @Override
                  public void setAsText(String text) throws IllegalArgumentException {
                      setValue(LocalDate.parse(text, DateTimeFormatter.ISO_LOCAL_DATE));
                  }
              });
          }
      
      完整的代码示例可以取自:

      因此,这种情况下的模型可以是:

      import lombok.Data;
      
      import java.time.LocalDate;
      
      @Data
      public class MyCriteriaLd {
          private LocalDate from;
          private LocalDate till;
          private Long communityNumber;
          private String communityName;
      }
      

      我写道,我没有任何
      json
      。添加您的conotroller codeController代码非常常见,但无论如何,我已经为您添加了它,请参见第2页和github链接。我也尝试了。它在所描述的条件下不工作。
          @InitBinder
          public void initBinder(WebDataBinder binder) {
              binder.registerCustomEditor(LocalDate.class, new PropertyEditorSupport() {
                  @Override
                  public void setAsText(String text) throws IllegalArgumentException {
                      setValue(LocalDate.parse(text, DateTimeFormatter.ISO_LOCAL_DATE));
                  }
              });
          }
      
      import lombok.extern.slf4j.Slf4j;
      import org.springframework.web.bind.WebDataBinder;
      import org.springframework.web.bind.annotation.GetMapping;
      import org.springframework.web.bind.annotation.InitBinder;
      import org.springframework.web.bind.annotation.RequestMapping;
      import org.springframework.web.bind.annotation.RestController;
      import org.vl.example.rest.dtofromoaramsandpath.web.dto.MyCriteriaLd;
      
      import java.beans.PropertyEditorSupport;
      import java.time.LocalDate;
      import java.time.format.DateTimeFormatter;
      
      @RestController
      @RequestMapping("verify/criteria/mapping")
      @Slf4j
      public class MyControllerLd {
          @InitBinder
          public void initBinder(WebDataBinder binder) {
              binder.registerCustomEditor(LocalDate.class, new PropertyEditorSupport() {
                  @Override
                  public void setAsText(String text) throws IllegalArgumentException {
                      setValue(LocalDate.parse(text, DateTimeFormatter.ISO_LOCAL_DATE));
                  }
              });
          }
      
          @GetMapping("community/{communityNumber}/dtold")
          public MyCriteriaLd loadDataByDto(MyCriteriaLd criteria) {
              log.info("received criteria: {}", criteria);
              return criteria;
          }
      }
      
      import lombok.Data;
      
      import java.time.LocalDate;
      
      @Data
      public class MyCriteriaLd {
          private LocalDate from;
          private LocalDate till;
          private Long communityNumber;
          private String communityName;
      }