接着上篇 WebService小白學習 之 使用jdk實現暴露接口java
本篇介紹使用CXF工具暴露接口,不過多介紹,主要看代碼。web
實現過程:apache
一、在pom.xml添加CXF須要jar包:瀏覽器
<dependencies> <!-- https://mvnrepository.com/artifact/org.apache.cxf/cxf-core --> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-core</artifactId> <version>3.2.5</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.cxf/cxf-rt-frontend-jaxws --> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxws</artifactId> <version>3.2.5</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.cxf/cxf-rt-transports-http-jetty --> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http-jetty</artifactId> <version>3.2.5</version> </dependency> </dependencies>
二、改動上篇的Server.java代碼便可:frontend
package com.gx.server; import org.apache.cxf.jaxws.JaxWsServerFactoryBean; import com.gx.webservice.IHelloWorld; import com.gx.webservice.impl.HelloWorldImpl; public class Server { static String address = "http://127.0.0.1:34563/helloWorld"; public static void main(String[] args) { System.out.println("web service start"); IHelloWorld implementor = new HelloWorldImpl(); JaxWsServerFactoryBean factoryBean = new JaxWsServerFactoryBean(); factoryBean.setAddress(address); //設置暴露地址 factoryBean.setServiceClass(IHelloWorld.class); //設置暴露接口類 factoryBean.setServiceBean(implementor); //設置實現類 factoryBean.create(); //建立webservice接口 System.out.println("web service started"); } }
效果,具體改動內容,運行成功:工具
在瀏覽器網址輸入自定義address:http://127.0.0.1:34563/helloWorld?wsdl,看到結果同樣學習
ok。.net
下篇:code