- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
背景:我的应用程序构建在 Spring Data REST 和 MongoDB 存储库之上。
考虑这个带有 YearMonth
字段的简单 Java 域对象:
@Getter @Setter
public class Console {
@Id private String id;
private String name;
private YearMonth releaseMonth;
private Vendor vendor;
}
并且该域对象可通过 MongoRepository 实现进行持久化:
public interface ConsoleRepository extends MongoRepository<Console, String> {
Console findByName(@Param("name") String name);
}
当公开 REST Controller (由 Data REST 自动)来管理此域对象时,我添加了 jackson-datatype-jsr310
gradle 依赖项,以便解析 YearMonth JSON 值(例如:“2016” -04") 由 Jackson 插入此字段:
compile 'com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.6.1'
当 POST 到此端点时,JSON 文档中包含的 YearMonth 值被正确解析为 YearMonth 字段,并且整个对象成功存储为 MongoDB 上的文档。在mongo上查找这个文档证明:
> db.console.find()
{ "_id" : ObjectId("575f837ca75df1fc7e5f4f96"),
"_class" : "xxx.yyy.Console",
"name" : "Console 1",
"releaseMonth" : { "year" : 1988, "month" : 10 },
"vendor" : "VENDOR_1" }
但是,当我尝试从 REST Controller 获取该资源时,MongoDB 客户端无法将此 YearMonth 值绑定(bind)到 Java 对象中:
GET localhost:8080/consoles
回应:
{
"timestamp": 1465954648903,
"status": 500,
"error": "Internal Server Error",
"exception": "org.springframework.data.mapping.model.MappingException",
"message": "No property null found on entity class java.time.YearMonth to bind constructor parameter to!",
"path": "/consoles"
}
我认为 MongoDB Java 客户端缺乏对 Java 8 的 YearMonth 值的内置支持,但由于它能够保存它们,所以这似乎被排除了。我在这里缺少什么?
最佳答案
我能够通过创建 Custom Converter 来解析这个对象。 :
@Component
public class DBObjectToYearMonthConverter implements Converter<DBObject, YearMonth> {
@Override
public YearMonth convert(DBObject source) {
return YearMonth.of(
(int) source.get("year"),
(int) source.get("month")
);
}
}
并在 Application 类上设置 CustomConversions @Bean:
@Bean
public CustomConversions getCustomConversions() {
return new CustomConversions(Arrays.asList(
new DBObjectToYearMonthConverter()
));
}
欢迎其他选项。
关于java - 存储在 mongodb 上的 YearMonth 字段无法解析回对象字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37824751/
此 map-reduce 用于计算“Hadoop:权威指南”中的示例每年和每月的平均温度 输出应该是 年[TAB]月[TAB]平均_温度 由于键(年+月)已经合并,是否可以使用 substring()
我想解析两个单独的字符串 "1982"和"SEP"进入java.time.YearMonth对象。 java.time.YearMonth.parse("1978 SEP", java.time.fo
在.Net Framework中,我们常用的时间类型是DateTime。直到.Net6微软加入了两个新的时间类型:DateOnly和TimeOnly,才弥补了之前的不足。 DateOnly :
本文整理了Java中java.time.YearMonth.with()方法的一些代码示例,展示了YearMonth.with()的具体用法。这些代码示例主要来源于Github/Stackoverfl
本文整理了Java中java.time.YearMonth.until()方法的一些代码示例,展示了YearMonth.until()的具体用法。这些代码示例主要来源于Github/Stackover
本文整理了Java中java.time.YearMonth.from()方法的一些代码示例,展示了YearMonth.from()的具体用法。这些代码示例主要来源于Github/Stackoverfl
本文整理了Java中java.time.YearMonth.of()方法的一些代码示例,展示了YearMonth.of()的具体用法。这些代码示例主要来源于Github/Stackoverflow/M
我正在使用 joda.time 的 YearMonth 对象,并希望在此对象中获取月份日期的最后一天。 yearMonth.monthOfYear().getMaximumValue(); //ret
我想找到等于或晚于当前 YearMonth 的月份 final YearMonth yearMonth = YearMonth.of(2020, 8); final boolean after = y
如何在实体中使用 YearMonth?我已经看到很多关于转换器的堆栈溢出答案?这仍然需要吗?如果需要的话如何? @Basic(optional = false) @NotNull @Column(na
我在解析代表年份和月份的特殊字符串时遇到问题,偏移量如下:2014-08+03:00 . 所需的输出是 YearMonth . 我已经测试过创建自定义 DateTimeFormatter有各种模式,但
本文整理了Java中java.time.YearMonth.()方法的一些代码示例,展示了YearMonth.()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven
本文整理了Java中java.time.YearMonth.compareTo()方法的一些代码示例,展示了YearMonth.compareTo()的具体用法。这些代码示例主要来源于Github/S
本文整理了Java中java.time.YearMonth.lengthOfMonth()方法的一些代码示例,展示了YearMonth.lengthOfMonth()的具体用法。这些代码示例主要来源于
本文整理了Java中java.time.YearMonth.minusMonths()方法的一些代码示例,展示了YearMonth.minusMonths()的具体用法。这些代码示例主要来源于Gith
本文整理了Java中java.time.YearMonth.now()方法的一些代码示例,展示了YearMonth.now()的具体用法。这些代码示例主要来源于Github/Stackoverflow
本文整理了Java中java.time.YearMonth.isBefore()方法的一些代码示例,展示了YearMonth.isBefore()的具体用法。这些代码示例主要来源于Github/Sta
本文整理了Java中java.time.YearMonth.getMonth()方法的一些代码示例,展示了YearMonth.getMonth()的具体用法。这些代码示例主要来源于Github/Sta
本文整理了Java中java.time.YearMonth.format()方法的一些代码示例,展示了YearMonth.format()的具体用法。这些代码示例主要来源于Github/Stackov
本文整理了Java中java.time.YearMonth.plus()方法的一些代码示例,展示了YearMonth.plus()的具体用法。这些代码示例主要来源于Github/Stackoverfl
我是一名优秀的程序员,十分优秀!