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'未定义的
- 下一篇: Java笔记-EasyCaptcha在前