简体   繁体   中英

Apache Camel how to fail on unknown properties in unmarshalling

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?

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM