Java工作笔记-使用CXF接入及创建WebService
生活随笔
收集整理的這篇文章主要介紹了
Java工作笔记-使用CXF接入及创建WebService
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
這里我采用Spring Boot進行項目啟動。
關于CXF要添加的Maven:
<dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-rt-frontend-jaxws</artifactId><version>3.1.1</version></dependency><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-rt-transports-http</artifactId><version>3.1.1</version></dependency><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-rt-frontend-jaxrs</artifactId><version>3.1.1</version></dependency><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-rt-transports-http-jetty</artifactId><version>3.1.1</version></dependency><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-simple</artifactId><version>1.7.12</version></dependency>使用CXF的流程:
1. 設置發布地址:
2. 設置訪問接口;
3. 設置服務實現對象;
4. 通過工廠進行創建;
程序結構如下:
weatherService.java
package cn.demo.webService;import javax.jws.WebService;@WebService public interface WeatherService {String query(String cityName); }WeatherServiceImpl.java
package cn.demo.webService;import java.util.Random;public class WeatherServiceImpl implements WeatherService {public String query(String cityName) {Random random = new Random();int n = random.nextInt(3);System.out.println("The n is " + n);String ret = "null";switch (n){case 0:ret = "晴天";break;case 1:ret = "陰天";break;case 3:ret = "雨天";}return ret;} }Main.java
package cn.demo;import cn.demo.webService.WeatherService; import cn.demo.webService.WeatherServiceImpl; import org.apache.cxf.jaxws.JaxWsServerFactoryBean; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication public class Main implements CommandLineRunner {public static void main(String[] args){SpringApplication.run(Main.class, args);}public void run(String... args) throws Exception {JaxWsServerFactoryBean factoryBean = new JaxWsServerFactoryBean();//設置發布的地址factoryBean.setAddress("http://localhost:9998/weatherService");//設置訪問接口factoryBean.setServiceClass(WeatherService.class);//設置服務的實現對象factoryBean.setServiceBean(new WeatherServiceImpl());//通過工廠創建服務factoryBean.create();System.out.println("發布服務成功!");} }項目源碼如下:
https://github.com/fengfanchen/Java/tree/master/CXFService
?
下面是接入:
同樣也是采用Spring Boot啟動
在此目錄下運行wsdl2java
E:\IdeaProject2019\CallOtherWebService\CXFClient\src\main\java>wsdl2java -d . http://localhost:9998/weatherService?wsdl生成的東西都是在webservice文件夾中。
這里wsdl2java在互聯網上下載就可以了!
這里要配置好java環境變量以及添加CXF環境變量。
訪問邏輯如下:
1. 設置服務地址;
2. 設置服務接口;
3. 通過工廠獲取對象;
4. 調用接口;
源碼如下Main.java
package cn.demo;import cn.demo.webservice.WeatherService; import org.apache.cxf.jaxws.JaxWsProxyFactoryBean; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication public class Main implements CommandLineRunner {public static void main(String[] args){SpringApplication.run(Main.class, args);}public void run(String... strings) throws Exception {JaxWsProxyFactoryBean proxyFactoryBean = new JaxWsProxyFactoryBean();//設置服務地址proxyFactoryBean.setAddress("http://localhost:9998/weatherService");//設置服務接口proxyFactoryBean.setServiceClass(WeatherService.class);//通過工廠獲取對象WeatherService service = (WeatherService)proxyFactoryBean.create();String nj = service.query("南京");System.out.println(nj);} }源碼下載地址:
https://github.com/fengfanchen/Java/tree/master/CXFClient
?
?
總結
以上是生活随笔為你收集整理的Java工作笔记-使用CXF接入及创建WebService的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C++工作笔记-对'xxxxx'未定义的
- 下一篇: Android安全笔记-Tasks与Re