这里使用的springboot版本是2.0.0,cxf的版本是3.1.12。之前在做这个demo时,因为springboot版本的问题,掉在坑里许久出不来。最开始使用springboot2.1.15版本,一直在报错,所以先声明版本。
这个demo比较简单一个服务端生成webservice服务,一个客户端调用webservice服务
服务端pom.xml
<?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>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web-services</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.58</version>
</dependency>
<!--cxf依赖-->
<!-- 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.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>
</project>
客户端的pom.xml文件同上
1、声明一个服务接口
package com.example.demo.webservice;
import javax.jws.WebService;
//name是要发布出去的服务名,targetNamespace是服务的命名空间,惯例使用包名反序
@WebService(name = "MyWebService",targetNamespace="http://webservice.demo.com")
public interface MyWebService {
//接口提供的方法
String getName(String name);
}
2、写一个实现类,实现1中的接口
package com.example.demo.webservice.impl;
import com.example.demo.webservice.MyWebService;
import javax.jws.WebService;
/**
*此处的name、targetNamespace 和接口中的相同,endpointInterface 为1中的接口全路径
*就是要对外提供服务的接口
*/
@WebService(serviceName = "MyWebService",targetNamespace = "http://webservice.demo.com",
endpointInterface = "com.example.demo.webservice.MyWebService")
public class MyWebServiceImpl implements MyWebService {
@Override
public String getName(String name) {
return "WebService测试成功:" + name;
}
}
3、服务端配置类
package com.example.demo.webservice;
import com.example.demo.webservice.impl.MyWebServiceImpl;
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.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.xml.ws.Endpoint;
@Configuration
public class WebServiceCfg {
@Bean
public ServletRegistrationBean dispatcherServlet(){
return new ServletRegistrationBean(new CXFServlet(),"/demo/*");
}
@Bean(name = Bus.DEFAULT_BUS_ID)
public SpringBus springBus(){
return new SpringBus();
}
@Bean
public MyWebService myWebService(){
return new MyWebServiceImpl();
}
@Bean
public Endpoint endpoint(){
EndpointImpl endpoint = new EndpointImpl(springBus(), myWebService());
endpoint.publish("/zb");
return endpoint;
}
}
4、启动springboot项目,访问地址 http://localhost:8080/demo/zb?wsdl,会出现以下画面,表示服务发布成功
5、实现一个测试的客户端
package com.example.client.webservice;
import com.example.demo.webservice.MyWebService;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
public class WebServiceTest {
public static void main(String[] args) {
//WebServiceTest.test1();
WebServiceTest.test2();
}
/**
* 1.代理类工厂的方式,需要拿到对方的接口地址
*/
public static void test1() {
try {
// 接口地址
String address = "http://127.0.0.1:8080/demo/zb?wsdl";
// 代理工厂
JaxWsProxyFactoryBean jaxWsProxyFactoryBean = new JaxWsProxyFactoryBean();
// 设置代理地址
jaxWsProxyFactoryBean.setAddress(address);
// 设置接口类型
jaxWsProxyFactoryBean.setServiceClass(MyWebService.class);
// 创建一个代理接口实现
MyWebService us = (MyWebService) jaxWsProxyFactoryBean.create();
String name = us.getName("张半仙");
System.out.println(name);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 2:动态调用
*/
public static void test2() {
// 创建动态客户端
JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
Client client = dcf.createClient("http://127.0.0.1:8080/demo/zb?wsdl");
// 需要密码的情况需要加上用户名和密码
// client.getOutInterceptors().add(new MyInterceptor(USER_NAME, PASS_WORD));
Object[] objects = new Object[0];
try {
// invoke("方法名",参数1,参数2,参数3....);
objects = client.invoke("getName", "张半仙");
System.out.println("返回数据:" + objects[0]);
} catch (java.lang.Exception e) {
e.printStackTrace();
}
}
}
6、执行5中代码会打印以下的值,即调用成功
至此,一个简单的webservice服务端和客户端的demo完成