Cxf的springboot集成使用
特别注意,用jdk11请加上这个包,jdk11已经将该包移除了
<dependency> <groupId>com.sun.xml.ws</groupId> <artifactId>jaxws-ri</artifactId> <version>2.3.3</version> <type>pom</type> </dependency>
一:项目的目录结构
二:pon.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 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.2.2.RELEASE</version> <relativePath/> <!– lookup parent from repository –> </parent>--> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.3.RELEASE</version> <relativePath/> </parent> <groupId>com.kexin</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-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web-services</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!--WerbService CXF依赖--> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxws</artifactId> <version>3.2.6</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http</artifactId> <version>3.2.6</version> </dependency> <!-- <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> </dependency>--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
三:webservice服务编写
3.1实体编写

package com.kexin.webservice.entity; import java.io.Serializable; /** * @Description: * @Author: 巫恒强 * @Date: 2019/12/17 15:28 */ public class User implements Serializable { private static final long serialVersionUID = -3628469724795296287L; private String userId; private String userName; private String email; public String getUserId() { return userId; } public void setUserId(String userId) { this.userId = userId; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } @Override public String toString() { return "User{" + "userId='" + userId + '\'' + ", userName='" + userName + '\'' + ", email='" + email + '\'' + '}'; } }
3.2userservice服务接口
package com.kexin.webservice.service; import com.kexin.webservice.entity.User; import javax.jws.WebMethod; import javax.jws.WebParam; import javax.jws.WebResult; import javax.jws.WebService; import java.util.Map; /** * @Description: * @Author: 巫恒强 * @Date: 2019/12/17 15:29 */ //@WebService(targetNamespace="http://service.springboot.mracale.com")如果不添加的话,动态调用invoke的时候,会报找不到接口内的方法,具体原因未知. @WebService(targetNamespace="http://service.webservice.kexin.com") public interface UserService { @WebMethod//标注该方法为webservice暴露的方法,用于向外公布,它修饰的方法是webservice方法,去掉也没影响的,类似一个注释信息。 public User getUser(@WebParam(name = "userId") String userId); @WebMethod @WebResult(name="String",targetNamespace="") public String getUserName(@WebParam(name = "userId") String userId); @WebMethod @WebResult(name="Map") public Map<String, User> getAllUserData(); }
3.3userservice服务实现
package com.kexin.webservice.service.impl; import java.util.HashMap; import java.util.Map; import java.util.UUID; import javax.jws.WebService; import com.kexin.webservice.entity.User; import com.kexin.webservice.service.UserService; import org.springframework.stereotype.Component; /** * @Description: * @Author: 巫恒强 * @Date: 2019/12/17 15:32 */ /** * @ClassName:UserServiceImpl * @Description:测试服务接口实现类 */ @WebService(serviceName="UserService",//对外发布的服务名 targetNamespace="http://service.webservice.kexin.com",//指定你想要的名称空间,通常使用使用包名反转 endpointInterface="com.kexin.webservice.service.UserService")//服务接口全路径, 指定做SEI(Service EndPoint Interface)服务端点接口 @Component public class UserServiceImpl implements UserService { private Map<String, User> userMap = new HashMap<String, User>(); public UserServiceImpl() { System.out.println("向实体类插入数据"); User user = new User(); user.setUserId(UUID.randomUUID().toString().replace("-", "")); user.setUserName("mracale01"); user.setEmail("mracale01@163.xom"); userMap.put(user.getUserId(), user); user = new User(); user.setUserId(UUID.randomUUID().toString().replace("-", "")); user.setUserName("mracale02"); user.setEmail("mracale02@163.xom"); userMap.put(user.getUserId(), user); user = new User(); user.setUserId(UUID.randomUUID().toString().replace("-", "")); user.setUserName("mracale03"); user.setEmail("mracale03@163.xom"); userMap.put(user.getUserId(), user); } @Override public String getUserName(String userId) { return "userId为:" + userId; } @Override public Map<String, User> getAllUserData() { return userMap; } @Override public User getUser(String userId) { System.out.println("userMap是:"+userMap); return userMap.get(userId); } }
四:webservice的全局配置
package com.kexin.webservice.config; import com.kexin.webservice.service.UserService; import com.kexin.webservice.service.impl.UserServiceImpl; 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; /** * @Description: * @Author: 巫恒强 * @Date: 2019/12/17 15:37 */ @Configuration public class CxfConfig { /** * 此方法作用是改变项目中服务名的前缀名,此处127.0.0.1或者localhost不能访问时,请使用ipconfig查看本机ip来访问 * 此方法被注释后:wsdl访问地址为http://127.0.0.1:8080/services/user?wsdl * 去掉注释后:wsdl访问地址为:http://127.0.0.1:8080/kexin/webservice/user?wsdl
* @return */ @SuppressWarnings("all") @Bean public ServletRegistrationBean dispatcherServlet() { return new ServletRegistrationBean(new CXFServlet(), "/kexin/webservice/*"); } @Bean(name = Bus.DEFAULT_BUS_ID) public SpringBus springBus() { return new SpringBus(); } @Bean public UserService userService() { return new UserServiceImpl(); } @Bean public Endpoint endpoint() { EndpointImpl endpoint=new EndpointImpl(springBus(), userService());//绑定要发布的服务 endpoint.publish("/user"); //显示要发布的名称 return endpoint; } }
五:服务启动后通过配置的地址访问wsdl文件 http://127.0.0.1:8080/kexin/webservice/user?wsdl
六:java的调用方式 client编写

package com.kexin.webservice.Test; import com.kexin.webservice.entity.User; import com.kexin.webservice.service.UserService; import org.apache.cxf.endpoint.Client; import org.apache.cxf.jaxws.JaxWsProxyFactoryBean; import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory; import java.util.Map; /** * @ClassName:CxfClient * @Description:webservice客户端: * 该类提供两种不同的方式来调用webservice服务 * 1:代理工厂方式 * 2:动态调用webservice */ public class CxfClient { public static void main(String[] args) { // CxfClient.main1(); CxfClient.main2(); } /** * 1.代理类工厂的方式,需要拿到对方的接口地址 */ public static void main1() { try { // 接口地址 String address = "http://127.0.0.1:8080/kexin/webservice/user?wsdl"; // 代理工厂 JaxWsProxyFactoryBean jaxWsProxyFactoryBean = new JaxWsProxyFactoryBean(); // 设置代理地址 jaxWsProxyFactoryBean.setAddress(address); // 设置接口类型 jaxWsProxyFactoryBean.setServiceClass(UserService.class); // 创建一个代理接口实现 UserService us = (UserService) jaxWsProxyFactoryBean.create(); // 数据准备 // String userId = "maple"; // 调用代理接口的方法调用并返回结果 // String result = us.getUserName(userId); Map<String, User> userMaps= us.getAllUserData(); System.out.println("返回结果:" + userMaps); } catch (Exception e) { e.printStackTrace(); } } /** * 2:动态调用 */ public static void main2() { // 创建动态客户端 JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance(); Client client = dcf.createClient("http://127.0.0.1:8080/kexin/webservice/user?wsdl"); // 需要密码的情况需要加上用户名和密码 // client.getOutInterceptors().add(new ClientLoginInterceptor(USER_NAME, PASS_WORD)); Object[] objects = new Object[0]; try { // invoke("方法名",参数1,参数2,参数3....); objects = client.invoke("getUserName", "maple"); System.out.println("返回数据:" + objects[0]); } catch (java.lang.Exception e) { e.printStackTrace(); } } }
分类:
springboot
标签:
websercice
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具