When you use jackson for conversion, it should actually throw an exception by default if unknown parameters are passed. I've tried this with jackson 2.26:
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>2.26</version>
</dependency>
And it threw the following exception without any further configuration:
Unrecognized field "value3" (class de.jan.model.TestObject), not marked as ignorable (one known property: "test"])
at [Source: org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$UnCloseableInputStream@2ff055a5; line: 1, column: 25] (through reference chain: de.jan.model.TestObject["value3"])%
So in case you're not constrained to anything else, you might just switch to jackson for conversion.
If you're already using jackson but still don't get this exception, you (or some library you're using) might have disabled this behaviour somewhere. To bring it back, you can add the following annotation to your class TestObject
:
@JsonIgnoreProperties(ignoreUnknown = false)
which will cause jackson to throw an exception for additional fields when deserialising instances of this particular class.
To configure this behaviour for all objects, you need to create a custom ObjectMapper
resolver like this:
@Provider
@Produces("application/json")
public class ObjectMapperResolver implements ContextResolver<ObjectMapper> {
private final ObjectMapper mapper;
public ObjectMapperResolver() {
mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
}
@Override
public ObjectMapper getContext(Class<?> type) {
return mapper;
}
}
and register this class in your ResourceConfig
.
If you're using gson, there is probably not much you can do. There is an open issue for supporting unknown properties handlers but so far it hasn't made it to the release. You can find more information about this, including a code snipped to manually check for additional fields, in their issue tracker