0

我正在尝试使用 Apache Camel 调用 Yahoo API。

雅虎 API 是 http://query.yahooapis.com/v1/public/yql?q=select%20 *%20from%20music.artist.search%20where%20keyword%3D%22Madonna%22&format=json

我的路线看起来像这样。

    from("direct:start_yahoo_artist")
            .process(new HTTPRequestParamProcessor())
            .setHeader(
                    Exchange.HTTP_QUERY,
                    simple("select+*+from+music.artist.search+where+keyword%3D%{in.headers.artist}%22&format=json"))
                    //simple("select * from music.artist.search where keyword=\"{in.headers.artist}\"&format=json"))
            .to("http://query.yahooapis.com/v1/public/yql")
            .unmarshal()
            .json(JsonLibrary.Jackson, YahooMusicArtistResponseObject.class)
            /*.bean(EmbeddedDroolsRuleEngine.class, "callRuleEngine")*/
            .process(new Processor() {

                @Override
                public void process(Exchange exchange) throws Exception {
                    exchange.getOut().setBody(exchange.getIn().getBody());

                }
            });    

但是,我收到了一个无效的查询异常。编码 URI 的正确方法是什么?

[               qtp263093125-29] Tracer                         INFO  ID-server190-tm-rtsslab-64570-1345237236639-0-2 >>> (route6)  --> http://query.yahooapis.com/v1/public/yql <<< Pattern:InOut, Headers:{CamelHttpMethod=GET, artist=Madonna, breadcrumbId=ID-server190-tm-rtsslab-64570-1345237236639-0-1, CamelHttpQuery=select+*+from+music.artist.search+where+keyword%3D%{in.headers.artist}%22&format=json}, BodyType:null, Body:[Body is null]
[               qtp263093125-29] DefaultErrorHandler            ERROR Failed delivery for (MessageId: ID-server190-tm-rtsslab-64570-1345237236639-0-3 on ExchangeId: ID-server190-tm-rtsslab-64570-1345237236639-0-2). Exhausted after delivery attempt: 1 caught: org.apache.commons.httpclient.URIException: Invalid query
org.apache.commons.httpclient.URIException: Invalid query
    at org.apache.commons.httpclient.URI.parseUriReference(URI.java:2049)[commons-httpclient-3.1.jar:]
    at org.apache.commons.httpclient.URI.<init>(URI.java:147)[commons-httpclient-3.1.jar:]
    at org.apache.commons.httpclient.HttpMethodBase.getURI(HttpMethodBase.java:265)[commons-httpclient-3.1.jar:]
    at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:383)[commons-httpclient-3.1.jar:]
    at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:323)[commons-httpclient-3.1.jar:]
    at org.apache.camel.component.http.HttpProducer.executeMethod(HttpProducer.java:243)[camel-http-2.10.0.jar:2.10.0]
4

1 回答 1

2

看这部分:

+keyword%3D%{in.headers.artist}%22

简单内联变量的正常方法${in.headers.artist}不是%{in.headers.artist},但您可能已将其配置为{ } ?

但是您在艺术家字符串之前没有引号,只有在 - 这与您上面的工作 URL 不同。

网址的这一部分不应该是:+keyword%3D%22${in.headers.artist}%22 (+keyword="madonna")

于 2012-08-18T06:28:18.330 回答