CXF是一个开源的WebService框架,首先通过一个简单的示例学习CXF的使用
@WebService 注解有多个属性,通过对这些属性的赋值去学习各个属性的作用。
name: 指定服务接口的名称。此属性映射到wsdl:portType元素的属性名称,默认值是实现类的类名。
targetNamespace 指定服务的命名空间,如果该属性未指定则命名空间来自包名。
serviceName :值发布的服务名称,次属性映射到wsdl:service元素的名称。默认值是实现类的名称实现类的名称+Service
portName:指定服务发布点的名称。此属性映射到wsdl:port
wsdlLocation:指定WSDL协议保存的URI。该属性默认值为服务部署的URI
endpointInterface: 指定实现类中服务接口的全名。
/**
* CXF 第一个示例
* @author zhangwei
* @version $Id: HelloWord.java, v 0.1 2014年9月27日 上午8:28:08 zhangwei Exp $
*/
public interface HelloWord {
/**
* 示例方法,打印“Hello,Word”
*/
public void sayHello();
}
/**
*@SOAPBinding(style = SOAPBinding.Style.RPC)
* @author zhangwei_david
* @version $Id: HelloWordImpl.java, v 0.1 2014年9月27日 上午8:32:20 zhangwei_david Exp $
*/
@WebService
public class HelloWordImpl implements HelloWord {
/**
* @see com.cathy.demo.cxf.HelloWord#sayHello()
*/
@WebMethod
public void sayHello() {
LoggerUtils.info("Hello,Word");
}
}
/**
*
* @author zhangwei_david
* @version $Id: SimpleServer.java, v 0.1 2014年9月27日 上午8:36:45 zhangwei_david Exp $
*/
public class SimpleServer {
public SimpleServer() {
init();
}
private void init() {
LoggerUtils.info("启动服务。。。");
HelloWord helloWord = new HelloWordImpl();
String address = "http://localhost:9000/HelloWord";
Endpoint.publish(address, helloWord);
}
public static void main(String[] args) throws Exception {
new SimpleServer();
LoggerUtils.info("服务就绪。。。。");
TimeUnit.MINUTES.sleep(10);
LoggerUtils.info("关闭服务。。。。");
System.exit(0);
}
}
浏览器中输入:http://localhost:9000/HelloWord?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://impl.cxf.demo.cathy.com/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:ns1="http://schemas.xmlsoap.org/soap/http" targetNamespace="http://impl.cxf.demo.cathy.com/" name="HelloWordImplService">
-<wsdl:types>
-<xs:schema xmlns:tns="http://impl.cxf.demo.cathy.com/" targetNamespace="http://impl.cxf.demo.cathy.com/" xmlns:xs="http://www.w3.org/2001/XMLSchema" version="1.0" elementFormDefault="unqualified">
<xs:element name="sayHello" type="tns:sayHello"/>
<xs:element name="sayHelloResponse" type="tns:sayHelloResponse"/>
-<xs:complexType name="sayHello">
<xs:sequence/>
</xs:complexType>
-<xs:complexType name="sayHelloResponse">
<xs:sequence/>
</xs:complexType>
</xs:schema>
</wsdl:types>
-<wsdl:message name="sayHelloResponse">
<wsdl:part name="parameters" element="tns:sayHelloResponse"> </wsdl:part>
</wsdl:message>
-<wsdl:message name="sayHello">
<wsdl:part name="parameters" element="tns:sayHello"> </wsdl:part>
</wsdl:message>
-<wsdl:portType name="HelloWordImpl">
-<wsdl:operation name="sayHello">
<wsdl:input name="sayHello" message="tns:sayHello"> </wsdl:input>
<wsdl:output name="sayHelloResponse" message="tns:sayHelloResponse"> </wsdl:output>
</wsdl:operation>
</wsdl:portType>
-<wsdl:binding name="HelloWordImplServiceSoapBinding" type="tns:HelloWordImpl">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
-<wsdl:operation name="sayHello">
<soap:operation style="document" soapAction=""/>
-<wsdl:input name="sayHello">
<soap:body use="literal"/>
</wsdl:input>
-<wsdl:output name="sayHelloResponse">
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
-<wsdl:service name="HelloWordImplService">
-<wsdl:port name="HelloWordImplPort" binding="tns:HelloWordImplServiceSoapBinding">
<soap:address location="http://localhost:9000/HelloWord"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
将实现类修改为:
/**
*
* @author zhangwei_david
* @version $Id: HelloWordImpl.java, v 0.1 2014年9月27日 上午8:32:20 zhangwei_david Exp $
*/
@WebService(name = "HelloWord", portName = "HelloPort", serviceName = "HelloWordService", targetNamespace = "http://com.cathy.demo")
public class HelloWordImpl implements HelloWord {
/**
* @see com.cathy.demo.cxf.HelloWord#sayHello()
*/
@WebMethod
public void sayHello() {
LoggerUtils.info("Hello,Word");
}
}
<?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://com.cathy.demo" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:ns1="http://schemas.xmlsoap.org/soap/http" targetNamespace="http://com.cathy.demo" name="HelloWordService">
-<wsdl:types>
-<xs:schema xmlns:tns="http://com.cathy.demo" targetNamespace="http://com.cathy.demo" xmlns:xs="http://www.w3.org/2001/XMLSchema" version="1.0" elementFormDefault="unqualified">
<xs:element name="sayHello" type="tns:sayHello"/>
<xs:element name="sayHelloResponse" type="tns:sayHelloResponse"/>
-<xs:complexType name="sayHello">
<xs:sequence/>
</xs:complexType>
-<xs:complexType name="sayHelloResponse">
<xs:sequence/>
</xs:complexType>
</xs:schema>
</wsdl:types>
-<wsdl:message name="sayHelloResponse">
<wsdl:part name="parameters" element="tns:sayHelloResponse"> </wsdl:part>
</wsdl:message>
-<wsdl:message name="sayHello">
<wsdl:part name="parameters" element="tns:sayHello"> </wsdl:part>
</wsdl:message>
-<wsdl:portType name="HelloWord">
-<wsdl:operation name="sayHello">
<wsdl:input name="sayHello" message="tns:sayHello"> </wsdl:input>
<wsdl:output name="sayHelloResponse" message="tns:sayHelloResponse"> </wsdl:output>
</wsdl:operation>
</wsdl:portType>
-<wsdl:binding name="HelloWordServiceSoapBinding" type="tns:HelloWord">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
-<wsdl:operation name="sayHello">
<soap:operation style="document" soapAction=""/>
-<wsdl:input name="sayHello">
<soap:body use="literal"/>
</wsdl:input>
-<wsdl:output name="sayHelloResponse">
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
-<wsdl:service name="HelloWordService">
-<wsdl:port name="HelloPort" binding="tns:HelloWordServiceSoapBinding">
<soap:address location="http://localhost:9000/HelloWord"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
可以发现上面的SOAP的方式都一直地document 如果想改为RPC又该如何呢?
**
*
* @author zhangwei_david
* @version $Id: HelloWordImpl.java, v 0.1 2014年9月27日 上午8:32:20 zhangwei_david Exp $
*/
@SOAPBinding(style = SOAPBinding.Style.RPC)
@WebService
public class HelloWordImpl implements HelloWord {
/**
* @see com.cathy.demo.cxf.HelloWord#sayHello()
*/
@WebMethod
public void sayHello() {
LoggerUtils.info("Hello,Word");
}
}
结果可以看到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://impl.cxf.demo.cathy.com/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:ns1="http://schemas.xmlsoap.org/soap/http" targetNamespace="http://impl.cxf.demo.cathy.com/" name="HelloWordImplService">
<wsdl:message name="sayHelloResponse"> </wsdl:message>
<wsdl:message name="sayHello"> </wsdl:message>
-<wsdl:portType name="HelloWordImpl">
-<wsdl:operation name="sayHello">
<wsdl:input name="sayHello" message="tns:sayHello"> </wsdl:input>
<wsdl:output name="sayHelloResponse" message="tns:sayHelloResponse"> </wsdl:output>
</wsdl:operation>
</wsdl:portType>
-<wsdl:binding name="HelloWordImplServiceSoapBinding" type="tns:HelloWordImpl">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="rpc"/>
-<wsdl:operation name="sayHello">
<soap:operation style="rpc" soapAction=""/>
-<wsdl:input name="sayHello">
<soap:body namespace="http://impl.cxf.demo.cathy.com/" use="literal"/>
</wsdl:input>
-<wsdl:output name="sayHelloResponse">
<soap:body namespace="http://impl.cxf.demo.cathy.com/" use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
-<wsdl:service name="HelloWordImplService">
-<wsdl:port name="HelloWordImplPort" binding="tns:HelloWordImplServiceSoapBinding">
<soap:address location="http://localhost:9000/HelloWord"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>