'Inform Jackson to throw exception if Json does not match expected object structure

Using this code to convert convert Json to a Java object using Jackson annotations :

import com.fasterxml.jackson.databind.ObjectMapper;
import objectmappertest.Request;
import java.io.IOException;

    public static void main(String args[]) throws IOException {
        final String json  = "{\"datePurchased\":\"2022-02-03 21:32:017\"},{\"unknownField\":\"test\"}";
        final Request request = mapper.readValue(json, Request.class);
        System.out.println("request : "+request);
    }

I expect an exception to be thrown as the Request Java object does not contain a field type unknownField , instead it seems that Jackson parses what it can from the JSON. Is there a configuration option which a causes an exception or a flag to be set if the Json being passed to Jackson does not match the Java object structure ?

Here is the expected structure :

import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import lombok.extern.jackson.Jacksonized;
import java.util.Date;

@Builder
@ToString
@Getter
@Setter
@Jacksonized
public class Request
{
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:sss")
    private final Date datePurchased;
}
 


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source