【WSDL】04 SpringBoot整合CXF
ApacheCXF框架对WSDL的信息几乎完全屏蔽了
能够更快速更方便的构建WebService
这里参考的视频地址是:
https://www.bilibili.com/video/BV1Kz4y1f78f
首先创建一个POM工程:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | <?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> <modules> <module>JAX-WS</module> <module>JAX-RS</module> </modules> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version> 1.5 . 4 .RELEASE</version> </parent> <groupId>cn.cloud9</groupId> <artifactId>Springboot-CXF</artifactId> <version> 1.0 -SNAPSHOT</version> <packaging>pom</packaging> </project> |
一、JAX-WS案例
创建子工程JAX-WS
依赖坐标:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | <?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" > <parent> <artifactId>Springboot-CXF</artifactId> <groupId>cn.cloud9</groupId> <version> 1.0 -SNAPSHOT</version> </parent> <modelVersion> 4.0 . 0 </modelVersion> <artifactId>JAX-WS</artifactId> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-spring-boot-starter-jaxws</artifactId> <version> 3.1 . 11 </version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version> 1.18 . 24 </version> <scope>provided</scope> </dependency> </dependencies> <properties> <maven.compiler.source> 8 </maven.compiler.source> <maven.compiler.target> 8 </maven.compiler.target> </properties> </project> |
创建普通的业务层资源:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | package cn.cloud9.po; import lombok.Data; /** * @author OnCloud9 * @description * @project Springboot-CXF * @date 2022年04月23日 16:50 */ @Data public class User { private Integer code; private String name; private String password; } |
接口,标记为Web服务接口
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | package cn.cloud9.intf; import cn.cloud9.po.User; import javax.jws.WebService; import java.util.List; @WebService public interface UserService { boolean saveUser(User user); boolean deleteUser(User user); boolean updateUser(User user); List<User> listUsers(); User findUserById(Integer code); } |
实现类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | package cn.cloud9.intf; import cn.cloud9.po.User; import org.springframework.stereotype.Service; import java.util.ArrayList; import java.util.List; /** * @author OnCloud9 * @description * @project Springboot-CXF * @date 2022年04月23日 16:53 */ @Service ( "userService" ) public class UserServiceImpl implements UserService{ static List<User> userList; static { userList = new ArrayList<>(); for ( int i = 1 ; i < 11 ; i++) { User user = new User(); user.setCode( 1000 + i); user.setName( "user - " + user.getCode()); user.setPassword( "123456" ); userList.add(user); } } public UserServiceImpl() { } @Override public boolean saveUser(User user) { return null != user; } @Override public boolean deleteUser(User user) { return null != user; } @Override public boolean updateUser(User user) { return null != user; } @Override public List<User> listUsers() { return userList; } @Override public User findUserById(Integer code) { return userList.stream().filter(user -> user.getCode().equals(code)).findFirst().get(); } } |
JaxWsConfig配置类
这里有一个服务总线Bean,类似Controller的概念,
但是所有服务都需要进过这个总Controller发布服务
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | package cn.cloud9.config; import cn.cloud9.intf.UserService; import org.apache.cxf.Bus; import org.apache.cxf.jaxws.EndpointImpl; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import javax.xml.ws.Endpoint; /** * @author OnCloud9 * @description * @project Springboot-CXF * @date 2022年04月23日 16:58 */ @Configuration public class JaxWsConfig { /** * 业务总线 */ @Autowired private Bus bus; @Autowired private UserService userService; @Bean public Endpoint createEndpoint() { final EndpointImpl endpoint = new EndpointImpl(bus, userService); endpoint.publish( "/userService" ); return endpoint; } } |
启动类:
这个服务没有任何的配置文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | package cn.cloud9; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * @author OnCloud9 * @description * @project Springboot-CXF * @date 2022年04月23日 16:50 */ @SpringBootApplication public class JaxWsApplication { public static void main(String[] args) { SpringApplication.run(JaxWsApplication. class , args); } } |
启动成功后,可以访问此地址查看WSDL的资源描述
http://localhost:8080/services/userService?wsdl
JaxWs客户端调用:
客户端需要对应的PO和接口一致,这里CXF会对WS进行封装
屏蔽之前许多内容,只需要提供WS的借口地址即可
另外注意一点,Bus会默认追加一个路径前缀,/services
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | import cn.cloud9.intf.UserService; import cn.cloud9.po.User; import org.apache.cxf.jaxws.JaxWsProxyFactoryBean; /** * @author OnCloud9 * @description * @project Springboot-CXF * @date 2022年04月23日 17:04 */ public class JaxWsTest { public static void main(String[] args) { final JaxWsProxyFactoryBean factoryBean = new JaxWsProxyFactoryBean(); factoryBean.setAddress( "http://localhost:8080/services/userService" ); factoryBean.setServiceClass(UserService. class ); final Object o = factoryBean.create(); UserService userService = (UserService) o; System.out.println(userService.saveUser( new User())); System.out.println(userService.listUsers()); final User userById = userService.findUserById( 1005 ); System.out.println(userById); } } |
得到WS接口代理的实例,就可以调用服务端了
二、JAX-RS案例
新建子工程JAX-RS:
由于是通过Rest风格调用,服务的调用完全可以按照Http的方式使用
这里多出的组件是为了响应JSON化,否则默认响应SOAP数据
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | <?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" > <parent> <artifactId>Springboot-CXF</artifactId> <groupId>cn.cloud9</groupId> <version> 1.0 -SNAPSHOT</version> </parent> <modelVersion> 4.0 . 0 </modelVersion> <artifactId>JAX-RS</artifactId> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-spring-boot-starter-jaxrs</artifactId> <version> 3.1 . 11 </version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-rs-extension-providers</artifactId> <version> 3.1 . 11 </version> </dependency> <dependency> <groupId>org.codehaus.jettison</groupId> <artifactId>jettison</artifactId> <version> 1.3 . 7 </version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version> 1.18 . 24 </version> <scope>provided</scope> </dependency> </dependencies> <properties> <maven.compiler.source> 8 </maven.compiler.source> <maven.compiler.target> 8 </maven.compiler.target> </properties> </project> |
PO,和实现类不需要变更,CXF对REST的支持是提供了这些HTTP的注解
定义在接口上面:
简单数据类型是直接返回的,像PO这样的数据类型,需要通过
@Produces({MediaType.APPLICATION_JSON}) 注解处理返回数据
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | package cn.cloud9.intf; import cn.cloud9.po.User; import javax.jws.WebService; import javax.ws.rs.*; import javax.ws.rs.core.MediaType; import java.util.List; @WebService public interface UserService { @POST @Path ( "/save" ) boolean saveUser(User user); @DELETE @Path ( "delete/{id}" ) boolean deleteUser( @PathParam ( "id" ) Integer code); @PUT () @Path ( "/update" ) boolean updateUser(User user); @GET @Path ( "/list" ) @Produces ({MediaType.APPLICATION_JSON}) List<User> listUsers(); @GET @Path ( "/find/{id}" ) @Produces (MediaType.APPLICATION_JSON) User findUserById( @PathParam ( "id" ) Integer code); } |
补充一下,PO需要打上@XmlRootElement注解
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | package cn.cloud9.po; import lombok.Data; import javax.xml.bind.annotation.XmlRootElement; /** * @author OnCloud9 * @description * @project Springboot-CXF * @date 2022年04月23日 16:50 */ @Data @XmlRootElement public class User { private Integer code; private String name; private String password; } |
JaxRsConfig并没有太大差别
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | package cn.cloud9.config; import cn.cloud9.intf.UserService; import org.apache.cxf.Bus; import org.apache.cxf.endpoint.Server; import org.apache.cxf.jaxrs.JAXRSServerFactoryBean; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * @author OnCloud9 * @description * @project Springboot-CXF * @date 2022年04月23日 17:30 */ @Configuration public class JaxRsConfig { @Autowired Bus bus; @Autowired private UserService userService; @Bean public Server createServer() { final JAXRSServerFactoryBean factoryBean = new JAXRSServerFactoryBean(); factoryBean.setAddress( "/userService" ); factoryBean.setBus(bus); factoryBean.setServiceBean(userService); return factoryBean.create(); } } |
JaxRS客户端调用:
因为是Rest接口,所以可以直接使用Postman来发送请求调用了
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
2020-04-23 【Java】JDBC Part2 工具类封装实现
2020-04-23 【Java】JDBC Part1 数据库连接的演变
2020-04-23 【IDEA】创建Maven工程
2020-04-23 【Java】JDK8特性 LambdaExpression lambda表达式
2020-04-23 【Java】Reflection 反射机制 03调用
2020-04-23 【Java】Reflection 反射机制 02获取类的一切
2020-04-23 【Java】Reflection 反射机制 01概述