- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
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
类型的字段 from
和 till
。使用 spring,我希望这个解决方案几乎是开箱即用的。但由于生命周期的原因,任何 spring 或 jackson 日期转换器都无法解决我的问题。
Spring 在注入(inject)日期之前验证 pojo 字段的类型,并且我收到类型不匹配异常。我认为一般原因是 spring 使用特殊的构建器,它尝试按名称查找所需的参数,并且忽略要在 pojo 内部应用的字段的注释。
是否有任何优雅的解决方案可以通过 spring 构建 pojo,其中某些字段默认从 String
转换为 LocalDate
格式?
强制性条件是:
GET
;public class MyCriteria {
private LocalDate from;
private LocalDate till;
private Long communityNumber;
private String communityName;
}
getter
的想法;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();
}
}
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/
我刚刚发现了 Camel,它似乎正是我所需要的。 我有几个知道如何处理特定输入数据的构建 block ,所以我想创建一个非常简单的 GUI 供用户选择构建 block ,只需将它们一个接一个地链接起来
根据Convert Spark DataFrame to Pojo Object的回复我了解到 Dataframe是 Dataset 的别名. 我目前计算了 JavaPairRDD哪里CityStat
我使用的是jackson,场景是: 在客户端 (Android) 中生成对 Web 服务的 URL 调用 Web 服务以 json 形式返回“complex pojo”(具有与其他 pojo 相同的类
我需要将文档存储到 Elasticsearch 索引中,因此我定义了一个映射。我需要从我的 java 客户端向它提供看起来像 Compony 类的 pojo。它有很多重复的值。我可以使用对象组合模式来
所以我正在开发一个聊天应用程序。有一部分我感兴趣的是一次从数据库中检索 3 个实体。有一个 serverDto 实体和 accountDto 实体。每个服务器都有很多账户,账户里有一个字段叫做acti
我有一个 Java 中的 POJO 列表,我需要将其转换为另一种类型的 POJO 列表。两个 POJO 都有两个字符串字段。除了迭代原始列表并将新项目添加到结果列表之外,是否有更好的方法来执行此操作?
我正在开发一个 API 来访问存储在系统中的数据。该系统包含人员、约会和与这些约会相关的程序等内容。我的应用程序将严格为只读。 我正在使用带 RowMapper 的 Spring 来构建对象,例如“P
希望有人能帮助我。我有一个具有以下结构的 POJO: public class Invoice{ private String docNum; private String customer; pri
引用以下链接,我在运行时创建了类,http://blog.javaforge.net/post/31913732423/howto-create-java-pojo-at-runtime-with-j
所以我遇到了这个教程,将 POJO 序列化为 json,然后将 json 文件反序列化回 POJO。 http://www.mkyong.com/java/how-to-convert-java-ob
我是 Java 的新手。我只是对序列化和反序列化感到困惑。所以,我很困惑我应该使用哪一个。 我正在寻找一个回合,发现 Boon、Jackson、GSON(我目前正在使用 GSON,但一些文章使用 Ja
我确信这个问题可能已经被问过几次了,但我不明白我应该使用什么查询。我想要做的是,将 POJO 传递给另一个参数可以是动态的 POJO 像下面的例子 Class DataPOJO{ privat
我有一个有多页的注册表。所以,我将一页映射到一个 pojo。最后,当我处理数据库时,我想将其作为单个 pojo 提交。任何简化此模型的想法。 谢谢:) 最佳答案 您可以创建一个包装器 POJO,它在主
希望有人能帮助我。我有一个具有以下结构的 POJO: public class Invoice{ private String docNum; private String customer; pri
要求: 将 json 文件中选定的字段收集到 POJO 中(字段名称不匹配) 更新 POJO 的其他非映射字段 转换回带有 POJO 字段名的 jsonNode 问题:步骤 1 和 2 没问题。当将
我的 Spring Boot 应用程序中有两个 Pojo Pojo 1:FeedData 代码(我有获取和设置,只是没有在这里显示): package com.cms.tb.model; import
我们有两个 Pojo 文件。 Person { String name; int age; String address; String phoneNo; boolean isMa
我有一个数据库调用,如果没有任何条件匹配,则可能返回 null。如果有记录匹配,则结果是一个包含嵌入对象列表的 Pojo。我想将该 Pojo 转换为其嵌入对象 id 的列表。 Foo.class 有
我正在尝试使用流将 pojo 映射到 pojo 上的属性公开的一组项目。我意识到这还不清楚,所以我将展示我是如何在没有流的情况下完成的。 我有产品类别的枚举,产品的枚举和具有此类产品的商店列表 pub
我们在 Apache2 网络服务器后面的 Tomcat 6 中运行的 AXIS2 v1.5.2 中实现了一个 POJO 网络服务。 出于演示目的,我将重点介绍 validateUser 消息和相应的方
我是一名优秀的程序员,十分优秀!