码迷,mamicode.com
首页 > Web开发 > 详细

使用CXF做简单的WebService例子

时间:2018-04-23 22:43:18      阅读:269      评论:0      收藏:0      [点我收藏+]

标签:tail   集成   ide   ebs   catch   tin   ips   localhost   first   

使用Maven搭建项目测试简单的CXF实例

Server:

  pom.xml:

    <!-- begin CXF Server -->
    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-frontend-jaxws</artifactId>
        <version>3.1.1</version>
    </dependency>
    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-transports-http</artifactId>
        <version>3.1.1</version>
    </dependency>
    <dependency>
        <!-- 如果CXF不集成到Web服务器中,必须添加该引用 -->
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-transports-http-jetty</artifactId>
        <version>3.1.1</version>
    </dependency>
    <!-- End CXF Server -->

定义WebServer访问接口: ICXFService

package com.cxf;

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

@WebService(name = "cxfService", targetNamespace = "http://localhost/services/testCXF")
public interface ICXFService {

    @WebMethod
    String test1(@WebParam(name = "name")String name);
}

定义接口的具体实现:CXFServiceImpl

package com.cxf.impl;

import javax.jws.WebService;

import com.cxf.ICXFService;

@WebService(endpointInterface = "com.cxf.ICXFService",
            portName = "HelloCXF",
            serviceName = "HelloCXFService",
            targetNamespace = "http://localhost/services/testCXF")
public class CXFServiceImpl implements ICXFService {

    @Override
    public String test1(String name) {
        return "Hello " + name;
    }

}

测试服务:

package com.cxf;

import org.apache.cxf.jaxws.JaxWsServerFactoryBean;

import com.cxf.impl.CXFServiceImpl;

public class CXFServiceRun {

    public static void main(String[] args) {
        JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean();
        factory.setServiceClass(ICXFService.class);
        factory.setAddress("http://localhost:8888/services/hello");
        factory.setServiceBean(new CXFServiceImpl());
        factory.create();
    }
}

访问: http://localhost:8888/services/hello?wsdl

技术分享图片

Client:

创建CXF的客户端maven工程

添加 CXF 必须的 jar :

<!-- begin CXF Client -->
    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-frontend-jaxws</artifactId>
        <version>3.1.1</version>
    </dependency>
    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-transports-http</artifactId>
        <version>3.1.1</version>
    </dependency>
    <!-- End CXF Client -->

下载 apache 的 CXF 安装包 本次使用的是 apache-cxf-3.1.15 解压,配置环境变量就可以使用。

技术分享图片

配置环境变量:

技术分享图片

配置完成之后再 命令窗口 下执行 wsdl2java -help 出现如下信息表示配置成功:

技术分享图片 

执行:wsdl2java -encoding UTF-8 -d C:\Users\丰志\Desktop\cxfServer\src\main\java -p com.cxf.generate http://localhost:8888/services/hello?wsdl 

-d 后面跟生成java代码的目录, -p后面跟生成代码的包名称,最后跟wsdl的链接地址(或wsdl文件路径 + 文件名称)

技术分享图片

生成的代码目录文件:

技术分享图片

然后将CXF生成的代码粘贴到客户端项目中(也可以直接将代码生成到eclipse中的webService的客户端工程中)

编写客户端测试:TestClient


package com.cxf.client;

import java.net.MalformedURLException;
import java.net.URL;

import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;

import com.cxf.generate.CxfService;
import com.cxf.generate.ICXFServiceService;

public class TestClient {

    public static void main(String[] args) {
        //jaxws调用
//        URL不是必须的,除非服务的地址有改变
//        URL wsdlUrl = null;
//        try {
//            wsdlUrl = new URL("http://localhost:8888/services/hello?wsdl");
//        } catch (MalformedURLException e) {
//            e.printStackTrace();
//        }
//        ICXFServiceService factory = new ICXFServiceService(wsdlUrl);
////        ICXFServiceService factory = new ICXFServiceService();
//        CxfService cxfService = factory.getCxfServicePort();
//        String name = cxfService.test1("lisi");
//        System.out.println(name);
        
        // CXF 调用
        JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
        factory.setServiceClass(CxfService.class);
        factory.setAddress("http://localhost:8888/services/hello");
        CxfService cxfService = factory.create(CxfService.class);
        String name = cxfService.test1("lisi");
        System.out.println(name);
    }

}

两种实现方式都可以

运行成功:

技术分享图片

 源码:https://files.cnblogs.com/files/guofz/FirstCXF.rar

参考:https://blog.csdn.net/accountwcx/article/details/47082487

使用CXF做简单的WebService例子

标签:tail   集成   ide   ebs   catch   tin   ips   localhost   first   

原文地址:https://www.cnblogs.com/guofz/p/8921954.html

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