多个注释 @JsonPath 不起作用

Multiple annotations @JsonPath are not working

我正在通过 http4 Camel 的组件调用 REST 服务,我想将 JSON 响应映射到某些 headers。出于这个原因,我使用了 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 注释,则会出现问题。

谢谢!

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

使用

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

现在我们的 bean 将与我们想要的一样多的@JsonPath 一起工作。