2

I'm trying getting the optional class with jersey client. When the remote server returns Optional.empty(), I'm getting this error.

Caused by: com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "present" (class java.util.Optional), not marked as ignorable 

My code

ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new Jdk8Module());

Client client = ClientBuilder.newClient().register(new JacksonJsonProvider(mapper));
Optional<MyType> myType = client.target(TARGET_URL)
                    .path(PATH)
                    .request()
                    .get(Optional.class);

My related dependencies are as

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.9.1</version>
</dependency>

<dependency>
    <groupId>com.fasterxml.jackson.datatype</groupId>
    <artifactId>jackson-datatype-jsr310</artifactId>
    <version>2.9.1</version>
</dependency>

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

EDIT

The application that running on the remote server is a spring boot app. I added jackson-datatype-jdk8 dependency on the app. After this, the Unrecognized field exception solved but the return object is null. Before this, the postman was returned the following result

{
    "present": false
}

But now

null
Rhododendron
  • 559
  • 2
  • 7
  • 15

1 Answers1

0

You can make use of the @JsonInclude(Include.NON_EMPTY) to annotate such a filed.

Value that indicates that only properties that have values that values that are null or what is considered empty are not to be included.

Mani
  • 1,068
  • 3
  • 13
  • 27