码迷,mamicode.com
首页 > 编程语言 > 详细

Spring Boot用Cxf的jax-ws开发WebService

时间:2018-05-24 13:11:49      阅读:321      评论:0      收藏:0      [点我收藏+]

标签:system   source   tin   aging   配置   group   服务   point   get   

首先上项目的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

标签:system   source   tin   aging   配置   group   服务   point   get   

原文地址:https://www.cnblogs.com/foohack/p/9082315.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!