【问题标题】:Spring Integration calling Apache Solr RESTful serviceSpring Integration 调用 Apache Solr RESTful 服务
【发布时间】:2013-05-24 10:26:50
【问题描述】:

我想使用 Spring Integration 调用 Apache Solr。 Solr 提供类似 RESTful 的搜索功能,例如我想打电话:http://localhost:8983/solr/#/ccy/query?q=id:*&wt=json 这将返回一个 json 字符串。

因此计划是提供一个 ReferenceData 控制器,它调用一个服务,该服务又将通过 spring 集成调用 Solr。但我需要响应是同步的。

我查看了提供的示例 Spring Integration 代码并遇到了 rest-http 示例。但它想在我的头上。那么我该如何做到这一点,任何代码示例都会很有用。

谢谢 通用汽车

【问题讨论】:

    标签: spring-mvc spring-integration


    【解决方案1】:

    其余样本集中在服务器端;在客户端,您需要类似...

    <int:gateway id="toRest" default-request-channel="foo" service-interface="ToRest" />
    
    <int:channel id="foo" />
    
    <int-http:outbound-gateway id="out" request-channel="foo"
        http-method="GET"
        url="http://localhost:8983/solr/ccy/query?q=id:{currency}&amp;wt=json">
            <int-http:uri-variable name="currency" expression="headers['currency']"/>
    </int-http:outbound-gateway>
    

    ToRest 是一个 Java 接口,其方法类似于 String toRest(String in);将 ToRest 实例注入您的控制器,然后发送一个空字符串“”。

    但是,我认为 URL 中间的 # 会给你带来麻烦。

    编辑:

    添加了uri-variable - 表达式可以是任何 SpEL 表达式,例如payload.currency(在消息负载上调用 getCurrency()); headers['currency'];或@someBean.determineCurrency(payload);等等等等。

    您的网关可以填充标题...

    String result(@Payload String payload, @Header("currency") String currency);

    当然,由于您只是在执行 GET,因此您可以简单地在有效负载中设置货币并使用 expression="payload"

    【讨论】:

    • 加里,谢谢,这正是我所做的。是的,删除了“#”,它应该是..../solr/ccy/....我还有一个问题。我需要 URL 的某些部分是动态的,例如?q=id:* 可以更改为 ?q=id:GBP 那么如何在 Spring Integration 中即时更改呢?
    • 谢谢加里。我会试试这个。
    【解决方案2】:

    关于哈希标签,请看:http://en.wikipedia.org/wiki/Fragment_identifier

    片段标识符的功能与 URI 的其余部分不同:即,它的处理完全是客户端,没有 Web 服务器的参与

    【讨论】:

      猜你喜欢
      • 2019-08-04
      • 1970-01-01
      • 2011-07-20
      • 2014-09-24
      • 2019-01-02
      • 1970-01-01
      • 2014-03-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多