调用Web服务。 需要缺少的链接

有人可以在下面的代码中填写缺失的链接吗?

第一种方式:

Web服务接口文件是HappyService。

JaxWSProxyFactoryBean factory = new JaxWsProxyFactoryBean(); factory.getInterceptors().add(new LoggingInInterceptor()); factory.getInterceptors().add(new LoggingOutInterceptor()); //MISSING LINK. Where does HappyService.class come from? I don't have it factory.setServiceClass(HappyService.class); factory.setAddress("http://......../happyService"); //Again how do I get HappyService? HappyService client = (HappyService) factory.create(); 

第二种方式:

 String UrlString = "Your WSDL URL"; String nameSpaceUri = "urn:Foo"; String serviceName = "MyHelloService"; String portName = "HelloIFPort"; URL helloWsdlUrl = new URL(UrlString); ServiceFactory serviceFactory = ServiceFactory.newInstance(); Service helloService = serviceFactory.createService(helloWsdlUrl, new QName(nameSpaceUri, serviceName)); //Where did dynamicproxy.HelloIF come from? This code won't compile as that file does not exist anywhere dynamicproxy.HelloIF myProxy = (dynamicproxy.HelloIF) helloService.getPort( new QName(nameSpaceUri, portName), dynamicproxy.HelloIF.class); System.out.println(myProxy.sayHello("Buzz")); 

任何有关这些接口类来自何处以及如何生成它们的线索的人请告诉我。 看起来我可以进行Web服务调用的唯一方法是手工编写SOAP请求,我真的不想这样做,因为它会变得非常大并且容易出错。

有许多工具可以从WSDL定义文件生成Web服务Java类。

您可以尝试JAXB ,它是此任务的标准Java工具。 另一种可能性是Axis ,它的水平更高。

您需要一个SOAP库,例如Apache Axis2 。 该库将包含用于从WSDL生成Java类的工具。 您将使用生成的代码进行Web服务调用。

基于您的第一个示例,我认为您使用CXF框架。

该框架提供了一个名为wsdl2java的任务,它允许从WSDL文件生成类。

生成类后,您可以在代码中使用它们以简单的方式调用Web Service,而无需手动构建SOAP消息。 这是CXF的工作。

我认为如果你在java中引用一些Web服务的基础知识会有所帮助

http://www.oracle.com/technetwork/java/index-jsp-137004.html

http://metro.java.net/