Spring3 and Axis2 Integration(I)

Spring3 and Axis2 Integration(I)

I use spring3 and axis2.
1. we will have our spring configuration file and our spring bean configuration
main-context.xml:
<context:annotation-config />
<context:component-scan base-package="com.sillycat">
</context:component-scan>
<import resource="classpath:resource-context.xml" />
<import resource="classpath:core-context.xml" />
<import resource="classpath:dao-context.xml" />
<import resource="classpath:manager-context.xml" />

<import resource="classpath:service-easyaxis2proxy-context.xml" />

our webservice bean configuration file service-easyaxis2proxy-context.xml:
<bean id="personService" class="com.sillycat.easyaxis2proxy.service.PersonServiceImpl" >
<property name="personManager" ref="personManager"/>
</bean>

2. axis2 configuration is as the same as we did in easyaxis project

3. spring3 axis2 intergration
We will not have aar file, we will put the configuration file WEB-INF/services/PersonService/META-INF/services.xml
<?xml version="1.0" encoding="UTF-8"?>
<service name="PersonService">
<description>
Person Demo Service
</description>
<parameter name="ServiceObjectSupplier">
org.apache.axis2.extensions.spring.receivers.SpringServletContextObjectSupplier
</parameter>
<parameter name="SpringBeanName">
personService
</parameter>
<messageReceivers>
<messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out"
class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
<messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-only"
class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver" />
</messageReceivers>
</service>

We can call spring bean named personService in our axis2 services.xml file directly.

Our web.xml will as follow:
<display-name>easyaxis2proxy</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:main-context.xml</param-value>
</context-param>
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
<!-- axis2 start -->
<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>
<servlet-name>SOAPMonitorService</servlet-name>
<servlet-class>
org.apache.axis2.soapmonitor.servlet.SOAPMonitorService
</servlet-class>
<init-param>
<param-name>SOAPMonitorPort</param-name>
<param-value>5001</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>AxisServlet</servlet-name>
<url-pattern>/servlet/AxisServlet</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>AxisServlet</servlet-name>
<url-pattern>*.jws</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>AxisServlet</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>SOAPMonitorService</servlet-name>
<url-pattern>/SOAPMonitor</url-pattern>
</servlet-mapping>

4. Everything is ok, we can visit the listservice page
http://localhost:8080/easyaxis2proxy/services/listServices

we can get the WSDL file from here:
http://localhost:8080/easyaxis2proxy/services/PersonService?wsdl

5. Run my junit test, PersonServiceRemoteTest:
package com.sillycat.easyaxis2proxy.remote;

import javax.xml.namespace.QName;

import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.rpc.client.RPCServiceClient;
import org.apache.axis2.transport.http.HTTPConstants;

import com.sillycat.core.BaseTestCase;
import com.sillycat.easyjpa.model.Person;

public class PersonServiceRemoteTest extends BaseTestCase
{

public void setUp() throws Exception
{
super.setUp();
}

public void tearDown() throws Exception
{
super.tearDown();
}

@SuppressWarnings("unchecked")
public void testGet()
{
RPCServiceClient serviceClient = null;
try
{
serviceClient = new RPCServiceClient();
Options options = serviceClient.getOptions();
EndpointReference targetEPR = new EndpointReference("http://localhost:8080/easyaxis2proxy/services/PersonService");
options.setTimeOutInMilliSeconds(1800000); // 10000 seconds
options.setProperty(HTTPConstants.CHUNKED, Boolean.FALSE);
options.setProperty(HTTPConstants.SO_TIMEOUT, 1800000);
options.setProperty(HTTPConstants.CONNECTION_TIMEOUT, 1800000);
options.setProperty(HTTPConstants.REUSE_HTTP_CLIENT, Boolean.TRUE);
options.setExceptionToBeThrownOnSOAPFault(true);
options.setTo(targetEPR);

// Getting the person
QName opGet = new QName("http://service.easyaxis2proxy.sillycat.com", "get");
Object[] opGetArgs = new Object[] { 2 };
Class[] returnTypes = new Class[] { Person.class };
Object[] response = serviceClient.invokeBlocking(opGet, opGetArgs, returnTypes);

Person result = (Person) response[0];

if (result == null)
{
System.out.println("Person didn't initialize!");
return;
}
// Displaying the result
System.out.println("Name : " + result.getName());
System.out.println("Age : " + result.getAge());
System.out.println("Birthday : " + result.getBirthday());
System.out.println("Sex : " + result.getSex());
System.out.println("Id : " + result.getId());
}
catch (Exception e)
{
System.out.println("error:" + e);
}
finally
{
try
{
// clear up
serviceClient.cleanupTransport();
}
catch (Exception e)
{
System.out.println("error:" + e);
}
}
}

}

I got this error message when I run my junit test:
date string can not be less than 19 charactors

It seems that the axis2 can not deal with Date type well. So I think the easiest way to do this is to trans Date to String in WebService level.

ok, everything is running well, the sample project is easyaxis2proxy.

references:
http://wujianjun.iteye.com/blog/517150
http://wujianjun.iteye.com/blog/517152
http://axis.apache.org/axis2/java/core/docs/spring.html
http://cheney-mydream.iteye.com/blog/390232
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值