如何在沒有 Spring 的情況下從 XML 片段實例化 Camel 組件?說,我有一個片段,是這樣的:<dataFormats> <json id="jack" library="Jackson" prettyPrint="true"/>....</dataFormats>,我想對其進行解析并有效地為我返回,因為JsonDataFormat jack = new JsonDataFormat(JsonLibrary.Jackson);jack.setPrettyPrint(true);甚至是這樣的事情,我希望能夠從以下位置實例化處理器:<setHeader headerName="inputRegistryHandler"> <simple>${header.CamelFileNameOnly}</simple></setHeader><mvel>request.headers.foo == 'bar'</mvel>等等希望在 Camel 3 中不會放棄 XML 支持?解決方案按照下面的@Sergei I. 建議,我寫了如下內容JAXBContext jc = JAXBContext.newInstance("org.apache.camel.model:org.apache.camel.model.language");Unmarshaller unmarshal = jc.createUnmarshaller();ProcessorDefinition pd = (ProcessorDefinition) unmarshal.unmarshal(serviceDefinitionElement);rc = new DefaultRouteContext(_camelContext);Processor processor = pd.createProcessor(rc);_camelContext.addService(processor, true);它吃了一個 DOM 元素,其中包含<camel:setBody> <camel:simple>mike check one two</camel:simple></camel:setBody>像魅力一樣,創造一個功能強大的處理器可能我需要添加更多的 java 包才能解析更多類型的 Camel XML 片段更新 我必須添加_camelContext.addService(processor, true);到上面的解決方案中。這個神奇的東西將所有 Camel Context bean 加載到處理器中,這使我們能夠省略一些配置。例如,這將在ObjectMapper沒有我們特別提及的情況下添加到 Jackson 數據格式,并且prettyPrint標志開始被resected:<camel:unmarshal> <camel:jacksonxml unmarshalTypeName="java.util.Map"/></camel:unmarshal><camel:marshal> <camel:json library="Jackson" prettyPrint="true"/></camel:marshal>
2 回答

慕容708150
TA貢獻1831條經驗 獲得超4個贊
Camel 使用 JAXB 來編組和解組 XML。XML 模型在org.apache.camel.model包中定義。您可以嘗試以下方法:
JAXBContext jaxbContext = JAXBContext.newInstance(SetHeaderDefinition.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
SetHeaderDefinition setHeaderDefinition = (SetHeaderDefinition) jaxbUnmarshaller.unmarshal(file);

一只斗牛犬
TA貢獻1784條經驗 獲得超2個贊
CamelContext 類具有允許加載 xml 文件的函數 loadRoutesDefinition。
XML 文件應具有如下結構:
<routes xmlns="http://camel.apache.org/schema/spring">
<route>
<from uri.....
</route>
</routes>
添加回答
舉報
0/150
提交
取消