我有一个带有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}]
经过一段时间的搜索和阅读,我找到了解决方案。
做到这一点的方法是使用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] 删除。
我来说两句