2. pox.xml文件添加jar包java
<dependencies> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-core</artifactId> <version>3.1.5</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxws</artifactId> <version>3.1.5</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http-jetty</artifactId> <version>3.1.5</version> </dependency> </dependencies>
3. 主方法: 而後啓動服務web
package com.java1234.webservice.impl; import javax.xml.ws.Endpoint; import org.apache.cxf.jaxws.JaxWsServerFactoryBean; import com.java1234.webservice.HelloWorld; public class Server { public static void main(String[] args) { System.out.println("web service start"); HelloWorld implementor=new HelloWorldImpl(); String address="http://192.168.244.1/helloWorld"; // Endpoint.publish(address, implementor); // jdk實現 暴露webservice接口 JaxWsServerFactoryBean factoryBean=new JaxWsServerFactoryBean(); factoryBean.setAddress(address); // 設置暴露地址 factoryBean.setServiceClass(HelloWorld.class); // 接口類 factoryBean.setServiceBean(implementor); // 設置實現類 factoryBean.create(); // 建立webservice接口 System.out.println("web service started"); } }
二.新建ws_Clientapache
2. 下載文件 apache-cxf-3.1.5.zip,而且解壓架構
新建一個文件夾: frontend
運行命令以下:maven
F:\apache-cxf-3.1.5\bin\wsdl2java http://192.168.244.1/helloWorld?wsdlspa
會生成樹結構文件: 以下code
3.編寫client.javaserver
package com.java1234.webservice; import java.util.List; public class Client { public static void main(String[] args) { HelloWorldService service=new HelloWorldService(); HelloWorld helloWorld=service.getHelloWorldPort(); System.out.println(helloWorld.say("java1234_小峯")); User user=new User(); user.setUserName("java1234"); user.setPassword("123456"); List<Role> roleList=helloWorld.getRoleByUser(user); for(Role role:roleList){ System.out.println(role.getId()+","+role.getRoleName()); } } }
程序運行結果:xml
Hello:java1234_小峯 1,技術總監 2,架構師 |