gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 07:36:08 25 4
gpt4 key购买 nike

Spring Rest 默认提供从路径变量和 url 参数构建 pojo 功能。

就我而言,我有 pojo:

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

在我的 Controller 中使用。网址为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 数据中包含按用途所需的类型会更好。所以我想要有 LocalDate 类型的字段 fromtill 。使用 spring,我希望这个解决方案几乎是开箱即用的。但由于生命周期的原因,任何 spring 或 jackson 日期转换器都无法解决我的问题。

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

问题是:

是否有任何优雅的解决方案可以通过 spring 构建 pojo,其中某些字段默认从 String 转换为 LocalDate 格式?

附注

强制性条件是:

  • 请求方法为GET
  • 所需的 pojo 是:
public class MyCriteria {
private LocalDate from;
private LocalDate till;
private Long communityNumber;
private String communityName;
}
  • 让我们跳过使用自定义 AOP 实现或注入(inject)转换逻辑的 getter 的想法;
  • 既没有body(请求体为空),也没有json(所有必需的数据都是路径变量或路径参数)。

PS2。

  • 可用于实验的 Controller 示例是:
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();
}
}
  • 还有link on github project其中包含具有所需 Controller 标准的模块,以使您的理解和实验更加有用。

最佳答案

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

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 类来完成此操作。希望您也能使用同样的方法。

关于java - Spring REST API 为 pojo 注册日期转换器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58544601/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com