Spring Boot用Cxf的jax-ws开发WebService

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

然后开发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 }

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

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

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

时间: 2024-08-26 03:54:41

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

spring boot整合cxf发布和调用webservice

一.前言 说起web service最近几年restful大行其道,大有取代传统soap web service的趋势,但是一些特有或相对老旧的系统依然使用了传统的soap web service,例如银行.航空公司的机票查询接口等.本博客主要讲解得是spring boot整合cxf发布webservice服务和spring boot整合cxf客户端调用webservice服务本案例使用maven方式二.编码核心文件清单1.pom.xml <?xml version="1.0"

spring boot 1.5.4 之web开发(三)

上一篇:springboot 1.5.4 入门和原理(二) spring Boot 之web开发 更多更详细的配置参考文件:application.properties和<SpringBoot之application配置详解>(新版本新增属性缺失)  或参考官网http://projects.spring.io/spring-boot/ 注意:Spring Boot工程默认沿用前文中的project,如有变动,各章节会明确说明 注释springboot工程中的application.prope

《深入实践Spring Boot》第一部分 基础应用开发

第一部分 基础应用开发 第1章 Spring Boot入门 第2章 在Spring Boot中使用数据库 第3章 Spring Boot界面设计 第4章 提高数据库访问性能 第5章 Spring Boot安全设计 这一部分从搭建开发环境,简单入门,到使用数据库.界面设计.安全管理等一系列内容,介绍了使用Spring Boot框架进行基础应用开发的方法. 第1章 介绍了开发环境的搭建和开发工具的选择和安装,并以一个非常简单的实例,演示了如何使用Spring Boot框架创建工程和发布应用. 第2章

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

一.前言 前后端分离开发是将项目开发工作前后端交互工作拆分,使前端开发人员能够专注于页面开发或APP 开发,后端开发人员专注与接口开发.业务逻辑开发. 此处开发后端项目,给前端或者APP 端提供接口.不涉及复杂的业务逻辑,通过简单的增删改查来演示后端开发项目. 环境介绍: 开发工具:IDEA JDK: 1.7 及以上 Spring Boot: 2.0 及以上 Maven: 3.0 及以上 二.新建Spring Boot 项目 通过功能菜单File - New - Project 新建Spring

Spring Boot加速Java web项目的开发

软件152唐伟 我想,现在企业级的Java web项目应该或多或少都会使用到Spring框架的. 回首我们以前使用Spring框架的时候,我们需要首先在(如果你使用Maven的话)pom文件中增加对相关的的依赖(使用gradle来构建的话基本也一样)然后新建Spring相关的xml文件,而且往往那些xml文件还不会少.然后继续使用tomcat或者jetty作为容器来运行这个工程.基本上每次创建一个新的项目都是这么一个流程,而我们有时候仅仅想快速的创建一个Spring web工程来测试一些东西,或

Spring Boot 2.X 微信公众平台开发之接入

声明 : 本系列纯属自己为了学习而编写,均已测试号为例,如果不正之处,恳请指正,谢谢! 接入微信公众平台开发,开发者需要按照如下步骤完成: 1.填写服务器配置 由于只是接入,只需要一个Controller的方法路径 和 定义一个token,可以写在配置文件里 2.验证服务器地址的有效性 /** * FileName: CoreController * Author: Phil * Date: 8/1/2018 15:52 * Description: 接入微信并处理消息事件 * History:

《深入实践Spring Boot》阅读笔记之一:基础应用开发

上上篇「1718总结与计划」中提到,18年要对部分项目拆分,进行服务化,并对代码进行重构.公司技术委员会也推荐使用spring boot,之前在各个技术网站中也了解过,它可以大大简化spring配置和各个组件的使用,与其关系密切的Spring Cloud可以很好支持微服务的开发. 为了后续项目的应用,想利用这2天看下<深入实践Spring Boot>,这本书是17年双十一期间在京东上买的,一直懒着没看.这本书偏应用,适合初学者看,正文内容也就240多页,看的会比较轻松. 目前,看完了第一部分,

Spring Boot入门,一步一步简化,实现Spring Web项目开发

一. Spring Boot介绍 Spring Boot诞生的目的就是用来简化Spring应用开发过程.该框架使用了特定的方式来进行配置,从而使得开发人员不在需要定义一系列样板化的配置文件,而专注于核心业务开发.帮助开发人员快速的构建出基于Spring的应用.它会在后台整合项目所需的第三方依赖类库或框架,不再需要编写复杂的XML配置文件,仅通过几行代码就能实现一个可运行的Web应用. 直接嵌入 Tomcat 或 Jetty 服务器,不需要部署 WAR 文件. 提供许多基于Maven的 POM配置

Springboot 系列(五)Spring Boot web 开发之静态资源和模版引擎

前言 Spring Boot 天生的适合 web 应用开发,它可以快速的嵌入 Tomcat, Jetty 或 Netty 用于包含一个 HTTP 服务器.且开发十分简单,只需要引入 web 开发所需的包,然后编写业务代码即可. 自动配置原理? 在进行 web 开发之前让我再来回顾一下自动配置,可以参考系列文章第三篇.Spring Boot 为 Spring MVC 提供了自动配置,添加了如下的功能: 视图解析的支持. 静态资源映射,WebJars 的支持. 转换器 Converter 的支持.