存储在 mongodb 上的 YearMonth 字段无法解析回对象字段
YearMonth field stored on mongodb cannot be parsed back to object field
背景:我的应用程序建立在 Spring Data REST 和 MongoDB Repositories 之上。
考虑这个带有 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 控制器(由 Data REST 自动执行)以管理此域对象时,我添加了 jackson-datatype-jsr310
gradle 依赖项以解析 YearMonth JSON 值(例如: "2016-04") 杰克逊进入这个领域:
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 控制器获取该资源时,MongoDB 客户端无法将此 YearMonth 值绑定到 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")
);
}
}
并在应用程序上设置 CustomConversions @Bean class:
@Bean
public CustomConversions getCustomConversions() {
return new CustomConversions(Arrays.asList(
new DBObjectToYearMonthConverter()
));
}
欢迎其他选项。
背景:我的应用程序建立在 Spring Data REST 和 MongoDB Repositories 之上。
考虑这个带有 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 控制器(由 Data REST 自动执行)以管理此域对象时,我添加了 jackson-datatype-jsr310
gradle 依赖项以解析 YearMonth JSON 值(例如: "2016-04") 杰克逊进入这个领域:
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 控制器获取该资源时,MongoDB 客户端无法将此 YearMonth 值绑定到 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")
);
}
}
并在应用程序上设置 CustomConversions @Bean class:
@Bean
public CustomConversions getCustomConversions() {
return new CustomConversions(Arrays.asList(
new DBObjectToYearMonthConverter()
));
}
欢迎其他选项。