spring-boot整合Cxf的webservice案例


1.運行環境

開發工具:intellij idea

JDK版本:1.8

項目管理工具:Maven 4.0.0

2.Maven Plugin管理

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0"
 3          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 5     <modelVersion>4.0.0</modelVersion>
 6 
 7     <groupId>spring-boot-ws</groupId>
 8     <artifactId>spring-boot-ws</artifactId>
 9     <version>1.0-SNAPSHOT</version>
10 
11     <parent>
12         <groupId>org.springframework.boot</groupId>
13         <artifactId>spring-boot-starter-parent</artifactId>
14         <version>1.5.6.RELEASE</version>
15     </parent>
16 
17     <dependencies>
18         <dependency>
19             <groupId>org.springframework.boot</groupId>
20             <artifactId>spring-boot-starter-web</artifactId>
21         </dependency>
22         <!-- CXF webservice -->
23         <dependency>
24             <groupId>org.apache.cxf</groupId>
25             <artifactId>cxf-spring-boot-starter-jaxws</artifactId>
26             <version>3.1.11</version>
27         </dependency>
28     </dependencies>
29 
30 
31 </project>
View Code

3.WebService方法創建

 1 package com.goku.demo.controller;
 2 
 3 import org.springframework.stereotype.Component;
 4 
 5 import javax.jws.WebMethod;
 6 import javax.jws.WebParam;
 7 import javax.jws.WebService;
 8 
 9 /**
10  * Created by nbfujx on 2017/11/27.
11  */
12 @Component
13 @WebService(serviceName = "ExampleService"
14         ,targetNamespace="http://controller.demo.goku.com/")
15 public class ExampleController {
16 
17     @WebMethod
18     public String echo(@WebParam(name = "para") String para) {
19     return "hello"+para;
20     }
21 }
View Code

4.CxfConfig配置編寫

 1 package com.goku.demo.config;
 2 
 3 import com.goku.demo.controller.ExampleController;
 4 import org.apache.cxf.Bus;
 5 import org.apache.cxf.jaxws.EndpointImpl;
 6 import org.springframework.beans.factory.annotation.Autowired;
 7 import org.springframework.context.annotation.Bean;
 8 import org.springframework.context.annotation.Configuration;
 9 
10 import javax.xml.ws.Endpoint;
11 
12 /**
13  * Created by nbfujx on 2017/11/27.
14  */
15 @Configuration
16 public class CxfConfig {
17 
18     @Autowired
19     private Bus bus;
20 
21     @Autowired
22     private ExampleController examplecontroller;
23 
24     @Bean
25     public Endpoint endpoint() {
26         EndpointImpl endpoint = new EndpointImpl(bus,examplecontroller);
27         endpoint.publish("/ExampleService");//接口發布在 /NetbarServices 目錄下
28         return endpoint;
29     }
30 
31 }
View Code

5.WsApplication啟動類編寫

 1 package com.goku.demo;
 2 
 3 import org.springframework.boot.SpringApplication;
 4 import org.springframework.boot.autoconfigure.SpringBootApplication;
 5 import org.springframework.boot.web.servlet.ServletComponentScan;
 6 
 7 /**
 8  * Created by nbfujx on 2017/10/19.
 9  */
10 // Spring Boot 應用的標識
11 @SpringBootApplication
12 @ServletComponentScan
13 public class WsApplication {
14 
15     public static void main(String[] args) {
16         // 程序啟動入口
17         // 啟動嵌入式的 Tomcat 並初始化 Spring 環境及其各 Spring 組件
18         SpringApplication.run(WsApplication.class,args);
19     }
20 }
View Code

6.Cxf客戶端編寫

 1 package test.com.goku.demo.client;
 2 
 3 
 4 import org.apache.cxf.endpoint.Client;
 5 import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
 6 import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
 7 
 8 import com.goku.demo.controller.ExampleController;
 9 
10 /**
11  * Created by nbfujx on 2017/11/27.
12  */
13 public class CxfClient {
14 
15     public static void main(String[] args) {
16         echo();
17     }
18 
19     public static void echo() {
20         // 創建動態客戶端
21         JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
22         Client client = dcf.createClient("http://localhost:8080/services/ExampleService?wsdl");
23         Object[] objects = new Object[0];
24         try {
25             objects = client.invoke("echo", "str");
26             System.out.println("echo:" + objects[0]);
27         } catch (java.lang.Exception e) {
28             e.printStackTrace();
29         }
30     }
31 }
View Code

7.查看測試結果

先啟動cxf服務端,再進行客戶端調閱

8.GITHUB地址

https://github.com/nbfujx/springBoot-learn-demo/tree/master/spring-boot-ws

待續


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM