因此Web瑟维斯上传文件,包含大文件的拍卖

CXF:通过WebService上传文件,包蕴大文件的拍卖,cxfwebservice

因此Web瑟维斯上传文件,包含大文件的拍卖。参考英特网文章,用CXF发表上传文件接口,并上传大文件的测量试验。

框架:spring3.1+cxf2.7.6

1.定义文件类实体

import javax.activation.DataHandler;
import javax.xml.bind.annotation.XmlMimeType;

/**
 * CXF上传和下载文件对象包装类 由于CXF的DataHandler无法获取文件名和文件类型,需要在上传和下载时附带文件名
 * 
 */
public class CxfFileWrapper {
    // 文件名
    private String fileName = null;
    // 文件扩展名
    private String fileExtension = null;
    // 文件二进制数据
    private DataHandler file = null;

    public final String getFileName() {
        return fileName;
    }

    public final void setFileName(String fileName) {
        this.fileName = fileName;
    }

    public final String getFileExtension() {
        return fileExtension;
    }

    public final void setFileExtension(String fileExtension) {
        this.fileExtension = fileExtension;
    }

    // 注解该字段为二进制流
    @XmlMimeType("application/octet-stream")
    public final DataHandler getFile() {
        return file;
    }

    public final void setFile(DataHandler file) {
        this.file = file;
    }
}

2.定义服务接口

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;

import net.bwda.domain.upload.CxfFileWrapper;


@WebService(name = "FileWS", serviceName = "FileWS")
public interface FileWS {
    /**
     * 文件上传
     * @param file 文件上传包装类
     * @return 上传成功返回true,上传失败返回false。
     */
    @WebMethod
    boolean upload(@WebParam(name = "file") CxfFileWrapper file);

    /**
     * 文件下载
     * @return 文件
     */
    @WebMethod
    CxfFileWrapper download();
}


import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;

import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import net.bwda.domain.upload.CxfFileWrapper;
import net.bwda.service.upload.FileWS;

@Service
@Component("fileWS")
public class FileWSImpl implements FileWS {

    public boolean upload(CxfFileWrapper file){
        boolean result = true;
        OutputStream os = null;
        InputStream is = null;
        BufferedOutputStream bos = null;
        try {
            is = file.getFile().getInputStream();
            File dest = new File("d:\\upload\\" + file.getFileName());
            os = new FileOutputStream(dest);
            bos = new BufferedOutputStream(os);
            byte[] buffer = new byte[1024*1024];
            int len = 0;
            while ((len = is.read(buffer)) != -1) {
                bos.write(buffer, 0, len);
            }
            bos.flush();
        } catch (Exception e) {
            e.printStackTrace();
            result = false;
        } finally {
            if(bos != null){
                try{
                    bos.close();
                }catch(Exception e){                    
                }
            }
            if(os != null){
                try{
                    os.close();
                }catch(Exception e){                    
                }
            }
            if(is != null){
                try{
                    is.close();
                }catch(Exception e){                    
                }
            }
        }
        return result;
    }

    public CxfFileWrapper download() {
        //下载文件的路径
         String filePath = "D:\\test.xlsx";
        CxfFileWrapper fileWrapper = new CxfFileWrapper();
        fileWrapper.setFileName("test.xlsx");
        fileWrapper.setFileExtension("xlsx");
        DataSource source = new FileDataSource(new File(filePath));
        fileWrapper.setFile(new DataHandler(source));
        return fileWrapper;
    }

}

3.定义CXF配置文件

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws"
    xsi:schemaLocation="
    http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"
    xmlns:cxf="http://cxf.apache.org/core">

    <import resource="classpath:META-INF/cxf/cxf.xml" />
    <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />

    <cxf:bus>
        <cxf:properties>
            <!-- 定义上传文件的最大长度,限制为256M -->
            <entry key="org.apache.cxf.stax.maxTextLength" value="268435456" />
        </cxf:properties>
    </cxf:bus>

    <jaxws:endpoint id="fileWSEndpoint" implementor="#fileWS"
        address="/file">
        <jaxws:properties>
            <!-- 开启MTOM -->
            <entry key="mtom_enabled" value="true"></entry>
        </jaxws:properties>
    </jaxws:endpoint>

</beans>

更多CXF配置,可参见:

 

4.生成客户端代码

d:\apache-cxf-2.7.6\bin\wsdl2java.bat -frontend jaxws21 -p com..test.webservice.file -client http://localhost:9080/filemark/webservice/file?wsdl

5.依照活动上传的代码上传文件测量检验

/**
 * Please modify this class to meet your needs
 * This class is not complete
 */

import java.io.File;
import java.net.URL;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.xml.namespace.QName;

/**
 * This class was generated by Apache CXF 2.7.6
 * 2016-11-10T18:37:44.270+08:00
 * Generated source version: 2.7.6
 * 
 */
public final class FileWS_FileWSImplPort_Client {

    private static final QName SERVICE_NAME = new QName("http://upload.serviceimpl.bwda.net/", "FileWSImplService");

    private FileWS_FileWSImplPort_Client() {
    }

    public static void main(String args[]) throws java.lang.Exception {
        URL wsdlURL = FileWSImplService.WSDL_LOCATION;
        FileWSImplService ss = new FileWSImplService(wsdlURL, SERVICE_NAME);
        FileWS port = ss.getFileWSImplPort();  

        {
        System.out.println("Invoking upload...");
        net.bwda.Test.webservice.CxfFileWrapper _upload_file = new CxfFileWrapper();
        _upload_file.setFileName("test.csv");
        _upload_file.setFileExtension("csv");
        DataSource source = new FileDataSource(new File("d:\\test.csv"));
        _upload_file.setFile(new DataHandler(source));
        boolean _upload__return = port.upload(_upload_file);
        System.out.println("upload.result=" + _upload__return);
        }

        System.exit(0);
    }

}

仿照效法英特网小说,用CXF发表上传文件接口,并上传大文件的测验。
框架:spring3….

参照他事他说加以考察英特网文章,用CXF发表上传文件接口,并上传大文件的测量检验。

  CXF简介

工作也快五个月了,时间相当的慢,开采本人越来越懒了,书都堆灰了…平常专门的职业虽忙不过周六不忙啊哈哈,看了zhoumorvan大神的博客和身边给力的伴儿们,决定之后恐怕多看看多写写多享受。

框架:spring3.1+cxf2.7.6

  Apache CXF = Celtix + XFire,初始叫 Apache CeltiXfire,后来更名叫Apache CXF 了。CXF 承接了 Celtix 和 XFire
两大开源项目标优秀,提供了对 JAX-WS 全面包车型的士支撑,何况提供了多样 Binding
、DataBinding、Transport 以及种种 Format
的扶助,而且能够依赖实际项目标内需,选择代码优先(Code First)可能 WSDL
优先(WSDL First)来轻易地达成 Web Services 的公告和使用。Apache
CXF已经是二个正规的Apache超级项目。

笔录是多个极好的习贯,大家会的知识中,有相当大学一年级部分实在是冲突零散的,很轻松忘记。将它们整理并记下来是叁个结构化知识的历程,也是三个整治和小结的长河。

1.定义文件类实体

  Apache CXF 是二个开源的 Services 框架,CXF 协助你使用 Frontend 编制程序API 来创设和开采 Services ,像 JAX-WS 。那几个 Services
能够支撑两种协议,举例:SOAP、XML/HTTP、RESTful HTTP
只怕 CORBA ,何况能够在三种传输协议上运维,比方:HTTP、JMS 大概 JBI,CXF
大大简化了 Services 的创造,同期它三番捌遍了 XFire 守旧,一样能够天然地和
Spring 举行无缝集成。

专门的工作首先篇,来个极简的:近些日子交接了总人口消息使用平台的Web
Service服务,第一遍接触,就说一说使用CFX来调用Web
Service服务的艺术(没写怎么样发表Web
Service服务,因为没做,可参考CXF和Spring整合宣布和基于Maven在Spring中集成CXF
Web Service框架)。

import javax.activation.DataHandler;
import javax.xml.bind.annotation.XmlMimeType;

/**
 * CXF上传和下载文件对象包装类 由于CXF的DataHandler无法获取文件名和文件类型,需要在上传和下载时附带文件名
 * 
 */
public class CxfFileWrapper {
    // 文件名
    private String fileName = null;
    // 文件扩展名
    private String fileExtension = null;
    // 文件二进制数据
    private DataHandler file = null;

    public final String getFileName() {
        return fileName;
    }

    public final void setFileName(String fileName) {
        this.fileName = fileName;
    }

    public final String getFileExtension() {
        return fileExtension;
    }

    public final void setFileExtension(String fileExtension) {
        this.fileExtension = fileExtension;
    }

    // 注解该字段为二进制流
    @XmlMimeType("application/octet-stream")
    public final DataHandler getFile() {
        return file;
    }

    public final void setFile(DataHandler file) {
        this.file = file;
    }
}

  CXF优点

1、下载CXF,解压即用

2.概念服务接口

  (1)辅助各种说道:

2、首要行使wadl2java那一个工具,将Web 瑟维斯宣布的劳务转移客户端java代码

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;

import net.bwda.domain.upload.CxfFileWrapper;


@WebService(name = "FileWS", serviceName = "FileWS")
public interface FileWS {
    /**
     * 文件上传
     * @param file 文件上传包装类
     * @return 上传成功返回true,上传失败返回false。
     */
    @WebMethod
    boolean upload(@WebParam(name = "file") CxfFileWrapper file);

    /**
     * 文件下载
     * @return 文件
     */
    @WebMethod
    CxfFileWrapper download();
}


import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;

import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import net.bwda.domain.upload.CxfFileWrapper;
import net.bwda.service.upload.FileWS;

@Service
@Component("fileWS")
public class FileWSImpl implements FileWS {

    public boolean upload(CxfFileWrapper file){
        boolean result = true;
        OutputStream os = null;
        InputStream is = null;
        BufferedOutputStream bos = null;
        try {
            is = file.getFile().getInputStream();
            File dest = new File("d:\\upload\\" + file.getFileName());
            os = new FileOutputStream(dest);
            bos = new BufferedOutputStream(os);
            byte[] buffer = new byte[1024*1024];
            int len = 0;
            while ((len = is.read(buffer)) != -1) {
                bos.write(buffer, 0, len);
            }
            bos.flush();
        } catch (Exception e) {
            e.printStackTrace();
            result = false;
        } finally {
            if(bos != null){
                try{
                    bos.close();
                }catch(Exception e){                    
                }
            }
            if(os != null){
                try{
                    os.close();
                }catch(Exception e){                    
                }
            }
            if(is != null){
                try{
                    is.close();
                }catch(Exception e){                    
                }
            }
        }
        return result;
    }

    public CxfFileWrapper download() {
        //下载文件的路径
         String filePath = "D:\\test.xlsx";
        CxfFileWrapper fileWrapper = new CxfFileWrapper();
        fileWrapper.setFileName("test.xlsx");
        fileWrapper.setFileExtension("xlsx");
        DataSource source = new FileDataSource(new File(filePath));
        fileWrapper.setFile(new DataHandler(source));
        return fileWrapper;
    }

}

    SOAP1.1,1.2

 wsdl2java -encoding utf-8 -p com.test.nciic -d D:\\src -all NciicServices.wsdl

3.定义CXF配置文件

    XML/HTTP

  • encoding:编码
  • p:package(那个包的结构要求留神,要求与代码中的包结构一样)
  • d:代码生成目录

www.5929.com 1

因此Web瑟维斯上传文件,包含大文件的拍卖。    CORBA(Common Object Request Broker
Architecture公共对象诉求代理系统布局,开始的一段时代语言应用的WS。C,c++,C#)

3、通过CXF的JaxWsProxyFactoryBean类来调用Web Service服务

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws"
    xsi:schemaLocation="
    http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"
    xmlns:cxf="http://cxf.apache.org/core">

    <import resource="classpath:META-INF/cxf/cxf.xml" />
    <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />

    <cxf:bus>
        <cxf:properties>
            <!-- 定义上传文件的最大长度,限制为256M -->
            <entry key="org.apache.cxf.stax.maxTextLength" value="268435456" />
        </cxf:properties>
    </cxf:bus>

    <jaxws:endpoint id="fileWSEndpoint" implementor="#fileWS"
        address="/file">
        <jaxws:properties>
            <!-- 开启MTOM -->
            <entry key="mtom_enabled" value="true"></entry>
        </jaxws:properties>
    </jaxws:endpoint>

</beans>

  (2)能够与Spring进行火速无缝的咬合

JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();factory.setServiceClass(NciicServicesPortType.class);factory.setAddress("https://ws.nciic.org.cn/nciic_ws/services/NciicServices");private NciicServicesPortType client = (NciicServicesPortType) factory.create();String responseXmlStr = client.nciicCheck(nciicAuthKey, requestXmlStr); // 调用nciic服务

更多CXF配置,可参见:。

  (3)灵活的安顿:能够运作在汤姆cat,Jboss,Jetty(内置),IBMWS,BeaWL上边。

也能够融为一体在spring mvc中

 

   看这么些在此之前能够看看关于WebService的小说:链接

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:jaxrs="http://cxf.apache.org/jaxrs" xmlns:cxf="http://cxf.apache.org/core" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd"> <!-- 使用<jaxws:clietn>调用服务端 jaxws:client内部使用JaxWsProxyFactoryBean方式 serviceClass:指定portType地址(需要使用wsdl2java工具生成) --> <jaxws:client address="https://ws.nciic.org.cn/nciic_ws/services/NciicServices" serviceClass="com.test.nciic.NciicServicesPortType"> </jaxws:client></beans>

public void testCxfSpringClient(){ //从Spring容器中取出portType NciicServicesPortType client = (NciicServicesPortType) applicationContext.getBean; String responseXmlStr = client.nciicCheck(nciicAuthKey, requestXmlStr); // 调用nciic服务}

4.生成客户端代码

  接下去就从头写贰个CXF的简短入门程序

这里的Nciic瑟维斯sPortType是CXF生成的Web
Service接口类,里面定义了wsdl文件所描述的保有服务接口,格局如下:

d:\apache-cxf-2.7.6\bin\wsdl2java.bat -frontend jaxws21 -p com..test.webservice.file -client http://localhost:9080/filemark/webservice/file?wsdl

 

/** * This class was generated by Apache CXF 3.1.13 * 2017-10-27T17:18:40.243+08:00 * Generated source version: 3.1.13 * */@WebService(targetNamespace = "https://api.nciic.org.cn/NciicServices", name = "NciicServicesPortType")@XmlSeeAlso({ObjectFactory.class})public interface NciicServicesPortType {@WebResult(name = "out", targetNamespace = "https://api.nciic.org.cn/NciicServices") @RequestWrapper(localName = "nciicCheck", targetNamespace = "https://api.nciic.org.cn/NciicServices", className = "com.test.nciic.NciicCheck") @WebMethod @ResponseWrapper(localName = "nciicCheckResponse", targetNamespace = "https://api.nciic.org.cn/NciicServices", className = "com.test.nciic.NciicCheckResponse") public String nciicCheck( @WebParam(name = "inLicense", targetNamespace = "https://api.nciic.org.cn/NciicServices") String inLicense, @WebParam(name = "inConditions", targetNamespace = "https://api.nciic.org.cn/NciicServices") String inConditions );}

5.依照活动上传的代码上传文件测量检验

  一、服务端程序

PS:Nciic这么些是个自签定的https,还索要安装它网址的证件,参考StevenLian这一个博客。附叁个java加多证书信任列表的静态代码块:

www.5929.com 2

  首先去官方网址下载jar包

static { javax.net.ssl.HttpsURLConnection.setDefaultHostnameVerifier( new javax.net.ssl.HostnameVerifier() { @Override public boolean verify(String hostname, javax.net.ssl.SSLSession sslSession) { //域名或ip地址 if (hostname.equals("ws.nciic.org.cn")) { return true; } return false; } }); System.setProperty("javax.net.ssl.trustStore", "C:\\jssecacerts"); System.setProperty("javax.net.ssl.trustStorePassword", "changeit"; }
/**
 * Please modify this class to meet your needs
 * This class is not complete
 */

import java.io.File;
import java.net.URL;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.xml.namespace.QName;

/**
 * This class was generated by Apache CXF 2.7.6
 * 2016-11-10T18:37:44.270+08:00
 * Generated source version: 2.7.6
 * 
 */
public final class FileWS_FileWSImplPort_Client {

    private static final QName SERVICE_NAME = new QName("http://upload.serviceimpl.bwda.net/", "FileWSImplService");

    private FileWS_FileWSImplPort_Client() {
    }

    public static void main(String args[]) throws java.lang.Exception {
        URL wsdlURL = FileWSImplService.WSDL_LOCATION;
        FileWSImplService ss = new FileWSImplService(wsdlURL, SERVICE_NAME);
        FileWS port = ss.getFileWSImplPort();  

        {
        System.out.println("Invoking upload...");
        net.bwda.Test.webservice.CxfFileWrapper _upload_file = new CxfFileWrapper();
        _upload_file.setFileName("test.csv");
        _upload_file.setFileExtension("csv");
        DataSource source = new FileDataSource(new File("d:\\test.csv"));
        _upload_file.setFile(new DataHandler(source));
        boolean _upload__return = port.upload(_upload_file);
        System.out.println("upload.result=" + _upload__return);
        }

        System.exit(0);
    }

}

  官方下载网站:

PS:Nciic那个服务挺慢的,还挺贵的…大家在调用时参预memcache缓存,制止有的时候同样央求重复调用,而且将评释通过的结果写入到数据库中,先查缓存和数据库最后再用第三方服务。

  步入此页面后,拜见到此页面

Web
Service通过依照XML的SOAP来代表数据和乞请,低耦合,在接口的昭示和复用性上实在还不错,接口的可读性也足以,但是真的照旧尚未调Http服务的api接口方便,比比较多国有公司和国家的对外地劳工务貌似Web
Service用的很多,可是在互连网公司貌似用的非常少。Nciic服务使用Xml做央求和再次回到格局照旧未有json方便,orz。

 www.5929.com 3

2017/11/18 done

  下载好后会看到那般三个压缩文件:www.5929.com 4

此小说也一并至个人Github博客

  解压后,有这个目录文件

www.5929.com 5

  下边简要介绍一下那几个目录以及中间文件的效应

   (1)bin:是 CXF 框架中所提供的代码生成、校验、管控台工具:

   (2)docs:CXF 全体类(class)对应的 API 文书档案,为开荒者使用 CXF
完毕应用开荒提供相应的援救。

   (3)etc:包蕴多少个基本的 Service 暴光所需求的 web.xml
文件,及别的的布署文件。

   (4)lib:目录中隐含 CXF
及其运维时所急需的和可选的第三方辅助类包(.jar
文件),可以依赖差异门类所需的 CXF 性情采取所必要的支持类包。

   (5)licenses:列表了援引第三方 jar 包的相干许可协议。

   (6)samples:samples 目录中带有了有着随 CXF
二进制包宣布的言传身教,包蕴那个示例的源代码和连锁 Web
应用配置文件,能够一本万利地用 Ant 来编写翻译运营测量检验那么些示例,来打听 CXF
的支出和行使的办法。可以经过 samples
目录和它各个子目录下的 README.txt
的文本来详细询问示例的编写翻译与运作的手续。

   (7)LICENSE :文件中蕴藏了 CXF 框架的授权协议 Apache License
Version 2.0 。

   (8)NOTICE :罗列了 CXF
框架用到的有关第三方组件的授权协议以别的的连带信息。

   (9)README :文件中满含了 CXF 框架本身的一部分数之差非常少表明。

     (10)release_notes.txt :包括了 CXF
发表时的部分音信,包涵运行时所急需的条件,修复 BUG 的列表等。

  接下去,第一步:成立动态web项目

  第二步:导入CXF相关jar包,以下jar包是选择CXF所必须的jar包(CXF能运作起来最基本的jar包)

 www.5929.com 6

  第三步:在web.xml中配置CXF框架提供的叁个Servlet

  <!-- 配置CXF框架提供的Servlet -->
  <servlet>
      <servlet-name>cxf</servlet-name>
      <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
      <!-- 通过初始化参数指定CXF框架的配置文件位置 -->
      <init-param>
          <param-name>config-location</param-name>
          <param-value>classpath:cxf.xml</param-value>
      </init-param>
  </servlet>
  <servlet-mapping>
      <servlet-name>cxf</servlet-name>
      <url-pattern>/service/*</url-pattern>
  </servlet-mapping>

  第四步:在类路径下提供cxf.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:soap="http://cxf.apache.org/bindings/soap"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
                    http://www.springframework.org/schema/beans/spring-beans.xsd
                    http://cxf.apache.org/bindings/soap 
                    http://cxf.apache.org/schemas/configuration/soap.xsd
                    http://cxf.apache.org/jaxws 
                    http://cxf.apache.org/schemas/jaxws.xsd">

</beans>

  第五步:开垦多少个接口和贯彻类

  接口,接口上必须利用@WebService完结类(jdk1.6及以往才支撑@WebService评释,所以要确定保障jdk版本够高)

import javax.jws.WebService;

@WebService
public interface TestCXFService {
    public String firstCXF(String name);
}

  实现类

public class TestCXFServiceImpl implements TestCXFService{
    @Override
    public String firstCXF(String name) {
        System.out.println("基于CXF开发的服务端firstCXF被调用了");
        return "Hello "+name;
    }
}

  第六步:在cxf.xml中注册服务

<bean id="firstCXFService" class="com.cxf.service.impl.TestCXFServiceImpl"/>
<!-- 注册服务  -->
<jaxws:server id="myCXFService" address="/cxfService">
    <jaxws:serviceBean>
        <ref bean="firstCXFService"/>
    </jaxws:serviceBean>
</jaxws:server>

  到明天,使用CXF配置的劳动就铺排成功了

  接下去,运转tomcat,在浏览器访谈那个地址:

  www.5929.com, / 端口 / 你所创制的工程的名目 /
本身在web.xml配置的门道  / 本身陈设的address?wsdl

  注意:(1)由于笔者动用tomcat所以端口号为8080

       (2)<url-pattern>/service/*</url-pattern>

         

(3)address=”/cxfService”

  若出现那些页面表达成功了

This XML file does not appear to have any style information associated with it. The document tree is shown below.
<wsdl:definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://impl.service.cxf.com/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:ns2="http://schemas.xmlsoap.org/soap/http" xmlns:ns1="http://service.cxf.com/" name="TestCXFServiceImplService" targetNamespace="http://impl.service.cxf.com/">
<wsdl:import location="http://localhost:8080/testCXF/service/cxfService?wsdl=TestCXFService.wsdl" namespace="http://service.cxf.com/"></wsdl:import>
<wsdl:binding name="TestCXFServiceImplServiceSoapBinding" type="ns1:TestCXFService">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="firstCXF">
<soap:operation soapAction="" style="document"/>
<wsdl:input name="firstCXF">
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output name="firstCXFResponse">
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="TestCXFServiceImplService">
<wsdl:port binding="tns:TestCXFServiceImplServiceSoapBinding" name="TestCXFServiceImplPort">
<soap:address location="http://localhost:8080/testCXF/service/cxfService"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>

 

   二、客户端程序

  有二种格局:方式一:使用jdk提供的wsimport命令生开销地代码实现调用(另一篇介绍webservice的文章中有利用,链接在文章初始给出了,在那边不再采纳)

        格局二:使用CXF提供的格局(大家采纳第二种情势)    

  第一步:创制Java项目并导入CXF相关jar包 (jar包同上)

  第二步:使用wsimport只怕CXF提供wsdl2java生产资料产地代码,只供给扭转接口文件

  张开cmd步入CXF文件夹的bin目录下(如apache-cxf-3.2.0\bin),输入

wsdl2java -d . -p com.cxf.client http://localhost:8080/testCXF/service/cxfService?wsdl

  .代表将扭转的文书放到当前目录下,-p代表生成的目录结构,最终的地点代表基于哪个wsdl生成文件

  敲回车,出现以下结果,和艺术一不一致那一个成功后,未有别的提醒

www.5929.com 7

  那时就意识,当前目录下出现了那个文件夹

www.5929.com 8

  展开后,有以下目录文件,而大家只要求使用四个接口文件(红框标出来的)

www.5929.com 9

  

  第三步:将接口文件复制到项目中

  www.5929.com 10

  复制过来后,我们必要将红框内内容删掉,因为大家只把三个接口文件复制过来了,别的的并未有动,所以找不到这几个文件,而笔者辈又用不到,所以平昔删掉就好  www.5929.com 11

  删除后接口内代码是那样的

package com.cxf.client;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.xml.bind.annotation.XmlSeeAlso;
import javax.xml.ws.RequestWrapper;
import javax.xml.ws.ResponseWrapper;

/**
 * This class was generated by Apache CXF 3.2.0
 * 2017-09-24T12:35:54.073+08:00
 * Generated source version: 3.2.0
 * 
 */
@WebService(targetNamespace = "http://service.cxf.com/", name = "TestCXFService")
@XmlSeeAlso({})
public interface TestCXFService {

    @WebMethod
    @RequestWrapper(localName = "firstCXF", targetNamespace = "http://service.cxf.com/", className = "com.cxf.client.FirstCXF")
    @ResponseWrapper(localName = "firstCXFResponse", targetNamespace = "http://service.cxf.com/", className = "com.cxf.client.FirstCXFResponse")
    @WebResult(name = "return", targetNamespace = "")
    public java.lang.String firstCXF(
        @WebParam(name = "arg0", targetNamespace = "")
        java.lang.String arg0
    );
}

  第四步:提供spring配置文件,注册客户端代理对象

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:soap="http://cxf.apache.org/bindings/soap"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
                    http://www.springframework.org/schema/beans/spring-beans.xsd
                    http://cxf.apache.org/bindings/soap 
                    http://cxf.apache.org/schemas/configuration/soap.xsd
                    http://cxf.apache.org/jaxws 
                    http://cxf.apache.org/schemas/jaxws.xsd">

    <!-- 注册CXF客户端代理对象,通过spring框架创建这个代理对象,使用代理对象实现远程调用 -->
    <jaxws:client id="myCXFClient" address="http://localhost:8080/testCXF/service/cxfService" serviceClass="com.cxf.client.TestCXFService">

    </jaxws:client>
</beans>

  第五步:写三个测量试验类,读取spring配置文件,成立spring工厂,从工厂中获得代理对象,完毕远程调用

package com.cxf.client;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestCXFClient {
    public static void main(String[] args) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:cxf.xml");
        TestCXFService proxy = (TestCXFService) ctx.getBean("myCXFClient");
        String result = proxy.firstCXF("my first CXFClient");
        System.out.println(result);
    }
}

  执行main方法(此时要保管服务端运转着),出现以下结果,就认证程序运转成功了

  客户端调控台打字与印刷

        www.5929.com 12

  服务端调节台打字与印刷

        www.5929.com 13

  到未来,CXF入门主次就成功了。

 

  

 

  

 

 

Leave a Comment.