Apache Camel how to fail on unknown properties in unmarshalling

1.3k views Asked by At

I'm working on a system which uses Apache Camel and Spring-boot and I want my routes to return an error if an unknown parameter is sent in the request object.

I know how to do that with Jackson directly (configuring the object-mapper bean) but, in my case, the route is configured without the possibility to pass an opportune custom object mapper.

In fact, in my route I have:

from(...)
.unmarshal().json(JsonLibrary.Jackson, MyInputDtoClass.class)
.process(FirstProcessor.BEAN)
.process(SecondProcessor.BEAN)
.to(OtherRoute.ROUTE) 

If I add the Jackson annotation on my MyInputDtoClass:

@JsonIgnoreProperties(ignoreUnknown = false)
public class MyInputDtoClass {
   ...
}

it still let the request to be unmarshalled even if I add unknown parameters in the Body of the Request. How to block sending unknown properties and return an error?

1

There are 1 answers

0
Siya Ntombela On

You can build a JacksonDataFormat for unmarshalling in your route:

Something like this

JacksonDataFormat dataFormat = new JacksonDataFormat();
dataFormat.setUnmarshalType(MyInputDtoClass.class);

//This will enable the feature you are looking for
dataFormat.enableFeature(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);

from(...)
.unmarshal(dataFormat)
.process(FirstProcessor.BEAN)
.process(SecondProcessor.BEAN)
.to(OtherRoute.ROUTE)