Apache-camel:如何将json对象(通过curl发送)转换为标头?

开发者X

我有一个带有Apache骆驼的springboot应用程序。在其中,我有一个骆驼语境。我正在尝试通过带有密钥对值的curl发送json并通过路由对其进行处理。

发送数据:

curl --header "Content-Type: application/json" -X POST -d '{"msgId=EB2C7265-EF68-4F8F-A709-BEE2C52E842B", "ticket":"ERR001"}' http://lcalhost:8888/api/erroradmin

Camel-context.xml:

<?xml version="1.0" encoding="utf-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd         http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">
        <camelContext xmlns="http://camel.apache.org/schema/spring" useMDCLogging="true">
            <properties>
                <property key="CamelLogEipName" value="ThisLogger"/>
            </properties>
            <dataFormats>
                <!-- here we define a Json data format with the id jack and that it should use the TestPojo as the class type when
                     doing unmarshal. The unmarshalTypeName is optional, if not provided Camel will use a Map as the type -->
                <json id="jack" library="Jackson" unmarshalTypeName="java.util.HashMap"/>
            </dataFormats>

            <restConfiguration component="jetty" port="8888" bindingMode="json">
                <dataFormatProperty key="prettyPrint" value="true"/>
            </restConfiguration>

            <rest path="/api/erroradmin">
                <get uri="{id}">
                    <to uri="direct:processErrorAdminGet"/>
                </get>
                <post>
                    <to uri="direct:processErrorAdminPost"/>
                </post>
            </rest>

            <route id="processErrorAdminPost">
                <from uri="direct:processErrorAdminPost"/>
                <log message="Route(processErrorAdminPost): ${body}"/>
                <unmarshal>
                    <custom ref="jack"/>
                </unmarshal>
                <log message="Route(processErrorAdminPost): ${body}"/>

            </route>
        </camelContext>
    </beans>

我得到以下Stacktrace:

org.apache.camel.InvalidPayloadException:类型为java.io.InputStream的正文不可用,但具有值:类型为java.util.LinkedHashMap的{msgId = D507B9EE-176D-4F3C-88E7-9E36CC2B9731,ticket = ERR001}: HttpMessage @ 0x28c1a31a。原因:没有可用的类型转换器将类型:java.util.LinkedHashMap转换为所需类型:值{msgId = D507B9EE-176D-4F3C-88E7-9E36CC2B9731,ticket = ERR001}的java.io.InputStream。交换[09395660-c947-47f1-b00f-d0d3030a39d1]。由以下原因引起:[org.apache.camel.NoTypeConversionAvailableException-没有类型转换器可用于从类型:java.util.LinkedHashMap转换为所需类型:值{msgId = D507B9EE-176D-4F3C-88E7-9E36CC2B9731的java.io.InputStream ,ticket = ERR001}]

开发者X

经过一段时间的搜索和阅读,我找到了解决方案。

做到这一点的方法是使用JsonPath。现在,在Java DSL中有很多示例,但是在XML DSL中没有很多示例。我终于找到了一个可行的例子。

我的骆驼上下文现在看起来像这样:

<camelContext xmlns="http://camel.apache.org/schema/spring" useMDCLogging="true" streamCache="true">
    <properties>
        <property key="CamelLogEipName" value="SomeLogger"/>
    </properties>

     <dataFormats>
            <json id="json" library="Jackson"/>     
     </dataFormats>

    <restConfiguration component="jetty" port="9090" >
        <dataFormatProperty key="prettyPrint" value="true"/>
    </restConfiguration>

    <rest path="/api/erroradmin">
        <post>
            <to uri="direct:error" />
        </post>
    </rest>

    <route id="error">
        <from uri="direct:error"/>
        <log message="Route(error): ${body}"/>

        <setHeader headerName="errMessageId">
            <jsonpath suppressExceptions="true">$[0].msgId</jsonpath>
        </setHeader>
        <setHeader headerName="errTicket">
            <jsonpath suppressExceptions="true">$[0].ticket</jsonpath>
        </setHeader>               
        <setHeader headerName="errHandled">
            <jsonpath suppressExceptions="true">$[0].handled</jsonpath>
        </setHeader>

        <log message="Route(error): Header name: errMessageId -> ${header[errMessageId]}"/>
        <log message="Route(error): Header name: errTicket -> ${header[errTicket]}"/>
        <log message="Route(error): Header name: errHandled -> ${header[errHandled]}"/>
    </route>
</camelContext>

访问相应节点的密钥时,我在新设置的标头中获得了值。

JSON的发送方式如下:所以您发送:

curl -XPOST http:// localhost:9090 / api / erroradmin -d'[{“ var1”:10,“ var2”:20}]]'--header“ Content-Type:application / json”

我正在使用的依赖项:

    <dependency>
        <groupId>org.apache.camel</groupId>
        <artifactId>camel-jackson</artifactId>
        <version>${camel.version}</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.9.8</version>
    </dependency>
    <dependency>
        <groupId>org.apache.camel</groupId>
        <artifactId>camel-jsonpath</artifactId>
        <version>${camel.version}</version>
    </dependency>
  </dependencies>

希望将来对某人有益!

编辑:确保使用骆驼2.25.0及更高版本。显然,当使用带有相同核心版本的camel-json 2.24.1时,camel-json将下载一个过时的依赖项json-smart,它将丢失一些类以使JsonPath正常工作。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何将具有键值的JSON文件转换为Apache骆驼标头-Spring DSL

将 JSON 数组转换为对象数组 - Apache camel

使用Apache Camel通过地图列表将CSV文件转换为JSON文件

Apache Camel:如何将分层数据从数据库转换为pojo

无法使用Apache Camel 3.1.0将XML转换为JSON

如何通过标头在Apache Camel JPA中传递namedQuery参数?

Apache camel,RabbitMQ如何发送消息/对象

XML 路由中的 Apache Camel 简单谓词不起作用 - 需要将标头转换为字符串吗?

如何将apache.cfx.header对象转换为OMElement

如何使用Apache Camel转储与HTTP组件一起发送的HTTP正文和标头

Modsecurity和Apache:如何通过标头限制访问速率?

如何使用Apache Camel XmlJsonDataFormat编组将XML字符串转换为JSON字符串

如何使用Apache Nifi将CSV转换为JSON?

将CXF与Apache Camel一起使用时,如何设置WS-Addressing MessageId标头?

执行获取后,Apache Camel标头设置为null

Apache Flink:无法将 Table 对象转换为 DataSet 对象

如何在 Apache Camel 3.XX 路由上设置标头?

如何从Apache Camel中的curl获得结果?

Apache CXF和Apache Camel

如何安装 Apache Camel?

.htaccess标头被Apache忽略

Docker的Apache CORS标头

Apache精简了“ Authorization”标头

Apache Camel:XML 到 JSON 的转换

Apache ssl.conf SSLCipherSuite语法:如何将下划线格式的密码套件名称转换为Apache?

如何将基于Apache Ignite Java的配置转换为Spring XML Config?

Apache Hive:如何将字符串转换为时间戳?

如何将500GB SQL表转换为Apache Parquet?

如何将数据转换为所需格式并写入文件 - Python + Apache Beam