使用cxf发布Web Service
2015-01-04 22:46
381 查看
一、服务端程序:
1、接口:
2、实现类:
3、服务发布:
二、通过cxf的命令生成代码:
命令为:wsdl2java,命令参数地址为:http://192.168.0.107:8888/hello?wsdl
三、客户端程序:
四、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://ws/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:ns1="http://schemas.xmlsoap.org/soap/http" name="HelloWSImplService" targetNamespace="http://ws/">
<wsdl:types>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://ws/" elementFormDefault="unqualified" targetNamespace="http://ws/" version="1.0">
<!--
// 用于请求
<sayHello>
<arg0>string</arg0>
</sayHello>
-->
<xs:element name="sayHello" type="tns:sayHello"/>
<!--
// 用于响应
<sayHelloResponse>
<return>string</return>
</sayHelloResponse>
-->
<xs:element name="sayHelloResponse" type="tns:sayHelloResponse"/>
<xs:complexType name="sayHello">
<xs:sequence>
<xs:element minOccurs="0" name="arg0" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="sayHelloResponse">
<xs:sequence>
<xs:element minOccurs="0" name="return" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
</wsdl:types>
<!--
message:用来定义消息的结构
part:指定引用types中定义的标签片段
-->
<wsdl:message name="sayHelloResponse">
<wsdl:part element="tns:sayHelloResponse" name="parameters">
</wsdl:part>
</wsdl:message>
<wsdl:message name="sayHello">
<wsdl:part element="tns:sayHello" name="parameters">
</wsdl:part>
</wsdl:message>
<!--
portType: 用来定义服务器端的SEI
operation:用来指定SEI中的处理请求方法
input:指定客户端应用传过来的数据,会引用上面的定义的<message>
output:指定服务器端返回给客户端的数据,会引用上面定义的<message>
-->
<wsdl:portType name="HelloWS">
<wsdl:operation name="sayHello">
<wsdl:input message="tns:sayHello" name="sayHello">
</wsdl:input>
<wsdl:output message="tns:sayHelloResponse" name="sayHelloResponse">
</wsdl:output>
</wsdl:operation>
</wsdl:portType>
<!--
binding:用于定义SEI的实现类
type属性:引用上面的<portType>
<soap:binding style="document"/>:绑定的数据是一个document(xml)
operation:用来定义实现的方法
<soap:operation style="document"/> 传输的是document(xml)
input:指定客户端应用传过来的数据,会引用上面的定义的<message>
<soap:body use="literal"/>:文本数据
output:指定服务器端返回给客户端的数据,会引用上面定义的<message>
<soap:body use="literal"/>:文本数据
-->
<wsdl:binding name="HelloWSImplServiceSoapBinding" type="tns:HelloWS">
<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>
<!--
sevice:服务端的一个service的容器
name属性:它用来指定客户端容器类
port:用来指定一个服务器端处理请求的入口(SEI的实现)
binding属性:引用上面定义的<binding>
address:当前webservice的请求地址
-->
<wsdl:service name="HelloWSImplService">
<wsdl:port binding="tns:HelloWSImplServiceSoapBinding" name="HelloWSImplPort">
<soap:address location="http://192.168.0.107:8888/hello"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
1、接口:
package ws; import javax.jws.WebMethod; import javax.jws.WebService; /** * * SEI * */ @WebService public interface HelloWS { @WebMethod public String sayHello(String name); }
2、实现类:
package ws; import javax.jws.WebService; /* * SEA实现 */ @WebService public class HelloWSImpl implements HelloWS { @Override public String sayHello(String name) { System.out.println("sayHello"+name); return "Hello"+name; } }
3、服务发布:
package ws; import javax.xml.ws.Endpoint; /* * 发布web service */ public class HelloService { public static void main(String[] args) { String url = "http://192.168.0.107:8888/hello"; Endpoint.publish(url, new HelloWSImpl()); System.out.println("发布成功!"); } }
二、通过cxf的命令生成代码:
命令为:wsdl2java,命令参数地址为:http://192.168.0.107:8888/hello?wsdl
三、客户端程序:
package client; import ws.HelloWS; import ws.HelloWSImplService; public class Test { public static void main(String[] args) { HelloWSImplService hs = new HelloWSImplService(); HelloWS helloWS = hs.getHelloWSImplPort(); helloWS.sayHello("Hello"); } }
四、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://ws/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:ns1="http://schemas.xmlsoap.org/soap/http" name="HelloWSImplService" targetNamespace="http://ws/">
<wsdl:types>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://ws/" elementFormDefault="unqualified" targetNamespace="http://ws/" version="1.0">
<!--
// 用于请求
<sayHello>
<arg0>string</arg0>
</sayHello>
-->
<xs:element name="sayHello" type="tns:sayHello"/>
<!--
// 用于响应
<sayHelloResponse>
<return>string</return>
</sayHelloResponse>
-->
<xs:element name="sayHelloResponse" type="tns:sayHelloResponse"/>
<xs:complexType name="sayHello">
<xs:sequence>
<xs:element minOccurs="0" name="arg0" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="sayHelloResponse">
<xs:sequence>
<xs:element minOccurs="0" name="return" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
</wsdl:types>
<!--
message:用来定义消息的结构
part:指定引用types中定义的标签片段
-->
<wsdl:message name="sayHelloResponse">
<wsdl:part element="tns:sayHelloResponse" name="parameters">
</wsdl:part>
</wsdl:message>
<wsdl:message name="sayHello">
<wsdl:part element="tns:sayHello" name="parameters">
</wsdl:part>
</wsdl:message>
<!--
portType: 用来定义服务器端的SEI
operation:用来指定SEI中的处理请求方法
input:指定客户端应用传过来的数据,会引用上面的定义的<message>
output:指定服务器端返回给客户端的数据,会引用上面定义的<message>
-->
<wsdl:portType name="HelloWS">
<wsdl:operation name="sayHello">
<wsdl:input message="tns:sayHello" name="sayHello">
</wsdl:input>
<wsdl:output message="tns:sayHelloResponse" name="sayHelloResponse">
</wsdl:output>
</wsdl:operation>
</wsdl:portType>
<!--
binding:用于定义SEI的实现类
type属性:引用上面的<portType>
<soap:binding style="document"/>:绑定的数据是一个document(xml)
operation:用来定义实现的方法
<soap:operation style="document"/> 传输的是document(xml)
input:指定客户端应用传过来的数据,会引用上面的定义的<message>
<soap:body use="literal"/>:文本数据
output:指定服务器端返回给客户端的数据,会引用上面定义的<message>
<soap:body use="literal"/>:文本数据
-->
<wsdl:binding name="HelloWSImplServiceSoapBinding" type="tns:HelloWS">
<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>
<!--
sevice:服务端的一个service的容器
name属性:它用来指定客户端容器类
port:用来指定一个服务器端处理请求的入口(SEI的实现)
binding属性:引用上面定义的<binding>
address:当前webservice的请求地址
-->
<wsdl:service name="HelloWSImplService">
<wsdl:port binding="tns:HelloWSImplServiceSoapBinding" name="HelloWSImplPort">
<soap:address location="http://192.168.0.107:8888/hello"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
相关文章推荐
- java 使用CXF发布web service
- 使用.net如何发布web service
- 使用WSDL发布WebService(第一部分)——Web Service 和WSDL简介
- 使用WSDL发布WebService(第一部分)——Web Service 和WSDL简介
- 使用CXF框架发布webservice
- Apache CXF实战之九 发布使用SSL的Web Service .
- 使用WSDL发布WebService(第一部分)——Web Service 和WSDL简介
- 使用cxf 发布 jax-rs 风格webservice 。并客户端测试。
- 在SpringSide 2.0中使用CXF提供Web Service服务
- CXF的发布和使用简介
- Web项目中使用Spring整合CXF发布Web Services
- 使用.net如何发布web service
- CXF之使用CXF API 发布服务与进行客户端调用
- CXF之使用jaxws API 发布服务与进行客户端调用
- 使用weblogic11g+XFire发布web service服务
- 使用Java调用以WSDL形式发布的web service
- Web service的使用与发布
- CXF之使用工厂方法(java代码)发布服务与进行客户端调用
- Web应用中使用CXF不基于Spring的WebService发布
- 使用CXF为Web Service添加拦截器,自定义拦截器