Java Spring Patch RFC-6902即时类型转换异常

我有修补问题,这与将String值转换为相应的类型有关.当我尝试修补“Locale”类型(或原语)时,它可以工作.但它失败了即时

实体:

@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”:“替换”,“路径”:“dateOfBirth”,“value”:“1517846620.12312312”}]

解:

虽然@Babl的答案可能会奏效,但我想出了以下几点.

>正如@Babl指出的那样,Spring框架修补不是FasterXML,而是Spring Expression Context所以所有Jackson注释都不会产生任何影响.
>我正在直接修补用户实体,这是非常糟糕的做法.

所以我最终得到了以下实现

补丁库

<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());
    }
}

!注意:theapplyPatchOnObject方法只接受扩展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 Expression Context完成.所以添加jackson-datatype-jsr310根本没用.仅当补丁值为对象或数组时才使用FasterXML.但在您的情况下,补丁值是字符串类型,因此JsonPatchPatchConverter将尝试使用纯粹的Spring工具(Spring Expression Context)转换值.所以现在你缺少的是Spring Framework的String to Instant转换器.我很确定有一些实现可用,甚至可能有一些在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);
上一篇:python – Flask更新SQLite记录


下一篇:odoo12从零开始:二、个性化定制odoo12 之 创建数据库页面