WebService - CXF开发Server和Client(main方法测试)

本文介绍如何使用JAX-WS与CXF框架实现简单的WebService服务端发布及客户端调用过程。从配置环境变量开始,逐步演示了Service接口定义、实现类编写、服务发布及客户端测试等步骤。

本篇简要记录使用jdk生成Stub进行service与client的测试,未与spring结合且未发布到tomcat容器,Server和Client都是使用的main方法。

篇末有第二种方式(客户端代理工厂)进行客户端开发示例。

这里使用的是CXF,引入jar永远是不可少的(自己下载引入)。

这里写图片描述


【1】配置系统环境变量

如果使用jetty生成Stub来编写客户端项目,需要进行如下配置:

这里写图片描述


【2】编辑Service并进行发布

service的编写使用JAX-WS,直接使用注解进行声明,简单便捷。Service项目不需要额外的jar,Client项目需要博文头部提到的jar。

① 编写service接口

package com.web.service;

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

//这里使用了注解,声明为webservice接口
@WebService
public interface ServiceServer {
    //这里使用了注解,声明方法
    @WebMethod
    public String sayHello(String name);

}

② 编写其实现类

package com.web.service.impl;

import javax.jws.WebService;

import com.web.service.ServiceServer;

//这里使用了注解
@WebService
public class ServiceServerImpl implements ServiceServer{

    @Override
    public String sayHello(String name) {
        System.out.println("server sayHello "+name);
        return "hello"+name;
    }

}

③ 使用main方法进行发布

package com.web.service.impl;

import java.util.Date;

import javax.xml.ws.Endpoint;

public class ServerTest {

    public static void main(String[] args) {

        String address = "http://192.168.2.225:8989/ServiceServerImpl";
        Endpoint.publish(address , new ServiceServerImpl());
        System.out.println("webservice 发布成功!"+new Date());
    }
}

这里写图片描述


【3】根据address,查看并保存wsdl文件

① 浏览器中输入地址,查看wsdl

http://192.168.2.225:8989/ServiceServerImpl?wsdl

这里写图片描述


将其保存为test.wsdl放入客户端项目下(新建一个客户端项目)

这里写图片描述

<?xml version='1.0' encoding='UTF-8'?><wsdl:definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://impl.service.web.com/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:ns2="http://schemas.xmlsoap.org/soap/http" xmlns:ns1="http://service.web.com/" name="ServiceServerImplService" targetNamespace="http://impl.service.web.com/">
  <wsdl:import location="http://192.168.2.225:8989/ServiceServerImpl?wsdl=ServiceServer.wsdl" namespace="http://service.web.com/">
    </wsdl:import>
  <wsdl:binding name="ServiceServerImplServiceSoapBinding" type="ns1:ServiceServer">
    <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
    <wsdl:operation name="sayHello">
      <soap:operation soapAction="" style="document"/>
      <wsdl:input name="sayHello">
        <soap:body use="literal"/>
      </wsdl:input>
      <wsdl:output name="sayHelloResponse">
        <soap:body use="literal"/>
      </wsdl:output>
    </wsdl:operation>
  </wsdl:binding>
  <wsdl:service name="ServiceServerImplService">
    <wsdl:port binding="tns:ServiceServerImplServiceSoapBinding" name="ServiceServerImplPort">
      <soap:address location="http://192.168.2.225:8989/ServiceServerImpl"/>
    </wsdl:port>
  </wsdl:service>
</wsdl:definitions>

【4】根据wsdl生成Stub,进行客户端测试

① DOS进入客户端项目src路径

这里写图片描述


② 根据项目下的wsdl生成Stub

wsimport -keey url

这里写图片描述


③ 刷新项目,可以看到src下生成了service和impl

这里写图片描述


另外,还可以直接根据wsdl的url地址生成Stub,不用再保存wsdl具体文件到项目下。

这里写图片描述


上面两种方式都是使用jdk生成Stub,还可以使用如下命令使用jetty进行生成:

wsdl2java url

④ 编写main方法,进行客户端调用

package com.web.client;

import com.web.service.impl.ServiceServer;
import com.web.service.impl.ServiceServerImplService;

public class Client {

    public static void main(String[] args) {
        ServiceServerImplService factory = new ServiceServerImplService();
        ServiceServer serviceServerImplPort = factory.getServiceServerImplPort();
        System.out.println(serviceServerImplPort.getClass());
        String result = serviceServerImplPort.sayHello("Tom");
        System.out.println("Client "+result);
    }
}

客户端输出结果如下:

这里写图片描述


服务端输出结果如下:

这里写图片描述


项目源码与jar下载:http://download.youkuaiyun.com/download/j080624/10051042


【Tips】可以采用JaxWsProxyFactoryBean 如下方式:

package com.web.test;

import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;

import com.web.service.ServiceServer;

public class ClientTest2 {

    public static void main(String[] args) {
         //创建WebService客户端代理工厂
          JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
          //注册WebService接口
          factory.setServiceClass(ServiceServer.class);
          //设置WebService地址
          factory.setAddress("http://192.168.2.225:8888/redis/hello");
          ServiceServer serverService = (ServiceServer)factory.create();
          System.out.println("invoke webservice...");
          String sayHello = serverService.sayHello("Lucy");
          System.out.println("message context is:"+sayHello);   
    }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

流烟默

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值