为了账号安全,请及时绑定邮箱和手机立即绑定

Spring REST API 为 pojo 注册日期转换器

Spring REST API 为 pojo 注册日期转换器

守着星空守着你 2023-12-13 14:24:43
Spring Rest默认提供从路径变量和url参数构建pojo的功能。就我而言,我有 pojo:public class MyCriteria {  private String from;  private String till;  private Long communityNumber;  private String communityName;}这是我的控制器中使用的。网址是http://localhost:8080/community/{communityNumber}/app. 请求结果curl "http://localhost:8080/community/1/app?from=2018-11-14&till=2019-05-13&communityName=myCOm"是:{  'from':'2018-11-14';  'till':'2019-05-12';  'communityNumber':'1';  'communityName':'myCOm'}看起来效果很好。在 pojo 数据中包含按用途所需的类型会更好。所以我想要有字段from和till类型LocalDate。使用 spring 我希望这个解决方案几乎是开箱即用的。但由于生命周期,任何弹簧或杰克逊日期转换器都无法解决我的问题。Spring 在注入日期之前验证 pojo 字段的类型,并且我得到类型不匹配异常。我认为一般原因是 spring 使用特殊的构建器,它尝试按名称查找所需的参数,并且忽略要在 pojo 内部应用的字段的注释。问题是:是否有任何优雅的解决方案可以通过 spring 构建 pojo,其中某些字段默认会转换为String格式?LocalDate
查看完整描述

4 回答

?
ABOUTYOU

TA贡献1812条经验 获得超5个赞

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


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 创建对象时,它都会根据要求创建它。


当我实现这个时,我使用“Jackson”的 ObjectMapper 类来完成此操作。希望你也能使用同样的方法。


查看完整回答
反对 回复 2023-12-13
?
慕仙森

TA贡献1827条经验 获得超7个赞

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



查看完整回答
反对 回复 2023-12-13
?
弑天下

TA贡献1818条经验 获得超7个赞

问题的答案是使用 init binder 来注册指定条件内的类型映射。需要PropertyEditorSupport针对特定目的进行实施。


短代码示例是:


    @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));

            }

        });

    }

完整的代码示例可以从github获取:


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;

}


查看完整回答
反对 回复 2023-12-13
?
30秒到达战场

TA贡献1828条经验 获得超6个赞

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

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

我希望这对你有用。


查看完整回答
反对 回复 2023-12-13
  • 4 回答
  • 0 关注
  • 108 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信