Apache CXF 是一個開源的 Services 框架,CXF 幫助您利用 Frontend 編程 API 來構建和開發 Services ,像 JAX-WS 。這些 Services 能夠支持多種協議,好比:SOAP、XML/HTTP、RESTful HTTP 或者 CORBA ,而且能夠在多種傳輸協議上運行,好比:HTTP、JMS 或者 JBI,CXF 大大簡化了 Services 的建立,同時它繼承了 XFire 傳統,同樣能夠自然地和 Spring 進行無縫集成。
java
本次例子爲Client-Server(客戶端-服務端)。仍是以我最喜歡的Blog爲例。git
本次涉及兩個項目,一個是blog-cxf-client,另一個是blog-cxf-server。github
<dependencies> <!-- SpringBoot Web --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- CXF webservice --> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-spring-boot-starter-jaxws</artifactId> <version>3.2.4</version> </dependency> <!-- Lombok--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <artifactId>blog-cxf-server</artifactId> <groupId>com.blog.cxf</groupId> <version>1.0</version> </dependency> </dependencies>
package com.blog.cxf.server; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; /** * @description: * @author: youcong * @time: 2020/10/24 22:30 */ @SpringBootApplication(exclude = {DataSourceAutoConfiguration.class}) public class BlogCxfServerApplication { public static void main(String[] args) { SpringApplication.run(BlogCxfServerApplication.class, args); System.out.println("====啓動Blog Cxf Server===="); } }
# Tomcat server: tomcat: uri-encoding: UTF-8 #最小線程數 min-spare-threads: 500 #最大線程數 max-threads: 2500 #最大鏈接數 max-connections: 5000 #最大等待隊列長度 accept-count: 1000 #請求頭最大長度kb max-http-header-size: 1048576 #啓動APR(非阻塞IO) protocol: org.apache.coyote.http11.Http11AprProtocol port: 9090 # Spring spring: application: # 應用名稱 name: blog-cxf-server cxf: path: /cxf
UserService.javaweb
package com.blog.cxf.server.service; import com.blog.cxf.server.dto.UserReqDto; import javax.jws.WebParam; import javax.jws.WebService; /** * @description: * @author: youcong * @time: 2020/10/24 22:32 */ @WebService(targetNamespace = "http://service.server.cxf.blog.com/") public interface UserService { /** * 添加用戶 * @param email * @param username * @param password * @return */ int addUser(@WebParam(name = "email") String email, @WebParam(name = "username") String username, @WebParam(name = "password") String password); /** * 更新用戶信息 * @param userReqDto * @return */ int updateUser(@WebParam(name="user")UserReqDto userReqDto); }
UserServiceImpl.javaspring
package com.blog.cxf.server.service.impl; import com.blog.cxf.server.dto.UserReqDto; import com.blog.cxf.server.service.UserService; import org.springframework.stereotype.Component; import javax.jws.WebService; /** * @description: * @author: youcong * @time: 2020/10/24 22:35 */ @WebService(serviceName = "userService",//對外發布的服務名 targetNamespace = "http://service.server.cxf.blog.com/",//指定你想要的名稱空間,一般使用使用包名反轉 endpointInterface = "com.blog.cxf.server.service.UserService") @Component public class UserServiceImpl implements UserService { public int addUser(String email, String username, String password) { System.out.println("註冊用戶:"+email); return 1; } public int updateUser(UserReqDto userReqDto) { return 1; } }
數據傳輸類(UserReqDto.java):apache
package com.blog.cxf.server.dto; import lombok.Data; /** * @description: * @author: youcong * @time: 2020/10/24 22:49 */ @Data public class UserReqDto { private Long ID; private String email; private String username; private String password; }
package com.blog.cxf.server.config; import com.blog.cxf.server.service.UserService; import com.blog.cxf.server.service.impl.UserServiceImpl; import org.apache.cxf.Bus; import org.apache.cxf.bus.spring.SpringBus; import org.apache.cxf.jaxws.EndpointImpl; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * @description: * @author: youcong * @time: 2020/10/24 22:37 */ @Configuration public class CxfConfig { @Bean(name = Bus.DEFAULT_BUS_ID) public SpringBus springBus() { return new SpringBus(); } @Bean public UserService userService() { return new UserServiceImpl(); } /** * 發佈服務並指定訪問URL * @return */ @Bean public EndpointImpl userEnpoint() { EndpointImpl endpoint = new EndpointImpl(springBus(), userService()); endpoint.publish("/user"); return endpoint; } }
訪問路徑:
http://localhost:9090/cxf/user?wsdl編程
<?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>blog-cxf</artifactId> <groupId>com.blog.cxf</groupId> <version>1.0</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>blog-cxf-client</artifactId> <dependencies> <!-- SpringBoot Web --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- CXF webservice --> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-spring-boot-starter-jaxws</artifactId> <version>3.2.4</version> </dependency> <!-- Lombok--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <artifactId>blog-cxf-server</artifactId> <groupId>com.blog.cxf</groupId> <version>1.0</version> </dependency> </dependencies> </project>
package com.blog.cxf.client; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; /** * @description: * @author: youcong * @time: 2020/10/24 23:35 */ @SpringBootApplication(exclude = {DataSourceAutoConfiguration.class}) public class BlogCxfClientApplication { public static void main(String[] args) { SpringApplication.run(BlogCxfClientApplication.class, args); System.out.println("====啓動Blog Cxf Client===="); } }
# Tomcat server: tomcat: uri-encoding: UTF-8 #最小線程數 min-spare-threads: 500 #最大線程數 max-threads: 2500 #最大鏈接數 max-connections: 5000 #最大等待隊列長度 accept-count: 1000 #請求頭最大長度kb max-http-header-size: 1048576 #啓動APR(非阻塞IO) protocol: org.apache.coyote.http11.Http11AprProtocol port: 9091 # Spring spring: application: # 應用名稱 name: blog-cxf-client
package com.blog.cxf.client.controller; import com.blog.cxf.server.dto.UserReqDto; import com.blog.cxf.server.service.UserService; import org.apache.cxf.jaxws.JaxWsProxyFactoryBean; import org.springframework.web.bind.annotation.*; /** * @description: * @author: youcong * @time: 2020/10/24 23:37 */ @RestController @RequestMapping("/user") public class UserApiController { @PostMapping("/add") public int add(@RequestParam String email, @RequestParam String username, @RequestParam String password) { try { // 接口地址 String address = "http://127.0.0.1:9090/cxf/user?wsdl"; // 代理工廠 JaxWsProxyFactoryBean jaxWsProxyFactoryBean = new JaxWsProxyFactoryBean(); // 設置代理地址 jaxWsProxyFactoryBean.setAddress(address); // 設置接口類型 jaxWsProxyFactoryBean.setServiceClass(UserService.class); // 建立一個代理接口實現 UserService userService = (UserService) jaxWsProxyFactoryBean.create(); return userService.addUser(email, username, password); } catch (Exception e) { e.printStackTrace(); return -1; } } }
注意:
實際中這段代碼應該放在blog-cxf-server裏面的Controller,而後客戶端經過http-client或者其它http工具包進行請求。app
還有若是是服務是都在一塊兒,可按照maven依賴導入的方式來實現兩個不一樣項目進行調用。
代碼例子已上傳到個人GitHub上。