简体   繁体   English

从Seam调用Web服务

[英]Calling a Web Service from Seam

A simple question, but could someone provide sample code as to how would someone call a web service from within the JBoss Seam framework, and process the results? 一个简单的问题,但是有人可以提供示例代码,说明如何从JBoss Seam框架内调用Web服务,并处理结果?

I need to be able to integrate with a search platform being provided by a private vendor who is exposing his functionality as a web service. 我需要能够与私人供应商提供的搜索平台集成,后者将其功能作为Web服务公开。 So, I'm just looking for some guidance as to what the code for calling a given web service would look like. 所以,我只是在寻找一些关于调用给定Web服务的代码是什么样的指导。

(Any sample web service can be chosen as an example.) (可以选择任何示例Web服务作为示例。)

There's roughly a gajillion HTTP client libraries (Restlet is quite a bit more than that, but I already had that code snippet for something else), but they should all provide support for sending GET requests. 大概有一个高大的HTTP客户端库(Restlet远不止这些,但我已经有了其他的代码片段),但它们都应该支持发送GET请求。 Here's a rather less featureful snippet that uses HttpClient from Apache Commons: 这是一个使用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());
            }
        }
    }
}

This code uses Restlet to make a request to Yahoo's RESTful search service. 此代码使用Restlet向Yahoo的RESTful搜索服务发出请求。 Obviously, the details of the web service you are using will dictate what your client for it looks like. 显然,您使用的Web服务的详细信息将决定您的客户端的外观。

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

So, if I understand this correctly, the above line is where the actual call to the web service is being made, with the response being converted to an appropriate format and manipulated after this line. 因此,如果我理解正确,上面的行就是对Web服务的实际调用,将响应转换为适当的格式并在此行之后进行操作。

Assuming I were not using Restlet, how would this line differ? 假设我没有使用Restlet,这条线会有什么不同?
(Of course, the actual processing code would be significantly different as well, so that's a given.) (当然,实际的处理代码也会有很大差异,所以这是给定的。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM