2、定义将要被暴露成webservice的接口
package com.xzb.demo.interfaces; import javax.jws.WebService; @WebService public interface HelloWorld { public String sayHello(String name); }
3、定义实现接口的Impl
package com.xzb.demo.impl; import javax.jws.WebParam; import javax.jws.WebService; import javax.jws.soap.soAPBinding; import javax.jws.soap.soAPBinding.Style; import com.xzb.demo.interfaces.HelloWorld; @WebService @SOAPBinding(style=Style.RPC) public class HelloWorldImpl implements HelloWorld { public String sayHello(@WebParam(name="name")String name) { return name+" say:Hello World!"; } }
4、发布及访问web Service
(1)代码实现
发布web Service:
package com.xzb.demo.serviceApp; import javax.xml.ws.Endpoint; import com.xzb.demo.impl.HelloWorldImpl; public class WebServiceApp { public static void main(String[] args) { System.out.println("web service start"); HelloWorldImpl implementor= new HelloWorldImpl(); String address="http://localhost:80/helloWorld"; Endpoint.publish(address,implementor); System.out.println("web service started"); } }
运行以上程序后,访问地址http://localhost/helloWorld?wsdl,即可查看到对应的wsdl。
This XML file does not appear to have any style @R_542_4045@ion associated with it. The document tree is shown below. <wsdl:deFinitions xmlns:ns1="http://interfaces.demo.xzb.com/" xmlns:ns2="http://schemas.xmlsoap.org/soap/http" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://impl.demo.xzb.com/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="HelloWorldImplService" targetNamespace="http://impl.demo.xzb.com/"> <wsdl:import location="http://localhost/helloWorld?wsdl=HelloWorld.wsdl" namespace="http://interfaces.demo.xzb.com/"></wsdl:import> <wsdl:binding name="HelloWorldImplServiceSoapBinding" type="ns1:HelloWorld"> <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/> <wsdl:operation name="sayHello"> <soap:operation soapAction="" style="document"/> <wsdl:input name="sayHello"> <soap:body use="literal"/> </wsdl:input> <wsdl:output name="sayHelloResponse"> <soap:body use="literal"/> </wsdl:output> </wsdl:operation> </wsdl:binding> <wsdl:service name="HelloWorldImplService"> <wsdl:port binding="tns:HelloWorldImplServiceSoapBinding" name="HelloWorldPort"> <soap:address location="http://localhost:80/helloWorld"/> </wsdl:port> </wsdl:service> </wsdl:deFinitions>备注:虽然在Impl中采用注释的方式标识接口方法所需参数@WebParam(name="name"),但不起作用。试了之后,发现如果接口不采用@WebService,只在接口实现类上采用@WebService,@WebParam(name="name"),在wsdl上即可看到所需参数和返回值等信息。
客户端访问web Service:
package com.xzb.demo.serviceClient; import org.apache.cxf.jaxws.JaxWsProxyfactorybean; import com.xzb.demo.interfaces.HelloWorld; public class HelloWorlClient { public static void main(String[] args) throws Exception { JaxWsProxyfactorybean svr = new JaxWsProxyfactorybean(); svr.setServiceClass(HelloWorld.class); svr.setAddress("http://localhost:80/helloWorld"); HelloWorld hw = (HelloWorld) svr.create(); System.out.println(hw.sayHello("xzb")); } }
client运行结果:
2013-10-18 10:38:49 org.apache.cxf.service.factory.ReflectionServicefactorybean buildServiceFromClass 信息: Creating Service {http://interfaces.demo.xzb.com/}HelloWorldService from class com.xzb.demo.interfaces.HelloWorld xzb say:Hello World!
(2)与spring结合实现
在spring配置文件上发布webservice和客户端访问webservice的配置。配置代码如下:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"> <import resource="classpath:meta-inf/cxf/cxf.xml"/> <import resource="classpath:meta-inf/cxf/cxf-extension-soap.xml"/> <import resource="classpath:meta-inf/cxf/cxf-servlet.xml"/> <!--在项目的 /helloWorld 路径将 com.xzb.demo.impl.HelloWorldImpl 暴露出去--> <jaxws:endpoint id="helloWorld" implementor="com.xzb.demo.impl.HelloWorldImpl" address="/helloWorld" /> <!-- 客户端访问webservice配置 --> <bean id="client" class="com.xzb.demo.interfaces.HelloWorld" factory-bean="clientFactory" factory-method="create"/> <bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyfactorybean"> <property name="serviceClass" value="com.xzb.demo.interfaces.HelloWorld"/> <property name="address" value="http://localhost:80/CXFWebServiceTest/webservice/helloWorld"/> </bean> </beans>
然后,访问http://localhost/CXFWebServiceTest/webservice/helloWorld?wsdl可以查看wsdl,client只要通过访问http://localhost:80/CXFWebServiceTest/webservice/helloWorld,就可以调用到接口com.xzb.demo.interfaces.HelloWorld提供的方法。
package com.xzb.demo.serviceClient; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClasspathXmlApplicationContext; import com.xzb.demo.interfaces.HelloWorld; public class HelloWorlClient { public static void main(String[] args) throws Exception { ApplicationContext context = new ClasspathXmlApplicationContext("applicationContext.xml"); HelloWorld client = (HelloWorld)context.getBean("client"); System.out.println(client.sayHello("xzb")); } }
初步接触cxf,尚有很多不清楚的地方,如果错误,请指正。(以上client访问webservice,必须依赖于service端的接口,才能调用到正确的方法,但感觉从wsdl上得出信息貌似不太容易,因为目前还不太熟悉。)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。