Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jackson deserializing Optional throws NoSuchFieldError

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:

The model:

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

And the deserialization snippet:

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. enter image description here

Jackson versions:

<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
    }
like image 932
szab.kel Avatar asked Jan 29 '23 06:01

szab.kel


1 Answers

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>
like image 199
hiren Avatar answered Feb 02 '23 11:02

hiren