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

然后开发WebService服务接口并实现接口:

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

之后是实现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. }

然后是配置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. }

最后实现客户端调用(基本调用返回字符串,基于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. }

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

Spring Boot用Cxf的jax-ws开发WebService的更多相关文章

  1. Spring boot 整合CXF webservice 遇到的问题及解决

    将WebService的WSDL生成的代码的命令: wsimport -p com -s . com http://localhost:8080/service/user?wsdl Spring bo ...

  2. Cxf + Spring3.0 入门开发WebService

    转自原文地址:http://sunny.blog.51cto.com/182601/625540/ 由于公司业务需求, 需要使用WebService技术对外提供服务,以前没有做过类似的项目,在网上搜寻 ...

  3. Spring Boot 使用 CXF 调用 WebService 服务

    上一张我们讲到 Spring Boot 开发 WebService 服务,本章研究基于 CXF 调用 WebService.另外本来想写一篇 xfire 作为 client 端来调用 webservi ...

  4. Spring Boot 系列(五)web开发-Thymeleaf、FreeMarker模板引擎

    前面几篇介绍了返回json数据提供良好的RESTful api,下面我们介绍如何把处理完的数据渲染到页面上. Spring Boot 使用模板引擎 Spring Boot 推荐使用Thymeleaf. ...

  5. Spring Boot 系列(六)web开发-Spring Boot 热部署

    Spring Boot 热部署 实际开发中,修改某个页面数据或逻辑功能都需要重启应用.这无形中降低了开发效率,所以使用热部署是十分必要的. 什么是热部署? 应用启动后会把编译好的Class文件加载的虚 ...

  6. Spring Boot 整合JDBC 实现后端项目开发

    一.前言 二.新建Spring Boot 项目 三.Spring Boot 整合JDBC 与MySQL 交互 3.1 新建数据表skr_user 3.2 Jdbcproject 项目结构如下 3.3 ...

  7. Spring Boot微服务电商项目开发实战 --- 基础配置及搭建

    根据SpringBoot实现分布式微服务项目近两年的开发经验,今天决定开始做SpringBoot实现分布式微服务项目的系列文章,帮助其他正在使用或计划使用SringBoot开发的小伙伴们.本次系列文章 ...

  8. 【Spring Boot学习之二】WEB开发

    环境 Java1.8 Spring Boot 1.3.2 一.静态资源访问 动静分离:动态服务和前台页面图片分开,静态资源可以使用CDN加速;Spring Boot默认提供静态资源目录位置需置于cla ...

  9. Spring Boot教程(二十)开发Web应用(1)

    静态资源访问 在我们开发Web应用的时候,需要引用大量的js.css.图片等静态资源. 默认配置 Spring Boot默认提供静态资源目录位置需置于classpath下,目录名需符合如下规则: /s ...

随机推荐

  1. 应用:计算字符串的长度(一个双字节字符长度计2,ASCII字符计1) String.prototype.len=function(){return this.replace(/[^\x00-\xff]/g,"aa").length;}

    应用:计算字符串的长度(一个双字节字符长度计2,ASCII字符计1) String.prototype.len=function(){return this.replace(/[^\x00-\xff] ...

  2. 5、Qt Project之键盘数据监控

    键盘数据监控: 同样的,键盘的检测和鼠标的情形很类似,都是以QWidget为基类的工程 Step1:在UI设计中添加该模块需要使用的相关组件,如下所示: <width>141</wi ...

  3. Codefoces Gym 101652 【最大连续和】

    <题目链接> 题目大意: 给你一段只由 'B'和'R'组成的字符串,问你在连续的区间内,"B"和"R"的差值最大是多少,输出该区间:如果对于差值相等 ...

  4. 利用Fiddler拦截接口请求并篡改数据

    近期在测试一个下单的项目,出于安全角度考虑,测试了一个场景,那就是利用工具对接口进行拦截并篡改数据.将接口一拦截并篡改数据后,发现收货满满.开发默默接受了我的建议,并对代码进行了修改. 对于fiddl ...

  5. window下的计划任务

    0x00前言: 这几天看了看信息安全就业的面试题,其中有一条是计划任务如何设置,好几个月前稍微接触了,但是很久没用差不多都忘了>_<,这里就稍微学习下windows的计划任务 写着写着就偏 ...

  6. OSPF补全计划-1

    OSPF全称是啥我就不絮叨了,什么迪杰斯特拉,什么开放最短路径优先算法都是人尽皆知的事儿,尤其是一提算法还会被学数据结构的童鞋鄙视,干脆就不提了,直接开整怎么用吧.(不过好像真有人不知道OSPF里的F ...

  7. Manacher学习笔记

    目录 code(伪) Manacher算法 可在 \(O(n)\)的时间内求出一个字符串以每个位置为中心的最长回文子串. 原理:根据之前预处理出的回文串长度求得新的回文串长度 我们可以通过在字符中加上 ...

  8. python基础一 ------如何根据字典值对字典进行"排序"

    需求:{姓名:成绩} 的字典,按成绩进行排序 方法一:转化为元组,(91,"张三")的形式 ,用sorted()函数进行排序 方法二 :设置sorted() 中key的参数的值 # ...

  9. HQL实用技术

    HQL是Hibernate Query Language的缩写,提供更加丰富灵活.更为强大的查询能力:HQL更接近SQL语句查询语法. HQL基础查询  1.获取部分列 多列 /** * 获取部分列 ...

  10. [HNOI2010]平面图判定

    Description: 若能将无向图 \(G=(V, E)\) 画在平面上使得任意两条无重合顶点的边不相交,则称 \(G\) 是平面图.判定一个图是否为平面图的问题是图论中的一个重要问题.现在假设你 ...