Web Service学习笔记

本文详细介绍如何使用Apache CXF搭建WebService服务端及客户端,包括接口定义、实现、配置等步骤,并演示了客户端如何调用服务端提供的服务。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Web Service有以下的优越性:

1)平台无关。不管你使用什么平台,都可以使用Web service。

2)编程语言无关。只要遵守相关协议,就可以使用任意编程语言,向其他网站要求Web service。这大大增加了web service的适用性,降低了对程序员的要求。

3)对于Webservice提供者来说,部署、升级和维护Web service都非常单纯,不需要考虑客户端兼容问题,而且一次性就能完成。

4)对于Webservice使用者来说,可以轻易实现多种数据、多种服务的聚合(mashup),因此能够做出一些以前根本无法想像的事情。

事先准备:
下载 Apache CXF : http://cxf.apache.org/download.html
在工程项目导入下载后lib目录下的cxf的jar包。
一、服务器
1.1 . 编写 HelloWorld 接口,并将其标注成 WebService 的标准 java 接口

import javax.jws.WebService;

@WebService
public interface HelloWorld {

    public String sayHi(String message);

}

1.2.实现该接口用webService注解加入该接口的endpointInterface ,serviceName,targetNamespace。

import javax.jws.WebService;

import com.wang.chouqu.cxf.HelloWorld;

@WebService(
        endpointInterface = "com.wang.chouqu.cxf.HelloWorld", 
        serviceName="helloWorld", 
        targetNamespace="http://cxf.chouqu.wang.com/")
public class HelloWorldImpl implements HelloWorld {

    public String sayHi(String massage) {
        // TODO Auto-generated method stub
        return "Hi "+massage+" .";
    }

}

1.3.applicationContext.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:p="http://www.springframework.org/schema/p"
      xmlns:tx="http://www.springframework.org/schema/tx"
      xmlns:aop="http://www.springframework.org/schema/aop"
      xmlns:jaxws="http://cxf.apache.org/jaxws"
      xmlns:cxf="http://cxf.apache.org/core"
      xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
      http://www.springframework.org/schema/tx
      http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
      http://www.springframework.org/schema/aop
      http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
      http://cxf.apache.org/jaxws    
      http://cxf.apache.org/schemas/jaxws.xsd">

    <!-- 配置请参考官网: http://cxf.apache.org/docs/jax-rs-and-jax-ws.html -->
    <import resource="classpath:META-INF/cxf/cxf.xml" />
    <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
    <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />

    <bean id="helloWorld" class="com.wang.chouqu.cxf.imp.HelloWorldImpl" />

    <!-- JAX-WS -->
    <!-- implementor 指定 WebService 实现类, address 指定访问地址 -->
    <jaxws:endpoint implementor="#helloWorld" address="/helloworld" publish="true" />

</beans>

1.4、web配置,在项目下的web.xml中加入下面的代码

  <servlet>
    <servlet-name>CXFServlet</servlet-name>
    <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>CXFServlet</servlet-name>
    <url-pattern>/webservice/*</url-pattern>
  </servlet-mapping>

到这里服务端已经设置开发完,tomcat启动项目,访问 : http://localhost:8080/wen/webservice/helloworld?wsdl
其中wen对应自己的工程名。
如果看到WSDL文件就说明服务器这边正常了

This XML file does not appear to have any style information associated with it. The document tree is shown below.
<wsdl:definitions xmlns:ns1="http://cxf.apache.org/bindings/xformat" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://cxf.chouqu.wang.com/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="helloWorld" targetNamespace="http://cxf.chouqu.wang.com/">
<wsdl:types>
<xsd:schema xmlns:tns="http://cxf.chouqu.wang.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" attributeFormDefault="unqualified" elementFormDefault="unqualified" targetNamespace="http://cxf.chouqu.wang.com/">
<xsd:element name="sayHi" type="tns:sayHi"/>
<xsd:complexType name="sayHi">
<xsd:sequence>
<xsd:element minOccurs="0" name="arg0" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
<xsd:element name="sayHiResponse" type="tns:sayHiResponse"/>
<xsd:complexType name="sayHiResponse">
<xsd:sequence>
<xsd:element minOccurs="0" name="return" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
</wsdl:types>
<wsdl:message name="sayHi">
<wsdl:part element="tns:sayHi" name="parameters"></wsdl:part>
</wsdl:message>
<wsdl:message name="sayHiResponse">
<wsdl:part element="tns:sayHiResponse" name="parameters"></wsdl:part>
</wsdl:message>
<wsdl:portType name="HelloWorld">
<wsdl:operation name="sayHi">
<wsdl:input message="tns:sayHi" name="sayHi"></wsdl:input>
<wsdl:output message="tns:sayHiResponse" name="sayHiResponse"></wsdl:output>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="helloWorldSoapBinding" type="tns:HelloWorld">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="sayHi">
<soap:operation soapAction="" style="document"/>
<wsdl:input name="sayHi">
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output name="sayHiResponse">
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="helloWorld">
<wsdl:port binding="tns:helloWorldSoapBinding" name="HelloWorldImplPort">
<soap:address location="http://localhost:8070/wen/webservice/helloworld"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>

二、接下来是客户端的开发
2.1 在客户端的项目下新建一个类,同样是要@WebService注解标识

import javax.jws.WebService;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;

@WebService
public class HelloWorld {
    public static void main (String srgs []){
        JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance(); 
        org.apache.cxf.endpoint.Client client =  dcf.createClient("http://127.0.0.1:8070/wen/webservice/helloworld?wsdl"); 
        //sayHello 为接口中定义的方法名称   wang传递的参数   返回一个Object数组 
        Object[] objects;
        try {
            objects = client.invoke("sayHi","wang");
            //输出调用结果 
            System.out.println(objects[0].toString());
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

2.2执行main方法
这里写图片描述

到这里我们的客户端就成功调用服务器的sayHi方法,传入入‘wang’参数,成功打印出Hi wang .

在具体使用过程中,根据wsdlAddress地址创建CXF动态客户端,提供个工具类。

    public static Client getJaxWsDynamicClient(String wsdlAddress) {
        // 每个wsdl创建一个Client,大部分应用场景可以保证线程安全,
        // 可参照cxf官网http://cxf.apache.org/faq.html#FAQ-AreJAX-WSclientproxiesthreadsafe?
        Client client = wsdlToClient.get(wsdlAddress);
        if (client == null) {
            lock.lock();
            try {
                client = wsdlToClient.get(wsdlAddress);
                if (client == null) {
                    JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory
                            .newInstance();
                    client = dcf.createClient(wsdlAddress);
                    client.getRequestContext().put(
                            "thread.local.request.context", "true");
                    client.getOutInterceptors()
                            .add(new org.apache.cxf.interceptor.LoggingOutInterceptor());
                    client.getInInterceptors()
                            .add(new org.apache.cxf.interceptor.LoggingInInterceptor());
                    wsdlToClient.put(wsdlAddress, client);
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        }
        return client;
    }
}

如需加深了解可以查看

http://my.oschina.net/u/246522/blog/151160#OSC_h2_15

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值