【发布时间】:2018-02-05 09:59:00
【问题描述】:
我遇到了与将字符串值转换为相应类型有关的修补问题。当我尝试修补“区域设置”类型(或原语)时,它可以工作。但即时失败
实体:
@JsonIgnore
@Field("locale")
private Locale locale;
@JsonIgnore
@Field("dateOfBirth")
private Instant dateOfBirth;
@JsonIgnore
public Locale getLocale() {
return this.locale;
}
@JsonIgnore
public void setLocale(Locale locale) {
this.locale = locale;
}
@JsonIgnore
public Instant getDateOfBirth() {
return this.dateOfBirth;
}
@JsonIgnore
public void setDateOfBirth(Instant dateOfBirth) {
this.dateOfBirth = dateOfBirth;
}
补丁方法:
public static <T> T applyPatchOnObject(Class<T> type, T object, JsonNode jsonNode) {
try {
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JavaTimeModule());
return new JsonPatchPatchConverter(mapper).convert(jsonNode).apply(object, type);
} catch (Exception e) {
throw new UnprocessableEntityException(e.getMessage());
}
}
pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
<relativePath />
</parent>
<!-- Date -->
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
</dependency>
数据:
[{"op": "replace", "path": "dateOfBirth", "value": "1971-01-01T01:01:01.001Z"}]
例外:
EL1034E: A problem occurred whilst attempting to set the property 'dateOfBirth': Type conversion failure
更深层次的异常:
EL1001E: Type conversion problem, cannot convert from java.lang.String to @com.fasterxml.jackson.annotation.JsonIgnore @org.springframework.data.mongodb.core.mapping.Field java.time.Instant
编辑 1:
以下代码块起作用:
代码:System.out.println(mapper.readValue("1517846620.12312312", Instant.class));
结果:2018-02-05T16:03:40.123123120Z
以下代码块不起作用:
补丁:[{"op": "replace", "path": "dateOfBirth", "value": "1517846620.12312312"}]
解决方案:
虽然@Babl 的回答可能会奏效,但我想出了以下几点。
正如@Babl 所指出的,Spring 框架修补不是 FasterXML 而是由 Spring Expression Context 完成的,因此所有 Jackson 注释都不会生效。
我正在直接修补
User实体,这是非常糟糕的做法。
所以我最终得到了以下实现
补丁库
<dependency>
<groupId>com.flipkart.zjsonpatch</groupId>
<artifactId>zjsonpatch</artifactId>
<version>${zjsonpatch.version}</version>
</dependency>
补丁方法
public static <T extends EmbeddedResource> T applyPatchOnObject(Class<T> type, T object, JsonNode jsonNode) {
Assert.notNull(type, "Given type must not be null!");
Assert.notNull(object, "Given object must not be null!");
Assert.notNull(jsonNode, "Given jsonNode must not be null!");
try {
ObjectMapper mapper = new ObjectMapper().registerModule(new JavaTimeModule());
return mapper.convertValue(JsonPatch.fromJson(jsonNode).apply(mapper.convertValue(object, JsonNode.class)),
type);
} catch (Exception e) {
throw new UnprocessableEntityException(e.getMessage());
}
}
!注意:applyPatchOnObject 方法仅接受扩展 EmbeddedResource 的类,而 ResourceSupport 扩展了 ResourceSupport。所以基本上只有 DTO。
实体是一样的
使用所有正确的 Jackson 注释引入 UserDTO:
@NotNull(message = "locale cannot be null")
@JsonProperty("locale")
private Locale locale;
@NotNull(message = "dateOfBirth cannot be null")
@JsonProperty("dateOfBirth")
private Instant dateOfBirth;
@JsonIgnore
public Locale getLocale() {
return this.locale;
}
@JsonIgnore
public void setLocale(Locale locale) {
this.locale = locale;
}
@JsonIgnore
public Instant getDateOfBirth() {
return this.dateOfBirth;
}
@JsonIgnore
public void setDateOfBirth(Instant dateOfBirth) {
this.dateOfBirth = dateOfBirth;
}
在我的 DTO 修补了值之后。我将使用 ObjectMapper 或一些自定义方式将更改从 DTO 应用到实体。
欢迎所有建议和意见。
【问题讨论】:
-
抛开
Instant-conversion问题不谈,为什么不将出生日期建模为LocalDate? -
@MenoHochschild 我有另一种格式。但我想统一我的应用程序中的所有“日期”。这就是为什么所有“日期”现在都是
java.time.Instant。不确定它是否有意义,但它减少了担心的事情。
标签: java spring patch java-time rfc6902