簡體   English   中英

從Seam調用Web服務

[英]Calling a Web Service from Seam

一個簡單的問題,但是有人可以提供示例代碼,說明如何從JBoss Seam框架內調用Web服務,並處理結果?

我需要能夠與私人供應商提供的搜索平台集成,后者將其功能作為Web服務公開。 所以,我只是在尋找一些關於調用給定Web服務的代碼是什么樣的指導。

(可以選擇任何示例Web服務作為示例。)

大概有一個高大的HTTP客戶端庫(Restlet遠不止這些,但我已經有了其他的代碼片段),但它們都應該支持發送GET請求。 這是一個使用Apache Commons的HttpClient的一個相當不太功能的代碼片段:

HttpClient client = new HttpClient();
HttpMethod method = new GetMethod("http://api.search.yahoo.com/WebSearchService/V1/webSearch?appid=restbook&query=HttpClient");
client.executeMethod(method);
import org.restlet.Client;
import org.restlet.data.Protocol;
import org.restlet.data.Reference;
import org.restlet.data.Response;
import org.restlet.resource.DomRepresentation;
import org.w3c.dom.Node;

/**
 * Uses YAHOO!'s RESTful web service with XML.
 */
public class YahooSearch {
    private static final String BASE_URI = "http://api.search.yahoo.com/WebSearchService/V1/webSearch";

    public static void main(final String[] args) {
        if (1 != args.length) {
            System.err.println("You need to pass a search term!");
        } else {
            final String term = Reference.encode(args[0]);
            final String uri = BASE_URI + "?appid=restbook&query=" + term;
            final Response response = new Client(Protocol.HTTP).get(uri);
            final DomRepresentation document = response.getEntityAsDom();

            document.setNamespaceAware(true);
            document.putNamespace("y", "urn:yahoo:srch");

            final String expr = "/y:ResultSet/y:Result/y:Title/text()";
            for (final Node node : document.getNodes(expr)) {
                System.out.println(node.getTextContent());
            }
        }
    }
}

此代碼使用Restlet向Yahoo的RESTful搜索服務發出請求。 顯然,您使用的Web服務的詳細信息將決定您的客戶端的外觀。

final Response response = new Client(Protocol.HTTP).get(uri);

因此,如果我理解正確,上面的行就是對Web服務的實際調用,將響應轉換為適當的格式並在此行之后進行操作。

假設我沒有使用Restlet,這條線會有什么不同?
(當然,實際的處理代碼也會有很大差異,所以這是給定的。)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM