Java Spring 补丁 RFC-6902 即时类型转换异常
Java Spring Patch RFC-6902 Instant type conversion exception
我遇到了与将字符串值转换为相应类型有关的修补问题。当我尝试修补 "Locale" 类型(或基元)时,它起作用了。但 Instant
失败了
实体:
@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 表达式上下文完成的,因此所有 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
的 类。所以基本上只有 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 应用到实体。
欢迎所有建议和建议。
基本上,问题在于数据绑定不是由 FasterXML 完成的,而是由 Spring 表达式上下文完成的。所以添加 jackson-datatype-jsr310
根本无济于事。仅当补丁 value
是对象或数组时才会使用 FasterXML。但在您的情况下,补丁 value
是字符串类型,因此 JsonPatchPatchConverter
将尝试使用纯粹的 Spring 工具(Spring 表达式上下文)来转换值。所以现在您缺少的是 Spring 框架的字符串到即时转换器。我很确定有一些可用的实现,甚至可能有些在 Spring 库中,但我将在这里创建一个简单的实现并展示如何注册它。
最初,让我们创建一个转换器(不是最好的实现,只是为了概念证明)。
public static enum StringToInstantConverter implements Converter<String, Instant> {
INSTANCE;
@Override
public Instant convert(String source) {
try {
return Instant.parse(source);
} catch(DateTimeParseException ex) {
}
return null;
}
}
并在调用applyPatchOnObject
方法前注册
像这样的东西会起作用。
// REGISTER THE CONVERTER
ConversionService conversionService = DefaultConversionService.getSharedInstance();
ConverterRegistry converters = (ConverterRegistry) conversionService;
converters.addConverter(StringToInstantConverter.INSTANCE);
ObjectMapper mapper = new ObjectMapper();
ArrayNode patchArray = mapper.createArrayNode();
ObjectNode patch = mapper.createObjectNode();
patch.put("op", "replace");
patch.put("path", "dateOfBirth");
patch.put("value", "1971-01-01T01:01:01.001Z");
// [{"op": "replace", "path": "dateOfBirth", "value": "1971-01-01T01:01:01.001Z"}]
patchArray.add(patch);
// apply the patch
User patchedUser = applyPatchOnObject(User.class, new User(), patchArray);
只是为了补充上面的答案。如果你有 Instant
class 可用,为什么要使用 SimpleDateFormat
呢?直接解析输入即可:
public Instant convert(String source) {
try {
return Instant.parse(source);
} catch(DateTimeParseException ex) {
}
return null;
}
像"1971-01-01T01:01:01.001Z"
这样的字符串已经是上面方法解析的格式,所以这应该可以。
如果您需要解析不同格式的输入,只需使用 DateTimeFormatter
: https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html
我遇到了与将字符串值转换为相应类型有关的修补问题。当我尝试修补 "Locale" 类型(或基元)时,它起作用了。但 Instant
失败了实体:
@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 表达式上下文完成的,因此所有 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
的 类。所以基本上只有 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 应用到实体。
欢迎所有建议和建议。
基本上,问题在于数据绑定不是由 FasterXML 完成的,而是由 Spring 表达式上下文完成的。所以添加 jackson-datatype-jsr310
根本无济于事。仅当补丁 value
是对象或数组时才会使用 FasterXML。但在您的情况下,补丁 value
是字符串类型,因此 JsonPatchPatchConverter
将尝试使用纯粹的 Spring 工具(Spring 表达式上下文)来转换值。所以现在您缺少的是 Spring 框架的字符串到即时转换器。我很确定有一些可用的实现,甚至可能有些在 Spring 库中,但我将在这里创建一个简单的实现并展示如何注册它。
最初,让我们创建一个转换器(不是最好的实现,只是为了概念证明)。
public static enum StringToInstantConverter implements Converter<String, Instant> {
INSTANCE;
@Override
public Instant convert(String source) {
try {
return Instant.parse(source);
} catch(DateTimeParseException ex) {
}
return null;
}
}
并在调用applyPatchOnObject
方法前注册
像这样的东西会起作用。
// REGISTER THE CONVERTER
ConversionService conversionService = DefaultConversionService.getSharedInstance();
ConverterRegistry converters = (ConverterRegistry) conversionService;
converters.addConverter(StringToInstantConverter.INSTANCE);
ObjectMapper mapper = new ObjectMapper();
ArrayNode patchArray = mapper.createArrayNode();
ObjectNode patch = mapper.createObjectNode();
patch.put("op", "replace");
patch.put("path", "dateOfBirth");
patch.put("value", "1971-01-01T01:01:01.001Z");
// [{"op": "replace", "path": "dateOfBirth", "value": "1971-01-01T01:01:01.001Z"}]
patchArray.add(patch);
// apply the patch
User patchedUser = applyPatchOnObject(User.class, new User(), patchArray);
只是为了补充上面的答案。如果你有 Instant
class 可用,为什么要使用 SimpleDateFormat
呢?直接解析输入即可:
public Instant convert(String source) {
try {
return Instant.parse(source);
} catch(DateTimeParseException ex) {
}
return null;
}
像"1971-01-01T01:01:01.001Z"
这样的字符串已经是上面方法解析的格式,所以这应该可以。
如果您需要解析不同格式的输入,只需使用 DateTimeFormatter
: https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html