springboot版本和cfx依赖应该是有对应关系的,中间用过springboot2.5的版本启动会报错。
1、首先是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 https://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.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.jp</groupId>
<artifactId>webservice</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>webservice</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</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<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.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.2.5</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>3.2.5</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.78</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2、编写一个简单的实体类,用来测试:
public class Student implements Serializable {
private static final long serialVersionUID = -5077842228349746612L;
private String name;
private Integer age;
private String hobby;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getHobby() {
return hobby;
}
public void setHobby(String hobby) {
this.hobby = hobby;
}
public Student() {
}
public Student(String name, Integer age, String hobby) {
this.name = name;
this.age = age;
this.hobby = hobby;
}
}
3、编写webservice接口类,提供接口服务,和普通的接口类一样,可以写多个方法。如下:
@WebService(name = "DemoService", // 暴露服务名称
targetNamespace = "http://webservice.jp.com"// 命名空间,一般是接口的包名倒序
)
public interface DemoService {
/**
* 暴露的方法 可以写多个
* WebMethod注解可以改变方法暴露的名称 比如这里方法名称原本是sayHello 注解添加后在服务页面展示为test
* WebParam注解中的name属性用来对参数进行解释 会展示在服务页面 方便调用方
*
* @param name 测试参数
* @param age 测试参数
* @param hobby 测试参数
* @return Object
*/
@WebMethod(operationName = "test")
public Object sayHello(
@WebParam(name = "姓名") String name,
@WebParam(name = "年龄") Integer age,
@WebParam(name = "爱好") String hobby);
}
4、编写接口的实现类:
@WebService(
serviceName = "DemoService", // 与接口中指定的name一致
targetNamespace = "http://webservice.jp.com", // 与接口中的命名空间一致,一般是接口的包名倒
endpointInterface = "com.jp.webservice.DemoService"// 指向接口所在路径
)
public class DemoServiceImpl implements DemoService {
@Override
public Object sayHello(String name, Integer age, String hobby) {
return JSON.toJSONString(new Student(name, age, hobby));
}
}
5、配置类,这里面会涉及到访问webservcie的路径,注释中都有解释。如下:
@Configuration
public class CxfConfig {
@Bean
public ServletRegistrationBean<CXFServlet> dispatcherServlet() {
// 这里配置的demo是服务访问的一级路径 最终服务页面的访问路径为:http://localhost:8080/demo/api?wsdl
return new ServletRegistrationBean<>(new CXFServlet(), "/demo/*");
}
@Bean(name = Bus.DEFAULT_BUS_ID)
public SpringBus springBus() {
return new SpringBus();
}
@Bean
public DemoService demoService() {
return new DemoServiceImpl();
}
@Bean
public Endpoint endpoint() {
EndpointImpl endpoint = new EndpointImpl(springBus(), demoService());
// 这里配置的api是访问的二级路径 最终服务页面的访问路径为:http://localhost:8080/demo/api?wsdl
endpoint.publish("/api");
return endpoint;
}
}
到这里启动springboot服务,访问路径http://localhost:8080/demo就会看到如下界面:
下面写一个简单的接口去调用webservcie提供的借口,我是重新建了一个项目,项目依赖和上面保持一致,主要代码如下:
@RestController
@RequestMapping("/test")
public class TestController {
@GetMapping
public Object getResponse() throws Exception {
// 创建动态客户端
JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
// 这里的路径对应wsdl服务路径
Client client = dcf.createClient("http://localhost:8080/demo/api?wsdl");
// test为方法名字 后面依次拼接方法参数
Object[] objects = client.invoke("test", "小明", 1, "听歌");
return objects;
}
}
通过postman调用该接口,会调用webservice提供的test接口,返回结果如下:
[
"{\"age\":1,\"hobby\":\"听歌\",\"name\":\"小明\"}"
]