CXF服务器发布,客户端调用

来源:互联网 发布:php面向对象的封装 编辑:程序博客网 时间:2024/06/06 09:35

1.web service特点及应用场景

这里写图片描述

2.web servicede的CXF框架介绍

这里写图片描述

一个程序:来源,查看原网址
(1)定义Service

Jax-ws的WebService定义是通过注解进行的,我们必须在其WebService类的接口上使用@WebService注解进行标记。

@WebService  public interface HelloWorld {        public String sayHi(String who);      }  

如上,我们把HelloWorld定义为一个WebService,其对应有一个sayHi操作。其对应的实现类如下:

public class HelloWorldImpl implements HelloWorld {     @Override     public String sayHi(String who) {        return"Hi, " + who;     }   }  

(2) 服务端发布Service
对于Jax-ws WebService而言,发布Service有两种方式。
第一种:

public class Server {     private final static String ADDRESS = "http://localhost:8080/test/jaxws/services/HelloWorld";     public static void main(String args[]) {        HelloWorld hw = new HelloWorldImpl();        Endpoint.publish(ADDRESS, hw);     }  }  

第二种:

public class Server {     private final static String ADDRESS = "http://localhost:8080/test/jaxws/services/HelloWorld";     public static void main(String args[]) {        HelloWorld hw = new HelloWorldImpl();        JaxWsServerFactoryBean jwsFactory = new JaxWsServerFactoryBean();        jwsFactory.setAddress(ADDRESS);   //指定WebService的发布地址        jwsFactory.setServiceClass(HelloWorld.class);//WebService对应的类型        jwsFactory.setServiceBean(hw);//WebService对应的实现对象        jwsFactory.create();     }  }  

发布之后我们就可以通过发布地址?wsdl查看WebService的定义了(WSDL是Web Service Description Language的简称,即网络服务描述语言)。通过在浏览器输入http://localhost:8080/test/jaxws/services/HelloWorld?wsdl我们可以看到如下内容:

<wsdl:definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://ws.cxf.hongwei.com/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:ns1="http://schemas.xmlsoap.org/soap/http" name="HelloWorldServiceService" targetNamespace="http://ws.cxf.hongwei.com/"><wsdl:types><xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://ws.cxf.hongwei.com/" elementFormDefault="unqualified" targetNamespace="http://ws.cxf.hongwei.com/" version="1.0"><xs:element name="sayHi" type="tns:sayHi"/><xs:element name="sayHiResponse" type="tns:sayHiResponse"/><xs:complexType name="sayHi"><xs:sequence><xs:element minOccurs="0" name="arg0" type="xs:string"/></xs:sequence></xs:complexType><xs:complexType name="sayHiResponse"><xs:sequence><xs:element minOccurs="0" name="return" type="xs:string"/></xs:sequence></xs:complexType></xs:schema></wsdl:types><wsdl:message name="sayHiResponse"><wsdl:part element="tns:sayHiResponse" name="parameters"></wsdl:part></wsdl:message><wsdl:message name="sayHi"><wsdl:part element="tns:sayHi" 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="HelloWorldServiceServiceSoapBinding" 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="HelloWorldServiceService"><wsdl:port binding="tns:HelloWorldServiceServiceSoapBinding" name="HelloWorldServicePort"><soap:address location="http://localhost:8080/test/jaxws/services/HelloWorld"/></wsdl:port></wsdl:service></wsdl:definitions>

我们可以看到在上面我们的sayHi操作的参数who变成了arg0,这是因为接口在被编译为class文件的时候不能保存参数名,有时候这会影响可读性。如果需要参数名显示的可读性强一些的话,我们可以使用@WebParam来指定,如:

@WebService(serviceName="!@#$%^", name="123456")  public interface HelloWorld {     public String sayHi(@WebParam(name="who") String who);  }  
0 0
原创粉丝点击