多个注释@JsonPath无法正常工作

时间:2018-05-25 10:05:02

标签: apache-camel jsonpath

我通过http4 Camel的组件调用REST服务,我想将JSON响应映射到某些标头。因此我使用了jsonpath语言。

<route>
   ...
   <toD uri="http4://theCallingServiceUrl"/>

   <setHeader headerName="CamelAddressesLink">
      <jsonpath>$._links.collection/addresses.href</jsonpath>
   </setHeader>
   <setHeader headerName="CamelAvailabilitiesLink">
     <jsonpath>$._links.collection/availabilities.href</jsonpath>
   </setHeader>
</route>

当我尝试通过bean做同样的事情时会出现问题。

public void test(@JsonPath("$._links.collection/addresses.href") String address,
                 @JsonPath("$._links.collection/availabilities.href") String availabilities) {
            ...
}

我收到以下

Caused by: com.jayway.jsonpath.PathNotFoundException: Expected to find an object with property ['_links'] in path $ but found 'java.lang.String'. This is not a json object according to the JsonProvider: 'com.jayway.jsonpath.spi.json.JsonSmartJsonProvider'.

有什么想法吗?如果我尝试从JSON中仅提取一个值,那么如果我尝试添加多个@JsonPath注释就会出现问题。

谢谢!

1 个答案:

答案 0 :(得分:0)

似乎bean组件重置了流缓存。因此,即使我将流缓存设置为路由,在执行bean时也会删除此设置。这就是为什么我们只能使用1个@JsonPath的原因。

使用

在http4组件之后立即将正文转换为String的替代方法
<toD uri="http4://theCallingServiceUrl"/>
<convertBodyTo type="String"/>

现在,我们的bean将可以使用任意数量的@JsonPath。