Spring Boot用Cxf的jax-ws开发WebService

原文链接: http://www.cnblogs.com/foohack/p/9082315.html

首先上项目的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.0modelVersion>
  5 
  6     <groupId>com.mathxh-webservicegroupId>
  7     <artifactId>webserviceartifactId>
  8     <version>0.0.1-SNAPSHOTversion>
  9     <packaging>jarpackaging>
 10 
 11     <name>webservicename>
 12     <description>Learning WebServicedescription>
 13 
 14     <parent>
 15         <groupId>org.springframework.bootgroupId>
 16         <artifactId>spring-boot-starter-parentartifactId>
 17         <version>1.5.14.BUILD-SNAPSHOTversion>
 18         <relativePath/> 
 19     parent>
 20 
 21     <properties>
 22         <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
 23         <project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
 24         <java.version>1.8java.version>
 25     properties>
 26 
 27     <dependencies>
 28         <dependency>
 29             <groupId>org.springframework.bootgroupId>
 30             <artifactId>spring-boot-starterartifactId>
 31         dependency>
 32 
 33         <dependency>
 34             <groupId>org.springframework.bootgroupId>
 35             <artifactId>spring-boot-starter-testartifactId>
 36             <scope>testscope>
 37         dependency>
 38 
 39         <dependency>
 40             <groupId>org.springframework.bootgroupId>
 41             <artifactId>spring-boot-starter-webartifactId>
 42         dependency>
 43 
 44         
 45         <dependency>
 46             <groupId>org.apache.cxfgroupId>
 47             <artifactId>cxf-spring-boot-starter-jaxwsartifactId>
 48             <version>3.1.11version>
 49         dependency>
 50         
 51     dependencies>
 52 
 53     <build>
 54         <plugins>
 55             <plugin>
 56                 <groupId>org.springframework.bootgroupId>
 57                 <artifactId>spring-boot-maven-pluginartifactId>
 58             plugin>
 59         plugins>
 60     build>
 61 
 62     <repositories>
 63         <repository>
 64             <id>spring-snapshotsid>
 65             <name>Spring Snapshotsname>
 66             <url>https://repo.spring.io/snapshoturl>
 67             <snapshots>
 68                 <enabled>trueenabled>
 69             snapshots>
 70         repository>
 71         <repository>
 72             <id>spring-milestonesid>
 73             <name>Spring Milestonesname>
 74             <url>https://repo.spring.io/milestoneurl>
 75             <snapshots>
 76                 <enabled>falseenabled>
 77             snapshots>
 78         repository>
 79     repositories>
 80 
 81     <pluginRepositories>
 82         <pluginRepository>
 83             <id>spring-snapshotsid>
 84             <name>Spring Snapshotsname>
 85             <url>https://repo.spring.io/snapshoturl>
 86             <snapshots>
 87                 <enabled>trueenabled>
 88             snapshots>
 89         pluginRepository>
 90         <pluginRepository>
 91             <id>spring-milestonesid>
 92             <name>Spring Milestonesname>
 93             <url>https://repo.spring.io/milestoneurl>
 94             <snapshots>
 95                 <enabled>falseenabled>
 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 javax.activation.DataSource;
  9 import javax.activation.FileDataSource;
 10 import java.io.*;
 11 
 12 public class CxfClient {
 13     public static void main(String[] args) {
 14         //cl1();
 15        // downloadTest();
 16         uploadTest();
 17     }
 18 
 19     /**
 20      * 方式1.代理类工厂的方式,需要拿到对方的接口
 21      */
 22     public static void cl1() {
 23         try {
 24             // 接口地址
 25             String address = "http://localhost:8080/services/CommonService?wsdl";
 26             // 代理工厂
 27             JaxWsProxyFactoryBean jaxWsProxyFactoryBean = new JaxWsProxyFactoryBean();
 28             // 设置代理地址
 29             jaxWsProxyFactoryBean.setAddress(address);
 30             // 设置接口类型
 31             jaxWsProxyFactoryBean.setServiceClass(CommonService.class);
 32             // 创建一个代理接口实现
 33             CommonService cs = (CommonService) jaxWsProxyFactoryBean.create();
 34             // 数据准备
 35             String userName = "MathxH Chen";
 36             // 调用代理接口的方法调用并返回结果
 37             String result = cs.sayHello(userName);
 38             System.out.println("返回结果:" + result);
 39         } catch (Exception e) {
 40             e.printStackTrace();
 41         }
 42     }
 43 
 44     public static void uploadTest(){
 45         try{
 46             // 接口地址
 47             String address = "http://localhost:8080/services/CommonService?wsdl";
 48             // 代理工厂
 49             JaxWsProxyFactoryBean jaxWsProxyFactoryBean = new JaxWsProxyFactoryBean();
 50             // 设置代理地址
 51             jaxWsProxyFactoryBean.setAddress(address);
 52             // 设置接口类型
 53             jaxWsProxyFactoryBean.setServiceClass(CommonService.class);
 54             // 创建一个代理接口实现
 55             CommonService cs = (CommonService) jaxWsProxyFactoryBean.create();
 56 
 57             BinaryFile file = new BinaryFile();
 58             file.setTitle("uploaded.png");
 59             DataSource source = new FileDataSource(new File("d:" + File.separator + "downloaded.png"));
 60             file.setBinaryData(new DataHandler(source));
 61             if(cs.uploadFile(file)) {
 62                 System.out.println("upload success");
 63             }else{
 64                 System.out.println("upload failed");
 65             }
 66 
 67         }catch (Exception e){
 68             e.printStackTrace();
 69         }
 70     }
 71 
 72     public static void downloadTest(){
 73         try{
 74             // 接口地址
 75             String address = "http://localhost:8080/services/CommonService?wsdl";
 76             // 代理工厂
 77             JaxWsProxyFactoryBean jaxWsProxyFactoryBean = new JaxWsProxyFactoryBean();
 78             // 设置代理地址
 79             jaxWsProxyFactoryBean.setAddress(address);
 80             // 设置接口类型
 81             jaxWsProxyFactoryBean.setServiceClass(CommonService.class);
 82             // 创建一个代理接口实现
 83             CommonService cs = (CommonService) jaxWsProxyFactoryBean.create();
 84 
 85            BinaryFile file = cs.downloadFile("test.png");
 86            String title = file.getTitle();
 87            DataHandler binaryData = file.getBinaryData();
 88 
 89             try (
 90                     InputStream is = binaryData.getInputStream();
 91                     OutputStream os = new FileOutputStream(new File("d:" + File.separator + "downloaded.png"));
 92                     BufferedOutputStream bos = new BufferedOutputStream(os))
 93             {
 94 
 95                 byte[] buffer = new byte[100000];
 96                 int bytesRead = 0;
 97                 while ((bytesRead = is.read(buffer)) != -1) {
 98                     bos.write(buffer, 0, bytesRead);
 99                 }
100 
101                 bos.flush();
102             } catch (IOException e) {
103                 e.printStackTrace();
104 
105             }
106 
107         } catch (Exception e) {
108             e.printStackTrace();
109         }
110     }
111 }
View Code

 

 

references:

http://cxf.apache.org/docs/developing-a-service.html

http://cxf.apache.org/docs/developing-a-consumer.html

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

http://cxf.apache.org/docs/jax-ws-configuration.html

 

转载于:https://www.cnblogs.com/foohack/p/9082315.html

你可能感兴趣的:(Spring Boot用Cxf的jax-ws开发WebService)