WebService 简单例子,使用CXF框架

下载CXF:CXF下载

在lib下面有12jar包需要添加,

 

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
先定义接口:
import java.util.List;

import javax.jws.WebService;

@WebService
public interface FirstWebService {
	String say(String hua);

	String sayToUser(Person p);

	List<Person> getPersons();
 
}

实现类:
import java.util.List;

import javax.jws.WebService;

@WebService(endpointInterface = "com.webservice.FirstWebService", serviceName = "FirstWebService")
public class FirstWebServiceImpl implements FirstWebService {

	@Override
	public String say(String hua) {
		System.out.println("say:" + hua);
		return hua;
	}

	@Override
	public String sayToUser(Person p) {
		System.out.println(p + " by say:");
		return p.getName();
	}

	@Override
	public List<Person> getPersons() {

		return null;
	}

}

启动服务器:
import javax.xml.ws.Endpoint;

import com.webservice.FirstWebService;
import com.webservice.FirstWebServiceImpl;

public class Boot {
	public static void main(String args[]) {
		FirstWebService service = new FirstWebServiceImpl();
		String address = "http://127.0.0.1:9090/service";
		Endpoint.publish(address, service);
	}
}
2012-6-11 10:22:36 org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromClass
信息: Creating Service {http://webservice.com/}FirstWebService from class com.webservice.FirstWebService
2012-6-11 10:22:37 org.apache.cxf.endpoint.ServerImpl initDestination
信息: Setting the server's publish address to be http://127.0.0.1:9090/service
2012-06-11 10:22:37.781:INFO:oejs.Server:jetty-7.5.4.v20111024
2012-06-11 10:22:37.855:INFO:oejs.AbstractConnector:Started SelectChannelConnector@127.0.0.1:9090 STARTING
2012-06-11 10:22:37.913:INFO:oejsh.ContextHandler:started o.e.j.s.h.ContextHandler{,null}

然后在浏览器中查看注册的webService:

url:http://127.0.0.1:9090/service?wsdl

wsdl:definitions xmlns:ns1="http://schemas.xmlsoap.org/soap/http" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://webservice.com/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="FirstWebService" targetNamespace="http://webservice.com/">
<wsdl:types>
<xs:schema xmlns:tns="http://webservice.com/" xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="unqualified" targetNamespace="http://webservice.com/" version="1.0">
<xs:element name="getPersons" type="tns:getPersons"/>
<xs:element name="getPersonsResponse" type="tns:getPersonsResponse"/>
<xs:element name="say" type="tns:say"/>
<xs:element name="sayResponse" type="tns:sayResponse"/>
<xs:element name="sayToUser" type="tns:sayToUser"/>
<xs:element name="sayToUserResponse" type="tns:sayToUserResponse"/>
<xs:complexType name="sayToUser">
<xs:sequence>
<xs:element minOccurs="0" name="arg0" type="tns:person"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="person">
<xs:sequence>
<xs:element name="age" type="xs:int"/>
<xs:element maxOccurs="unbounded" minOccurs="0" name="friends" nillable="true" type="tns:person"/>
<xs:element name="id" type="xs:int"/>
<xs:element minOccurs="0" name="name" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="sayToUserResponse">
<xs:sequence>
<xs:element minOccurs="0" name="return" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="say">
<xs:sequence>
<xs:element minOccurs="0" name="arg0" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="sayResponse">
<xs:sequence>
<xs:element minOccurs="0" name="return" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="getPersons">
<xs:sequence/>
</xs:complexType>
<xs:complexType name="getPersonsResponse">
<xs:sequence>
<xs:element maxOccurs="unbounded" minOccurs="0" name="return" type="tns:person"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
</wsdl:types>
<wsdl:message name="say">
<wsdl:part element="tns:say" name="parameters"></wsdl:part>
</wsdl:message>
<wsdl:message name="getPersonsResponse">
<wsdl:part element="tns:getPersonsResponse" name="parameters"></wsdl:part>
</wsdl:message>
<wsdl:message name="sayResponse">
<wsdl:part element="tns:sayResponse" name="parameters"></wsdl:part>
</wsdl:message>
<wsdl:message name="getPersons">
<wsdl:part element="tns:getPersons" name="parameters"></wsdl:part>
</wsdl:message>
<wsdl:message name="sayToUserResponse">
<wsdl:part element="tns:sayToUserResponse" name="parameters"></wsdl:part>
</wsdl:message>
<wsdl:message name="sayToUser">
<wsdl:part element="tns:sayToUser" name="parameters"></wsdl:part>
</wsdl:message>
<wsdl:portType name="FirstWebService">
<wsdl:operation name="sayToUser">
<wsdl:input message="tns:sayToUser" name="sayToUser"></wsdl:input>
<wsdl:output message="tns:sayToUserResponse" name="sayToUserResponse"></wsdl:output>
</wsdl:operation>
<wsdl:operation name="say">
<wsdl:input message="tns:say" name="say"></wsdl:input>
<wsdl:output message="tns:sayResponse" name="sayResponse"></wsdl:output>
</wsdl:operation>
<wsdl:operation name="getPersons">
<wsdl:input message="tns:getPersons" name="getPersons"></wsdl:input>
<wsdl:output message="tns:getPersonsResponse" name="getPersonsResponse"></wsdl:output>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="FirstWebServiceSoapBinding" type="tns:FirstWebService">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="sayToUser">
<soap:operation soapAction="" style="document"/>
<wsdl:input name="sayToUser">
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output name="sayToUserResponse">
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="getPersons">
<soap:operation soapAction="" style="document"/>
<wsdl:input name="getPersons">
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output name="getPersonsResponse">
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="say">
<soap:operation soapAction="" style="document"/>
<wsdl:input name="say">
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output name="sayResponse">
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="FirstWebService">
<wsdl:port binding="tns:FirstWebServiceSoapBinding" name="FirstWebServiceImplPort">
<soap:address location="http://127.0.0.1:9090/service"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>


 


客户端:
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;

import com.webservice.FirstWebService;

public class Boot {
	/**
	 * @param args
	 */
	public static void main(String[] args) {

		JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
		factory.setServiceClass(FirstWebService.class);
		factory.setAddress("http://127.0.0.1:9090/service");
		FirstWebService client = (FirstWebService) factory.create();
		System.out.println("From Service response :" + client.say("hahh"));
		

	}
}

 

From Service response :hahh


 需要增加上面jar到系统类加载路径
动态客户端:
java提供:wsdl2java工具
wsdl2java -frontent javaxs21 http://127.0.0.1:9090/service?wsdl 
C++/C# 提供wsdl.exe
 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值