使用cxf发布Web Service

一、服务端程序:


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>





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值