WebService的实现之二CXF实现

来源:互联网 发布:sai软件详细介绍 编辑:程序博客网 时间:2024/06/06 11:04

使用Maven实现CXF.

1、开发服务端

先看POM

<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>org.qf</groupId>    <artifactId>M_CXF_Server1</artifactId>    <version>0.0.1-SNAPSHOT</version>    <description>基于Apache的CXF实现的Web Service</description>    <!--添加依赖 -->    <dependencies>        <!-- https://mvnrepository.com/artifact/org.apache.cxf/cxf-rt-frontend-jaxws -->        <dependency>            <groupId>org.apache.cxf</groupId>            <artifactId>cxf-rt-frontend-jaxws</artifactId>            <version>3.1.12</version>        </dependency>        <!-- https://mvnrepository.com/artifact/org.apache.cxf/cxf-rt-transports-http-jetty -->        <dependency>            <groupId>org.apache.cxf</groupId>            <artifactId>cxf-rt-transports-http-jetty</artifactId>            <version>3.1.12</version>        </dependency>    </dependencies>    <build>        <plugins>            <plugin>                <groupId>org.apache.maven.plugins</groupId>                <artifactId>maven-compiler-plugin</artifactId>                <version>3.6.0</version>                <configuration>                    <source>1.8</source>                    <target>1.8</target>                    <encoding>UTF-8</encoding>                </configuration>            </plugin>        </plugins>    </build></project>

代码:
接口:

@WebService//标记这是一个Web服务public interface FoodInterface {    String getF();    String getFByN(@WebParam(name="name")String n);}

接口实现类:

public class FoodInterfaceImple implements FoodInterface{    @Override    public String getF() {        // TODO Auto-generated method stub        System.out.println("服务端:获取请求");        return "外卖:米饭";    }    @Override    public String getFByN(String n) {        // TODO Auto-generated method stub        System.out.println("服务端:--"+n+"---获取请求");        return n+",中午吃炒面";    }}

发布代码:

public static void main(String[] args) {        //1、创建cxf服务工厂对象        JaxWsServerFactoryBean factoryBean=new JaxWsServerFactoryBean();        //2、设置要发布的接口类        factoryBean.setServiceClass(FoodInterface.class);        //3、设置接口的实现类对象        factoryBean.setServiceBean(new FoodInterfaceImple());        //4、设置发布路径,访问地址        factoryBean.setAddress("http://127.0.0.1:9797/ws/food");        //5、发布        factoryBean.create();        System.out.println("服务启动……");        //wsdl2java -p org.qf.ws -d G:/src -client http://127.0.0.1:9797/ws/food?wsdl        //验证有没有成功:http://127.0.0.1:9797/ws/food?wsdl    }

2、使用CXF自带工具自动生成客户端代码

将cxf解压好的bin配置到path中在xmd中使用命令进行生成:
wsdl2java -p org.qf.ws -d G:/src -client http://127.0.0.1:9797/ws/food?wsdl
然后到G盘下的src将生成的代码拷贝到客户端项目即可。
参数说明:
-p 也就是package 对应Java中的包
-d 输入目录,生成.java文件会在该目录,会自动添加-p参数配置的包路径
-client 生成客户端测试web service的代码.
-server 生成服务器启动web service的代码.
-impl 生成web service的实现代码.
-ant 生成build.xml文件.
-all 生成上面-client -server -impl -ant 对应的所有文件.

注意如果自动生成报错,请参考:CXF自动生成客户端代码异常jdk1.8

3、开发客户端代码

pom文件
跟服务端的一模一样
将刚刚自动生成的代码拷贝到src下
客户的代码:

    public static void main(String[] args) throws MalformedURLException {        test3();    }    //使用CXF自动生成的Service的子类---推荐    public static void test1(){        //创建自动生成的Servcie类对象        FoodInterfaceService factory=new FoodInterfaceService();        //获取接口的实现类--服务端        FoodInterface fi=factory.getFoodInterfacePort();        System.out.println("客户端:"+fi.getF());        System.out.println("客户端:1---"+fi.getFByN("蚂蚱"));    }    //使用CXF内置对象调用    public static void test2(){        //创建代理工厂对象        JaxWsProxyFactoryBean factoryBean=new JaxWsProxyFactoryBean();        //设置接口的Class对象        factoryBean.setServiceClass(FoodInterface.class);        //设置服务地址        factoryBean.setAddress("http://127.0.0.1:9797/ws/food?wsdl");        //获取接口的实现类        FoodInterface fi=(FoodInterface) factoryBean.create();        System.out.println("客户端:44--->"+fi.getF());    }    //使用Service调用服务    public static void test3() throws MalformedURLException{        //创建wsdl路径        URL url=new URL("http://127.0.0.1:9797/ws/food?wsdl");        QName qn=new QName("http://ws.code404.cn/", "FoodInterfaceService");        Service service=Service.create(url, qn);        FoodInterface fi=service.getPort(FoodInterface.class);        System.out.println("客户端:ooo---->"+fi.getFByN("青蛙"));    }

运行即可。

原创粉丝点击