Jackson反序列化可选抛出NoSuchFieldError [英] Jackson deserializing Optional throws NoSuchFieldError

查看:764
本文介绍了Jackson反序列化可选抛出NoSuchFieldError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个Spring Boot应用程序,并尝试包含 Optional< String> java.lang )我的一个模型中的字段,所以如果我使用的 @RestController 将不会获得这样的字段,它将包含为可选.empty()。每次我通过POST调用方法时,都会得到一个 java.lang.NoSuchFieldError:_valueInstantiator 异常。我开始尝试并制作了一个精简版的反序列化程序,它仍然给我错误:

I developing a Spring Boot application and I tried to include an Optional<String> (java.lang) field in one of my models, so If the @RestController I am using with won't get such a field, it will include it as an Optional.empty(). Every time I call the method via POST I get a java.lang.NoSuchFieldError: _valueInstantiator exception. I started experimenting and made a stripped down version of the deserializer, that still gives me the error:

模型:

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;

import java.util.Optional;

@JsonInclude(JsonInclude.Include.USE_DEFAULTS)
@JsonIgnoreProperties(ignoreUnknown = true)
public class Test {
    private int id;
    private String name;
    private Optional<String> nickname;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Optional<String> getNickname() {
        return nickname;
    }

    public void setNickname(Optional<String> nickname) {
        this.nickname = nickname;
    }
}

反序列化片段:

ObjectMapper mapper = new ObjectMapper();
Jdk8Module j = new Jdk8Module();
j.configureAbsentsAsNulls(true);//tried using true and false too
mapper.registerModule(j);
mapper.setSerializationInclusion(JsonInclude.Include.USE_DEFAULTS);
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);


mapper.readValue("{\"id\":1, \"name\":\"a\", \"nickname\":\"asd\"}", Test.class);

如果我不包含昵称属性,它仍然给我同样的错误。

If I don't include the nickname property at all, it still gives me the same error.

杰克逊版本:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.8.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

...

<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.datatype/jackson-datatype-jdk8 -->
<dependency>
    <groupId>com.fasterxml.jackson.datatype</groupId>
    <artifactId>jackson-datatype-jdk8</artifactId>
    <version>2.9.2</version>
</dependency>

尝试使用此控制器(相同的例外):

Tried it with this controller (same exception):

  @RequestMapping(value = "/test",method = RequestMethod.POST)
    public Test test(
            @RequestBody Test test
    ) {
        return test;//<-- did not even reach the breakpoint here
    }


推荐答案

通过最初运行代码,我发现代码在 jackson-datatype-jdk8 的2.8版本中运行良好。 6但不使用您正在使用的版本(即2.9.2)。使用以下依赖

By initially running your code I found code is working fine with jackson-datatype-jdk8's version 2.8.6 but not working with version you are using (i.e 2.9.2). Use below dependency

<dependency>
   <groupId>com.fasterxml.jackson.datatype</groupId>
   <artifactId>jackson-datatype-jdk8</artifactId>
   <version>2.8.6</version>
</dependency>

这篇关于Jackson反序列化可选抛出NoSuchFieldError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆