1. 程式人生 > >Spring Boot用Cxf的jax-ws開發WebService

Spring Boot用Cxf的jax-ws開發WebService

contex 字符串 scope nap 1.0 地址 method ons lean

首先上項目的pom.xml:

技術分享圖片
  1 <?xml version="1.0" encoding="UTF-8"?>
  2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4     <modelVersion>4.0.0</modelVersion
> 5 6 <groupId>com.mathxh-webservice</groupId> 7 <artifactId>webservice</artifactId> 8 <version>0.0.1-SNAPSHOT</version> 9 <packaging>jar</packaging> 10 11 <name>webservice</name> 12 <description>Learning WebService</
description> 13 14 <parent> 15 <groupId>org.springframework.boot</groupId> 16 <artifactId>spring-boot-starter-parent</artifactId> 17 <version>1.5.14.BUILD-SNAPSHOT</version> 18 <relativePath/> <!-- lookup parent from repository
--> 19 </parent> 20 21 <properties> 22 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 23 <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> 24 <java.version>1.8</java.version> 25 </properties> 26 27 <dependencies> 28 <dependency> 29 <groupId>org.springframework.boot</groupId> 30 <artifactId>spring-boot-starter</artifactId> 31 </dependency> 32 33 <dependency> 34 <groupId>org.springframework.boot</groupId> 35 <artifactId>spring-boot-starter-test</artifactId> 36 <scope>test</scope> 37 </dependency> 38 39 <dependency> 40 <groupId>org.springframework.boot</groupId> 41 <artifactId>spring-boot-starter-web</artifactId> 42 </dependency> 43 44 <!-- CXF webservice --> 45 <dependency> 46 <groupId>org.apache.cxf</groupId> 47 <artifactId>cxf-spring-boot-starter-jaxws</artifactId> 48 <version>3.1.11</version> 49 </dependency> 50 <!-- CXF webservice --> 51 </dependencies> 52 53 <build> 54 <plugins> 55 <plugin> 56 <groupId>org.springframework.boot</groupId> 57 <artifactId>spring-boot-maven-plugin</artifactId> 58 </plugin> 59 </plugins> 60 </build> 61 62 <repositories> 63 <repository> 64 <id>spring-snapshots</id> 65 <name>Spring Snapshots</name> 66 <url>https://repo.spring.io/snapshot</url> 67 <snapshots> 68 <enabled>true</enabled> 69 </snapshots> 70 </repository> 71 <repository> 72 <id>spring-milestones</id> 73 <name>Spring Milestones</name> 74 <url>https://repo.spring.io/milestone</url> 75 <snapshots> 76 <enabled>false</enabled> 77 </snapshots> 78 </repository> 79 </repositories> 80 81 <pluginRepositories> 82 <pluginRepository> 83 <id>spring-snapshots</id> 84 <name>Spring Snapshots</name> 85 <url>https://repo.spring.io/snapshot</url> 86 <snapshots> 87 <enabled>true</enabled> 88 </snapshots> 89 </pluginRepository> 90 <pluginRepository> 91 <id>spring-milestones</id> 92 <name>Spring Milestones</name> 93 <url>https://repo.spring.io/milestone</url> 94 <snapshots> 95 <enabled>false</enabled> 96 </snapshots> 97 </pluginRepository> 98 </pluginRepositories> 99 100 101 </project>
View Code

然後開發WebService服務接口並實現接口:

技術分享圖片
 1 package com.mathxhwebservice.webservice.service;
 2 
 3 
 4 /**
 5  * 接口
 6  *
 7  * @author MathxH Chen
 8  *
 9  */
10 
11 import com.mathxhwebservice.webservice.mtom.BinaryFile;
12 
13 import javax.jws.WebMethod;
14 import javax.jws.WebParam;
15 import javax.jws.WebResult;
16 import javax.jws.WebService;
17 import javax.xml.ws.soap.MTOM;
18 
19 @WebService(name = "CommonService", // 暴露服務名稱
20         targetNamespace = "http://service.webservice.mathxhwebservice.com/")// 命名空間,一般是接口的包名倒序
21 @MTOM(threshold = 1024)
22 public interface CommonService {
23 
24     @WebMethod
25     @WebResult(name = "String")
26     String sayHello(@WebParam(name = "userName") String name);
27 
28     @WebMethod
29     @WebResult(name ="BinaryFile")
30     BinaryFile downloadFile(@WebParam(name = "fileName") String fileName);
31 
32     @WebMethod
33     @WebResult(name = "boolean")
34     boolean uploadFile(@WebParam(name = "file") BinaryFile file);
35 }
View Code

之後是實現WebService接口:

技術分享圖片
 1 package com.mathxhwebservice.webservice.service;
 2 
 3 import com.mathxhwebservice.webservice.mtom.BinaryFile;
 4 import org.springframework.stereotype.Component;
 5 
 6 import javax.activation.DataHandler;
 7 import javax.activation.DataSource;
 8 import javax.activation.FileDataSource;
 9 import javax.jws.WebService;
10 import java.io.*;
11 
12 @WebService(serviceName = "CommonService", // 與接口中指定的name一致
13         targetNamespace = "http://service.webservice.mathxhwebservice.com/", // 與接口中的命名空間一致,一般是接口的包名倒
14         endpointInterface = "com.mathxhwebservice.webservice.service.CommonService"// 接口地址
15 )
16 @Component
17 public class CommonServiceImpl implements CommonService{
18 
19     @Override
20     public String sayHello(String name) {
21         return "Hello ," + name;
22     }
23 
24     @Override
25     public BinaryFile downloadFile(String fileName){
26         BinaryFile file = new BinaryFile();
27         file.setTitle(fileName);
28         DataSource source = new FileDataSource(new File("d:" + File.separator + fileName));
29         file.setBinaryData(new DataHandler(source));
30         return file;
31     }
32 
33     @Override
34     public  boolean uploadFile(BinaryFile file){
35         DataHandler dataHandler = file.getBinaryData();
36         String fileTitle = file.getTitle();
37 
38         try (
39                 InputStream is = dataHandler.getInputStream();
40                 OutputStream os = new FileOutputStream(new File("d:" + File.separator + fileTitle));
41                 BufferedOutputStream bos = new BufferedOutputStream(os))
42         {
43 
44             byte[] buffer = new byte[100000];
45             int bytesRead = 0;
46             while ((bytesRead = is.read(buffer)) != -1) {
47                 bos.write(buffer, 0, bytesRead);
48             }
49 
50             bos.flush();
51         } catch (IOException e) {
52             e.printStackTrace();
53             return false;
54         }
55         return true;
56     }
57 }
View Code

然後是配置WebService的發布類:

技術分享圖片
 1 package com.mathxhwebservice.webservice.config;
 2 
 3 import com.mathxhwebservice.webservice.service.CommonService;
 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 @Configuration
13 public class CxfConfig {
14     @Autowired
15     private Bus bus;
16 
17     @Autowired
18     CommonService commonService;
19 
20     /** JAX-WS **/
21     @Bean
22     public Endpoint endpoint() {
23         EndpointImpl endpoint = new EndpointImpl(bus, commonService);
24         endpoint.publish("/CommonService");
25 
26         return endpoint;
27     }
28 }
View Code

最後實現客戶端調用(基本調用返回字符串,基於MTOM的上傳下載文件):

技術分享圖片
  1 package com.mathxhwebservice.webservice.wsclient;
  2 
  3 import com.mathxhwebservice.webservice.mtom.BinaryFile;
  4 import com.mathxhwebservice.webservice.service.CommonService;
  5 import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
  6 
  7 import javax.activation.DataHandler;
  8 import java.io.*;
  9 
 10 public class CxfClient {
 11     public static void main(String[] args) {
 12         cl1();
 13         downloadTest();
 14         uploadTest();
 15     }
 16 
 17     /**
 18      * 方式1.代理類工廠的方式,需要拿到對方的接口
 19      */
 20     public static void cl1() {
 21         try {
 22             // 接口地址
 23             String address = "http://localhost:8080/services/CommonService?wsdl";
 24             // 代理工廠
 25             JaxWsProxyFactoryBean jaxWsProxyFactoryBean = new JaxWsProxyFactoryBean();
 26             // 設置代理地址
 27             jaxWsProxyFactoryBean.setAddress(address);
 28             // 設置接口類型
 29             jaxWsProxyFactoryBean.setServiceClass(CommonService.class);
 30             // 創建一個代理接口實現
 31             CommonService cs = (CommonService) jaxWsProxyFactoryBean.create();
 32             // 數據準備
 33             String userName = "MathxH Chen";
 34             // 調用代理接口的方法調用並返回結果
 35             String result = cs.sayHello(userName);
 36             System.out.println("返回結果:" + result);
 37         } catch (Exception e) {
 38             e.printStackTrace();
 39         }
 40     }
 41 
 42     public static void uploadTest(){
 43         try{
 44             // 接口地址
 45             String address = "http://localhost:8080/services/CommonService?wsdl";
 46             // 代理工廠
 47             JaxWsProxyFactoryBean jaxWsProxyFactoryBean = new JaxWsProxyFactoryBean();
 48             // 設置代理地址
 49             jaxWsProxyFactoryBean.setAddress(address);
 50             // 設置接口類型
 51             jaxWsProxyFactoryBean.setServiceClass(CommonService.class);
 52             // 創建一個代理接口實現
 53             CommonService cs = (CommonService) jaxWsProxyFactoryBean.create();
 54 
 55             BinaryFile file = new BinaryFile();
 56             if(cs.uploadFile(file)) {
 57                 System.out.println("upload success");
 58             }else{
 59                 System.out.println("upload failed");
 60             }
 61 
 62         }catch (Exception e){
 63             e.printStackTrace();
 64         }
 65     }
 66 
 67     public static void downloadTest(){
 68         try{
 69             // 接口地址
 70             String address = "http://localhost:8080/services/CommonService?wsdl";
 71             // 代理工廠
 72             JaxWsProxyFactoryBean jaxWsProxyFactoryBean = new JaxWsProxyFactoryBean();
 73             // 設置代理地址
 74             jaxWsProxyFactoryBean.setAddress(address);
 75             // 設置接口類型
 76             jaxWsProxyFactoryBean.setServiceClass(CommonService.class);
 77             // 創建一個代理接口實現
 78             CommonService cs = (CommonService) jaxWsProxyFactoryBean.create();
 79 
 80            BinaryFile file = cs.downloadFile("test.png");
 81            String title = file.getTitle();
 82            DataHandler binaryData = file.getBinaryData();
 83 
 84             try (
 85                     InputStream is = binaryData.getInputStream();
 86                     OutputStream os = new FileOutputStream(new File("d:" + File.separator + "downloaded.png"));
 87                     BufferedOutputStream bos = new BufferedOutputStream(os))
 88             {
 89 
 90                 byte[] buffer = new byte[100000];
 91                 int bytesRead = 0;
 92                 while ((bytesRead = is.read(buffer)) != -1) {
 93                     bos.write(buffer, 0, bytesRead);
 94                 }
 95 
 96                 bos.flush();
 97             } catch (IOException e) {
 98                 e.printStackTrace();
 99 
100             }
101 
102         } catch (Exception e) {
103             e.printStackTrace();
104         }
105     }
106 }
View Code

references:

http://cxf.apache.org/docs/mtom-attachments-with-jaxb.html

http://yufenfei.iteye.com/blog/1685910

https://blog.csdn.net/accountwcx/article/details/47165321

https://blog.csdn.net/a363722188/article/details/43983959

http://cxf.apache.org/docs/a-simple-jax-ws-service.html

Spring Boot用Cxf的jax-ws開發WebService