简体   繁体   English

Apache Camel 如何在解组中对未知属性失败

[英]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.我正在使用 Apache Camel 和 Spring-boot 的系统上工作,如果在请求对象中发送了未知参数,我希望我的路由返回错误。

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.我知道如何直接使用 Jackson(配置对象映射器 bean)来做到这一点,但在我的情况下,路由的配置无法传递合适的自定义对象映射器。

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:如果我在 MyInputDtoClass 上添加 Jackson 注释:

@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:您可以构建 JacksonDataFormat 以在您的路线中解组:

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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