首先上项目的pom.xml:

 <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.mathxh-webservice</groupId>
<artifactId>webservice</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>webservice</name>
<description>Learning WebService</description> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.14.BUILD-SNAPSHOT</version>
<relativePath/> <!-- lookup parent from repository -->
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <!-- CXF webservice -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-spring-boot-starter-jaxws</artifactId>
<version>3.1.11</version>
</dependency>
<!-- CXF webservice -->
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> <repositories>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories> <pluginRepositories>
<pluginRepository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
<pluginRepository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories> </project>

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

 package com.mathxhwebservice.webservice.service;

 /**
* 接口
*
* @author MathxH Chen
*
*/ import com.mathxhwebservice.webservice.mtom.BinaryFile; import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.xml.ws.soap.MTOM; @WebService(name = "CommonService", // 暴露服务名称
targetNamespace = "http://service.webservice.mathxhwebservice.com/")// 命名空间,一般是接口的包名倒序
@MTOM(threshold = 1024)
public interface CommonService { @WebMethod
@WebResult(name = "String")
String sayHello(@WebParam(name = "userName") String name); @WebMethod
@WebResult(name ="BinaryFile")
BinaryFile downloadFile(@WebParam(name = "fileName") String fileName); @WebMethod
@WebResult(name = "boolean")
boolean uploadFile(@WebParam(name = "file") BinaryFile file);
}

之后是实现WebService接口:

 package com.mathxhwebservice.webservice.service;

 import com.mathxhwebservice.webservice.mtom.BinaryFile;
import org.springframework.stereotype.Component; import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.jws.WebService;
import java.io.*; @WebService(serviceName = "CommonService", // 与接口中指定的name一致
targetNamespace = "http://service.webservice.mathxhwebservice.com/", // 与接口中的命名空间一致,一般是接口的包名倒
endpointInterface = "com.mathxhwebservice.webservice.service.CommonService"// 接口地址
)
@Component
public class CommonServiceImpl implements CommonService{ @Override
public String sayHello(String name) {
return "Hello ," + name;
} @Override
public BinaryFile downloadFile(String fileName){
BinaryFile file = new BinaryFile();
file.setTitle(fileName);
DataSource source = new FileDataSource(new File("d:" + File.separator + fileName));
file.setBinaryData(new DataHandler(source));
return file;
} @Override
public boolean uploadFile(BinaryFile file){
DataHandler dataHandler = file.getBinaryData();
String fileTitle = file.getTitle(); try (
InputStream is = dataHandler.getInputStream();
OutputStream os = new FileOutputStream(new File("d:" + File.separator + fileTitle));
BufferedOutputStream bos = new BufferedOutputStream(os))
{ byte[] buffer = new byte[100000];
int bytesRead = 0;
while ((bytesRead = is.read(buffer)) != -1) {
bos.write(buffer, 0, bytesRead);
} bos.flush();
} catch (IOException e) {
e.printStackTrace();
return false;
}
return true;
}
}

然后是配置WebService的发布类:

 package com.mathxhwebservice.webservice.config;

 import com.mathxhwebservice.webservice.service.CommonService;
import org.apache.cxf.Bus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import javax.xml.ws.Endpoint; @Configuration
public class CxfConfig {
@Autowired
private Bus bus; @Autowired
CommonService commonService; /** JAX-WS **/
@Bean
public Endpoint endpoint() {
EndpointImpl endpoint = new EndpointImpl(bus, commonService);
endpoint.publish("/CommonService"); return endpoint;
}
}

最后实现客户端调用(基本调用返回字符串,基于MTOM的上传下载文件):

 package com.mathxhwebservice.webservice.wsclient;

 import com.mathxhwebservice.webservice.mtom.BinaryFile;
import com.mathxhwebservice.webservice.service.CommonService;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean; import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import java.io.*; public class CxfClient {
public static void main(String[] args) {
//cl1();
// downloadTest();
uploadTest();
} /**
* 方式1.代理类工厂的方式,需要拿到对方的接口
*/
public static void cl1() {
try {
// 接口地址
String address = "http://localhost:8080/services/CommonService?wsdl";
// 代理工厂
JaxWsProxyFactoryBean jaxWsProxyFactoryBean = new JaxWsProxyFactoryBean();
// 设置代理地址
jaxWsProxyFactoryBean.setAddress(address);
// 设置接口类型
jaxWsProxyFactoryBean.setServiceClass(CommonService.class);
// 创建一个代理接口实现
CommonService cs = (CommonService) jaxWsProxyFactoryBean.create();
// 数据准备
String userName = "MathxH Chen";
// 调用代理接口的方法调用并返回结果
String result = cs.sayHello(userName);
System.out.println("返回结果:" + result);
} catch (Exception e) {
e.printStackTrace();
}
} public static void uploadTest(){
try{
// 接口地址
String address = "http://localhost:8080/services/CommonService?wsdl";
// 代理工厂
JaxWsProxyFactoryBean jaxWsProxyFactoryBean = new JaxWsProxyFactoryBean();
// 设置代理地址
jaxWsProxyFactoryBean.setAddress(address);
// 设置接口类型
jaxWsProxyFactoryBean.setServiceClass(CommonService.class);
// 创建一个代理接口实现
CommonService cs = (CommonService) jaxWsProxyFactoryBean.create(); BinaryFile file = new BinaryFile();
file.setTitle("uploaded.png");
DataSource source = new FileDataSource(new File("d:" + File.separator + "downloaded.png"));
file.setBinaryData(new DataHandler(source));
if(cs.uploadFile(file)) {
System.out.println("upload success");
}else{
System.out.println("upload failed");
} }catch (Exception e){
e.printStackTrace();
}
} public static void downloadTest(){
try{
// 接口地址
String address = "http://localhost:8080/services/CommonService?wsdl";
// 代理工厂
JaxWsProxyFactoryBean jaxWsProxyFactoryBean = new JaxWsProxyFactoryBean();
// 设置代理地址
jaxWsProxyFactoryBean.setAddress(address);
// 设置接口类型
jaxWsProxyFactoryBean.setServiceClass(CommonService.class);
// 创建一个代理接口实现
CommonService cs = (CommonService) jaxWsProxyFactoryBean.create(); BinaryFile file = cs.downloadFile("test.png");
String title = file.getTitle();
DataHandler binaryData = file.getBinaryData(); try (
InputStream is = binaryData.getInputStream();
OutputStream os = new FileOutputStream(new File("d:" + File.separator + "downloaded.png"));
BufferedOutputStream bos = new BufferedOutputStream(os))
{ byte[] buffer = new byte[100000];
int bytesRead = 0;
while ((bytesRead = is.read(buffer)) != -1) {
bos.write(buffer, 0, bytesRead);
} bos.flush();
} catch (IOException e) {
e.printStackTrace(); } } catch (Exception e) {
e.printStackTrace();
}
}
}

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. solr6.5.0(windows)教程

    第一步:安装Tomcat8重命名结尾加上solr6(自定义) 第二步: 解压solr,把solr-6.5.0\solr-6.5.0\server\solr-webapp下的webapp文件夹拷贝到to ...

  2. 错误: 在类中找不到 main 方法, 请将 main 方法定义为:public static void main(String[] args)否则 JavaFX 应用程序类必须扩展javafx.ap

    最近在使用eclipse编写java程序时遇到这样一个问题: 错误在类中找不到main方法,请将main方法定义为 public static void main(String[] args)否则 J ...

  3. Codeforces 1036E Covered Points (线段覆盖的整点数)【计算几何】

    <题目链接> <转载于 >>>  > 题目大意: 在二维平面上给出n条不共线的线段(线段端点是整数),问这些线段总共覆盖到了多少个整数点. 解题分析: 用GC ...

  4. PHP Kohana入门体验教程

    打开入口文件kohana目录下的index.php, 左边选中的文件和右边选中的是对应,如果重命名的话两边都要修改. 设置程序默认时区D:\xampp\htdocs\kohana\applicatio ...

  5. Spring MVC中Controller返回值void时报错

    Controller如下: 当使用url访问该处理器方法时,报错如下: 26-Jan-2019 21:16:28.105 警告 [http-nio-8080-exec-39] org.springfr ...

  6. BZOJ4912 : [Sdoi2017]天才黑客

    建立新图,原图中每条边在新图中是点,点权为$w_i$,边权为两个字符串的LCP. 对字典树进行DFS,将每个点周围一圈边对应的字符串按DFS序从小到大排序. 根据后缀数组利用height数组求LCP的 ...

  7. px与rem的换算

    在线转化工具: http://www.ofmonkey.com/front/rem rem是相对于根元素<html>,这样就意味着,我们只需要在根元素确定一个参考值,这个参考值设置为多少, ...

  8. weak_ptr_c++11

    unique_ptr 替代了原来的auto_ptr,指向对象具有唯一性,即同一时间只能有unique_ptr指向给定对象(和auto_ptr不同是禁止拷贝语义,通过移动语义替代) unique_ptr ...

  9. flask内容学习之蓝图以及单元测试

    蓝图的概念: 简单来说,蓝图是一个存储操作方法的容器.这些操作在这个蓝图被注册到一个应用之后就可以被调用.Flask可以通过蓝图来制止URL以及处理请求.Flask使用蓝图来让应用实现模块化,在Fla ...

  10. 小甲鱼Python第十四课后习题

    字符串格式化符号含义    符   号    说     明      %c    格式化字符及其ASCII码[>>> '%c' %97        'a']      %s    ...