[英]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.