标签:wsdl wsdl2java webservice cxf
Wsdl2java是cxf提供的一个用于生成客户端代码的工具,它的功能跟wsimport差不多。但是wsdl2java工具只能生成访问基于cxf发布的服务的代码。Wsdl2java位于cxf_home/bin目录下,因此使用这个工具时需要添加目录到path中。Wsdl2java工具的参数与wsimport有所不同,主要有:
-d 参数,指定生成代码的目录
-p 参数,指定生成的新的包结构
(Wsdlurl) –http://server.port/service?wsdl,webservice的wsdl地址,必须的参数
新建java project,命名为HelloCXF,导入CXF相关的jar包。
在src目录中新建com.hellocxf.service包,并新建People类,People类代码为:
package com.hellocxf.service; import javax.jws.WebMethod; import javax.jws.WebService; @WebService public interface People { publicString sayHello(String name); //@WebMethod(exclude=true) publicString makeFriend(String name); }
在src目录中新建com.hellocxf.serviceImpl包,并新建Student类,Student代码为:
package com.hellocxf.serviceImpl; import javax.jws.WebMethod; import javax.jws.WebService; import com.hellocxf.service.People; @WebService public class Student implements People { @Override publicString sayHello(String name) { //TODO Auto-generated method stub System.out.println("Hello:"+name); returnname; } @WebMethod(exclude=true) @Override publicString makeFriend(String name) { //TODO Auto-generated method stub System.out.println("makefriend with "+name+" student"); returnname; } }
在src目录新建com.hellocxf.servicePublice包,并创建ServicePublish类,用于发布webservice.ServicePublish代码为:
package com.hellocxf.servicePublish; import javax.xml.ws.Endpoint; import com.hellocxf.service.People; import com.hellocxf.serviceImpl.Student; public class ServicePublish { publicstatic void main(String[] args) { //TODO Auto-generated method stub PeoplemService = new Student(); Stringaddress = "http://localhost:8080/People"; Endpoint.publish(address,mService); } }
运行ServicePublish类,发布webservice服务。
在D盘新建文件夹generatedCode
打开cmd命令,切换到该目录下
运行wsdl2java –d . http://localhost:8080/People?wsdl,在generatedCode文件夹下生成访问服务的代码。
在eclipse中新建java工程,命名为CXFClient
将第三部生成的.java类文件整体复制到src目录下.
在src目录下新建com.webservice.use包,在包中新建类ServiceUse.用于完成对webservice的调用,代码为:
package com.webservice.use; import com.hellocxf.serviceimpl.People; import com.hellocxf.serviceimpl.StudentService; public class ServiceUse { publicstatic void main(String[] args) { //TODO Auto-generated method stub //<wsdl:servicename="StudentService"> StudentServicestudentService = new StudentService(); //<wsdl:portbinding="tns:StudentServiceSoapBinding"name="StudentPort"> //<wsdl:bindingname="StudentServiceSoapBinding" type="ns1:People"> Peoplepeople = studentService.getStudentPort(); StringserviceResult = people.sayHello("yinyuchun"); System.out.println(serviceResult); } }
类ServiceUse代码中创建的service类型和调用的端口、方法都是从wsdl获取的。
标签:wsdl wsdl2java webservice cxf
原文地址:http://blog.csdn.net/longshengguoji/article/details/41988311