一、新建一个SpringBoot项目
二、引入POM文件
这两个依赖很重要
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.1.12</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>3.1.12</version>
</dependency>
完整的POM文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>wang.chunsen</groupId>
<artifactId>springboot-cxf-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springboot-cxf-demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- CXF webservice -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web-services</artifactId>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.1.12</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>3.1.12</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
<pluginRepository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</project>
三、SpringBoot启动类
package wang.chunsen;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
/**
* @Author itwang
*/
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class SpringbootCxfDemoApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootCxfDemoApplication.class, args);
}
}
注意:SpringBoot启动时会自动加载数据源,所以当没有配置数据库时,@SpringBootApplication的注解应该这么写
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
四、 创建一个接口
注意:@WebService 中参数的意思
name:服务的名称
targetNameSpace :为包名的倒序
package wang.chunsen.service;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
/**
* @Author: itastro
* @Description: webservice 接口
* @Date: Created in 10:34 2018/12/10
* @Package: wang.chunsen.service
* @Modified By:
*/
@WebService(name = "demoService", targetNamespace = "service.chunsen.wang")
public interface DemoService {
@WebMethod
@WebResult(name = "String", targetNamespace = "")
String sayHello(@WebParam(name = "userName") String userName);
}
五、创建实现类
package wang.chunsen.service;
import java.util.Date;
import javax.jws.WebService;
import org.springframework.stereotype.Component;
/**
* @Author: itastro
* @Description: WebServices接口实现类
* @Date: Created in 10:36 2018/12/10
* @Package: wang.chunsen.service
* @Modified By:
*/
@Component("DemoServiceImpl")
@WebService(serviceName = "demoService", //与前面的接口一致
targetNamespace = "http://service.chunsen.wang", //与前面接口一致
endpointInterface = "wang.chunsen.service.DemoService") //接口地址
public class DemoServiceImpl implements DemoService {
@Override
public String sayHello(String user) {
return user + ":hello" + "(" + new Date() + ")";
}
}
六、配置类
package wang.chunsen.config;
import javax.xml.ws.Endpoint;
import org.apache.cxf.Bus;
import org.apache.cxf.bus.spring.SpringBus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.apache.cxf.transport.servlet.CXFServlet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import wang.chunsen.service.DemoService;
/**
* @Author: itastro
* @Description:webService 配置类
* @Date: Created in 10:38 2018/12/10
* @Package: wang.chunsen.config
* @Modified By:
*/
@Configuration
public class CxfConfig {
@Autowired
private DemoService demoService;
@Bean
public ServletRegistrationBean dispatcherServlet() {
/**
* // 发布服务名称 localhost:8090/test
*/
return new ServletRegistrationBean(new CXFServlet(), "/test/*");
}
@Bean(name = Bus.DEFAULT_BUS_ID)
public SpringBus springBus() {
return new SpringBus();
}
/**
* @Description:
* @params []
* @Return: javax.xml.ws.Endpoint
* @Author: itastro
* @Date: 2018/12/10 13:01
*/
@Bean
public Endpoint demoServiceImpl() {
/**
* 绑定要发布的服务实现类
*/
EndpointImpl endpoint = new EndpointImpl(springBus(), demoService);
// 接口访问地址
endpoint.publish("/demoService");
return endpoint;
}
}
七、启动服务,访问http://localhost:8090/test
八、创建客户端
注:动态客户端没有测试通过,不知道为什么
package wang.chunsen.client;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import wang.chunsen.service.DemoService;
/**
* @Author: itastro
* @Description:
* @Date: Created in 15:20 2018/12/10
* @Package: wang.chunsen.client
* @Modified By:
*/
public class Client {
static String url = "http://localhost:8090/test/demoService?wsdl";
public static void main(String[] args) {
one();
}
private static void one() {
//接口地址
//代理工厂
JaxWsProxyFactoryBean jaxWsProxyFactoryBean = new JaxWsProxyFactoryBean();
//设置代理地址
jaxWsProxyFactoryBean.setAddress(url);
//设置接口类型
jaxWsProxyFactoryBean.setServiceClass(DemoService.class);
//创建一个代理接口实现
DemoService demoService = (DemoService) jaxWsProxyFactoryBean.create();
//数据准备
String user = "admin";
//调用代理接口的方法并返回结果
String result = demoService.sayHello(user);
System.out.println(result);
}
private static void two() {
//创建动态客户端
JaxWsDynamicClientFactory jaxWsDynamicClientFactory = JaxWsDynamicClientFactory.newInstance();
org.apache.cxf.endpoint.Client client = jaxWsDynamicClientFactory.createClient(url);
//需要密码的时候卷上用户名和密码
Object[] objects = new Object[10];
try {
// invoke
objects = client.invoke("sayHello", "王");
System.err.println("返回数据:" + objects[0]);
} catch (Exception e) {
}
}
}
测试结果