首页 > 解决方案 > 用于 pojo 的 Spring REST API 注册日期转换器

问题描述

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和类型tillLocalDate使用 spring 我希望这个解决方案几乎是开箱即用的。但是由于生命周期,任何弹簧或杰克逊日期转换器都无法解决我的问题。

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

问题是:

是否有任何优雅的解决方案可以在春季构建 pojo,默认情况下某些字段将转换StringLocalDate格式?

附言

强制性条件是:

public class MyCriteria {
  private LocalDate from;
  private LocalDate till;
  private Long communityNumber;
  private String communityName;
}

PS2。

import lombok.extern.slf4j.Slf4j;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import org.vl.example.rest.dtofromoaramsandpath.web.dto.MyCriteria;

import java.time.LocalDate;

@RestController
@RequestMapping("verify/criteria/mapping")
@Slf4j
public class MyController {

    @GetMapping("community/{communityNumber}/dto")
    public MyCriteria loadDataByDto(MyCriteria criteria) {
        log.info("received criteria: {}", criteria);
        return criteria;
    }

    @GetMapping("community/{communityNumber}/default/params")
    public String loadDataByDefaultParameters(@PathVariable("communityNumber") String communityNumber,
                                            @RequestParam(value = "from", required = false) String from,
                                            @RequestParam(value = "till", required = false) String till,
                                            @RequestParam(value = "communityName", required = false) String communityName) {
        log.info("received data without converting:\n\tcommunityNumber => {}\n\tfrom => {}\n\ttill => {}\n\tcommunityName => {}",
                communityNumber, from, till, communityName);
        return new StringBuilder("{")
                .append("\n\tfrom:").append(from).append(";")
                .append("\n\tfrom:").append(from).append(";")
                .append("\n\ttill:").append(till).append(";")
                .append("\n\tcommunityName:").append(communityName)
                .append("\n}\n").toString();
    }

    @GetMapping("community/{communityNumber}/converted/params")
    public String loadUsingConvertedParameters(@PathVariable("communityNumber") String communityNumber,
                                             @RequestParam(value = "from") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate from,
                                             @RequestParam("till") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate till,
                                             @RequestParam(value = "communityName", required = false) String communityName) {
        log.info("received data with LocalDate converting:\n\tcommunityNumber => {}\n\tfrom => {}\n\ttill => {}\n\tcommunityName => {}",
                communityNumber, from, till, communityName);
        return new StringBuilder("{")
                .append("\n\tfrom:").append(from).append(";")
                .append("\n\tfrom:").append(from).append(";")
                .append("\n\ttill:").append(till).append(";")
                .append("\n\tcommunityName:").append(communityName)
                .append("\n}\n").toString();
    }
}

标签: javaspringspring-boothttp-request-parameterspath-variables

解决方案


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

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 类来做这件事。希望你也一样。


推荐阅读