WebService 简单例子,使用CXF框架
2012-06-11 10:08
579 查看
下载CXF:CXF下载
在lib下面有12jar包需要添加,
先定义接口:
实现类:
启动服务器:
然后在浏览器中查看注册的webService:
url:http://127.0.0.1:9090/service?wsdl
客户端:
From Service response :hahh
需要增加上面jar到系统类加载路径
动态客户端:
java提供:wsdl2java工具
在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?wsdlC++/C# 提供wsdl.exe
相关文章推荐
- 使用CXF框架实现webservice的一个简单例子
- 使用 CXF 做 webservice 简单例子
- 使用 CXF 做 webservice 简单例子
- 使用 CXF 做 webservice 简单例子 (转载)
- 使用 CXF 做 webservice 简单例子
- 使用 CXF 做 webservice 简单例子
- 使用 CXF 做 webservice 简单例子
- 【转】使用 CXF 做 webservice 简单例子
- 使用 CXF 做 webservice 简单例子
- 使用 CXF 做 webservice 简单例子(转载)
- 使用CXF做 webservice 简单例子
- 使用 CXF 做 webservice 简单例子
- 一个简单的cxf框架调用webservice接口天气预报例子
- 使用 CXF 做 webservice 简单例子
- 使用CXF框架开发一个简单的WebService
- 使用 CXF 做 webservice 简单例子
- 使用 CXF 做 webservice 简单例子
- 使用 CXF 做 webservice 简单例子
- 使用 CXF 做 webservice 简单例子
- 使用 CXF 做 webservice 简单例子