之前写的 http://renxiangzyq.iteye.com/blog/1021112 集成spring axis2 的方法太过复杂,切为了达到需求要对生成的代码做多出修改,极不方便, 网上看了一些文章,发现其实集成axis2 spring很简单
假设你已经有了一个可以用的spring项目
你有一个类要发布成webservice
public class MyService {
public Person serviceA(String name, Integer age) {
Person person = new Person();
person.setAge(age);
person.setName(name);
City city = new City();
city.setName("shanghai");
city.setZone("east");
person.setCity(city);
return person;
}
}
1. 下载axsi2.war 这个在 axis2官网有下,把他WEB-INF目录下的conf, lib, modules, services目录都copy到你的WEB-INF下, 其实lib里面的jar应该不用全copy的,有时间可以去筛选
2. 在你的web.xml加入以下
<servlet>
<servlet-name>AxisServlet</servlet-name>
<servlet-class>org.apache.axis2.transport.http.AxisServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>AxisServlet</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
3. 在你的application-context.xml里加入
<bean id="applicationContext"
class="org.apache.axis2.extensions.spring.receivers.ApplicationContextHolder" />
<bean id="springAwareService" class="com.axisspring.service.MyService">
</bean>
4. 在你的WEB-INF/services下, 建立 ./test/META-INF/services.xml, 里面的内容为
<?xml version="1.0" encoding="UTF-8"?>
<service name="testService">
<description>simple spring example</description>
<parameter name="ServiceObjectSupplier">
org.apache.axis2.extensions.spring.receivers.SpringAppContextAwareObjectSupplier
</parameter>
<parameter name="SpringBeanName">springAwareService
</parameter>
<messageReceivers>
<messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-only"
class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver" />
<messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out"
class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
</messageReceivers>
</service>
然后就是部署了, 因为我在web.xml里面拦截的是/services/*, 在services.xml里面配的是testService,所以我的webservice地址是
http://localhost:8080/axisspring/services/testService?wsdl,
生成的wsdl如下
<?xml version="1.0" encoding="UTF-8"?> <wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:ns1="http://org.apache.axis2/xsd" xmlns:ns="http://service.axisspring.com" xmlns:wsaw="http://www.w3.org/2006/05/addressing/wsdl" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:ax21="http://service.axisspring.com/xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" targetNamespace="http://service.axisspring.com"> <wsdl:documentation>testService</wsdl:documentation> <wsdl:types> <xs:schema attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="http://service.axisspring.com/xsd"> <xs:complexType name="Person"> <xs:sequence> <xs:element minOccurs="0" name="age" type="xs:int"/> <xs:element minOccurs="0" name="city" nillable="true" type="ax21:City"/> <xs:element minOccurs="0" name="name" nillable="true" type="xs:string"/> </xs:sequence> </xs:complexType> <xs:complexType name="City"> <xs:sequence> <xs:element minOccurs="0" name="name" nillable="true" type="xs:string"/> <xs:element minOccurs="0" name="zone" nillable="true" type="xs:string"/> </xs:sequence> </xs:complexType> </xs:schema> <xs:schema xmlns:ax22="http://service.axisspring.com/xsd" attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="http://service.axisspring.com"> <xs:import namespace="http://service.axisspring.com/xsd"/> <xs:element name="serviceA"> <xs:complexType> <xs:sequence> <xs:element minOccurs="0" name="name" nillable="true" type="xs:string"/> <xs:element minOccurs="0" name="age" type="xs:int"/> </xs:sequence> </xs:complexType> </xs:element> <xs:element name="serviceAResponse"> <xs:complexType> <xs:sequence> <xs:element minOccurs="0" name="return" nillable="true" type="ax22:Person"/> </xs:sequence> </xs:complexType> </xs:element> </xs:schema> </wsdl:types> <wsdl:message name="serviceARequest"> <wsdl:part name="parameters" element="ns:serviceA"/> </wsdl:message> <wsdl:message name="serviceAResponse"> <wsdl:part name="parameters" element="ns:serviceAResponse"/> </wsdl:message> <wsdl:portType name="testServicePortType"> <wsdl:operation name="serviceA"> <wsdl:input message="ns:serviceARequest" wsaw:Action="urn:serviceA"/> <wsdl:output message="ns:serviceAResponse" wsaw:Action="urn:serviceAResponse"/> </wsdl:operation> </wsdl:portType> <wsdl:binding name="testServiceSoap11Binding" type="ns:testServicePortType"> <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/> <wsdl:operation name="serviceA"> <soap:operation soapAction="urn:serviceA" style="document"/> <wsdl:input> <soap:body use="literal"/> </wsdl:input> <wsdl:output> <soap:body use="literal"/> </wsdl:output> </wsdl:operation> </wsdl:binding> <wsdl:binding name="testServiceSoap12Binding" type="ns:testServicePortType"> <soap12:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/> <wsdl:operation name="serviceA"> <soap12:operation soapAction="urn:serviceA" style="document"/> <wsdl:input> <soap12:body use="literal"/> </wsdl:input> <wsdl:output> <soap12:body use="literal"/> </wsdl:output> </wsdl:operation> </wsdl:binding> <wsdl:binding name="testServiceHttpBinding" type="ns:testServicePortType"> <http:binding verb="POST"/> <wsdl:operation name="serviceA"> <http:operation location="testService/serviceA"/> <wsdl:input> <mime:content type="text/xml" part="serviceA"/> </wsdl:input> <wsdl:output> <mime:content type="text/xml" part="serviceA"/> </wsdl:output> </wsdl:operation> </wsdl:binding> <wsdl:service name="testService"> <wsdl:port name="testServiceHttpSoap11Endpoint" binding="ns:testServiceSoap11Binding"> <soap:address location="http://localhost:8080/axisspring/services/testService.testServiceHttpSoap11Endpoint/"/> </wsdl:port> <wsdl:port name="testServiceHttpSoap12Endpoint" binding="ns:testServiceSoap12Binding"> <soap12:address location="http://localhost:8080/axisspring/services/testService.testServiceHttpSoap12Endpoint/"/> </wsdl:port> <wsdl:port name="testServiceHttpEndpoint" binding="ns:testServiceHttpBinding"> <http:address location="http://localhost:8080/axisspring/services/testService.testServiceHttpEndpoint/"/> </wsdl:port> </wsdl:service> </wsdl:definitions>
把这个wsdl保存成文件, 用soapui测试, 可以正常使用
有些要提的地方,
所有public方法自动发布成webservice
函数的输入自动匹配成webservice的输入, 返回成输出,
如果我的返回数据不止一个, 比如上面我就包装了一个person, 所以返回的数据的顶层节点就是一个person, person里面可以有很多节点,很多层,
但假如我想要返回的是2个顶层的节点,就不知道怎么弄了, 我试过返回一个map里面放2个数据, 但不行,之前的那篇文章的集成方法就可以返回2个顶层的节点
还有假如要返回数组, 返回文件流 等 也还没有去研究