SpringBoot整合CXF框架开发
在开发过程中遇到了一些使用webservice开发的需求,后查阅资料学习,可上手开发。在学习过程中实现了个小demo,为了养成良好的总结习惯(我还没这”坏习惯“),特意写了个小呆萌,记录一下搭建过程。
-
首先导入必需jar(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.1.2.RELEASE</version>-->
<version>2.0.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.demo</groupId>
<artifactId>springboot-cxf-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot-cxf-demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!-- CXF webservice -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-spring-boot-starter-jaxws</artifactId>
<version>3.2.4</version>
</dependency>
<!-- CXF webservice -->
<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>
-
新建测试服务
UserService.java
package com.demo.springbootcxfdemo.service;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
//指定webservice命名空间
@WebService(targetNamespace = "http://service.springbootcxfdemo.demo.com")
public interface UserService {
@WebMethod(operationName = "getHello")//在wsdl文档中显示的方法名,可不指定默认与方法相同;@WebMethod可不指定
String getHello(@WebParam(name = "name") String name);
}
UserServiceImpl.java
package com.demo.springbootcxfdemo.service.impl;
import com.demo.springbootcxfdemo.service.UserService;
import org.springframework.stereotype.Component;
/**
* @Author Guixing
* @Date 2019/1/17 14:00
* @Description
*/
@Component
public class UserServiceImpl implements UserService {
@Override
public String getHello(String name) {
return "hello "+name;
}
}
-
配置服务访问路径并发布服务
package com.demo.springbootcxfdemo.config;
import com.demo.springbootcxfdemo.service.UserService;
import org.apache.cxf.Bus;
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 javax.xml.ws.Endpoint;
/**
* @Author Guixing
* @Date 2019/1/17 14:01
* @Description
*/
@Configuration
public class CXFConfig {
@Autowired
private Bus bus;
@Autowired
private UserService userService;
/**
* 此方法被注释后:wsdl访问地址为http://127.0.0.1:8080/services/user?wsdl
* 去掉注释后:wsdl访问地址为:http://127.0.0.1:8080/soap/user?wsdl
*/
@Bean
public ServletRegistrationBean dispatcherServlet(){
return new ServletRegistrationBean(new CXFServlet(),"/soap/*");
}
/**
* 发布服务
* 指定访问url
* @return
*/
@Bean
public Endpoint userEndpoint(){
EndpointImpl endpoint = new EndpointImpl(bus,userService);
endpoint.publish("/user");
return endpoint;
}
}
-
测试
浏览器中查看
可以在浏览器中输入http://localhost:8080/soap/user?wsdl来查看
红线圈出为可查看详情的url:http://localhost:8080/soap/user?wsdl=UserService.wsdl
java客户端访问
CXFClient.java
package com.demo.springbootcxfdemo.client;
import com.demo.springbootcxfdemo.service.UserService;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
/**
* @Author Guixing
* @Date 2019/1/17 14:53
* @Description
*/
public class CXFClient {
public static void main(String[] args) {
try {
// 接口地址
String address = "http://127.0.0.1:8080/soap/user?wsdl";
// 代理工厂
JaxWsProxyFactoryBean jaxWsProxyFactoryBean = new JaxWsProxyFactoryBean();
// 设置代理地址
jaxWsProxyFactoryBean.setAddress(address);
// 设置接口类型
jaxWsProxyFactoryBean.setServiceClass(UserService.class);
// 创建一个代理接口实现
UserService us = (UserService) jaxWsProxyFactoryBean.create();
// 数据准备
String name = "zhang";
// 调用代理接口的方法调用并返回结果
String result = us.getHello(name);
System.out.println("返回结果:" + result);
} catch (Exception e) {
e.printStackTrace();
}
}
}
postman测试
请求中的name标签即为@WebParam中name指定的名称,下面为后端返回数据。
写在最后
到这里一个简单的springboot整合cxf框架开发webservice服务的demo就开发完了。
源码地址:https://github.com/zhangguixing/springboot-demo
学习使我快乐!😀