java - 用于 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
和类型till
。LocalDate
使用 spring 我希望这个解决方案几乎是开箱即用的。但是由于生命周期,任何弹簧或杰克逊日期转换器都无法解决我的问题。
Spring 在注入日期之前验证 pojo 字段的类型,我得到类型不匹配异常。我认为一般的原因是spring使用了特殊的构建器,它试图按名称查找所需的参数,它忽略了要在pojo内部为字段应用的注释。
问题是:
是否有任何优雅的解决方案可以在春季构建 pojo,默认情况下某些字段将转换String
为LocalDate
格式?
附言
强制性条件是:
- 请求方法是
GET
; - 所需的 pojo 是:
public class MyCriteria {
private LocalDate from;
private LocalDate till;
private Long communityNumber;
private String communityName;
}
- 让我们跳过自定义 AOP 实现或
getter
注入转换逻辑的想法; - 既没有正文(请求正文为空)也没有 json(所有必需的数据都是路径变量或路径参数)。
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();
}
}
- 还可以链接到包含所需模块的github 项目
controller
,criteria
以使您的理解和实验更有用。
解决方案
这可能会帮助你。我有类似的情况,我使用这种方法将数据转换为特定要求。
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 类来做这件事。希望你也一样。
推荐阅读
- sql - 组中排名靠前的结果的 SQL 优化
- c# - C# 将 combobox.value 获取到路径
- css - Windows 操作系统中的 Roboto-Regular 字体大小 14px 问题
- swift - 我的 Scrollview 不会滚动 ios
- angular - 在处理组件中的 nativeElement.setAttribute 的函数中升级到 Angular6 后出现 TypeError
- android - 如何以编程方式添加子项和父项 [ExpandableRecyclerView]?
- regex - Perl 从文本文件中解释正则表达式
- c++ - c++ - 在将数据写入管道之前,如何让客户端检查服务器是否完成了管道的读取操作
- javascript - 修复 Edge 中基于滚动的错误动画?
- python - 如何生成一个列表,其中元素与所需列表的距离固定