groupId : com.example
artifactId : SpringService
packaging : war
Add axis2m, axis2 and Spring dependencies in to the pom.xml. This is my pom.xml.
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>SpringService</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>org.axis2m</groupId>
<artifactId>axis2m-spring</artifactId>
<version>1.0M2</version>
</dependency>
<dependency>
<groupId>org.apache.axis2</groupId>
<artifactId>axis2-adb</artifactId>
<version>1.5.4</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
<version>2.5.6</version>
</dependency>
</dependencies>
</project>
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>SpringService</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>org.axis2m</groupId>
<artifactId>axis2m-spring</artifactId>
<version>1.0M2</version>
</dependency>
<dependency>
<groupId>org.apache.axis2</groupId>
<artifactId>axis2-adb</artifactId>
<version>1.5.4</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
<version>2.5.6</version>
</dependency>
</dependencies>
</project>
Now I create a new java class. This is the class where I am going to write my service methods. I add getHello() method as below.
public class HelloService {
public String getMessage(String username) {
return "Hello " + username + "! from spring web service.";
}
}
public String getMessage(String username) {
return "Hello " + username + "! from spring web service.";
}
}
Now I have to register this class as a Spring bean. So we need to create a beans xml file.
Create new xml file in src/main/webapp/WEB-INF folder. I name it as context.xml. This is the content of my context.xml file.
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="myService" class="org.axis2m.spring.beans.ServiceBean">
<property name="serviceName" value="TestService" />
<property name="serviceBean" ref="myServiceClass" />
<property name="targetNameSpace" value="com.hello"/>
</bean>
<bean id="myServiceClass" class="services.HelloService" />
</beans>
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="myService" class="org.axis2m.spring.beans.ServiceBean">
<property name="serviceName" value="TestService" />
<property name="serviceBean" ref="myServiceClass" />
<property name="targetNameSpace" value="com.hello"/>
</bean>
<bean id="myServiceClass" class="services.HelloService" />
</beans>
The line <bean id="myServiceClass" class="com.beans.HelloWorld" /> registers this class as a SpringBean named as 'myServiceClass'.
The block <bean id="testService".... exposes above bean as a web service via axis.
Finally I add the context xml file and axis servlet in to the web xml. This is my web.xml.
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/context.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>AxisServlet</servlet-name>
<servlet-class>org.axis2m.spring.servlet.SpringAxis2Servlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>AxisServlet</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
</web-app>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/context.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>AxisServlet</servlet-name>
<servlet-class>org.axis2m.spring.servlet.SpringAxis2Servlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>AxisServlet</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
</web-app>
Now run the project in tomcat server. After the application is deployed open the web browser and point to below wsdl location.(My tomcat runs on port 8080)
http://localhost:8080/SpringService/services/TestService?wsdl
Wow! The wsdl for my web service is opened. It means my web service has been successfully published.
Look at the wsdl path again. It can be described as below.
I use SOAP-UI to test the service. These are my request and the response received.
Note:
All the public methods written in HelloService class are exposed as web services. If you want any public method in this class not to be exposed as a service you just can mention it in context.xml file as below. For an example if you have a public method myHiddenMethod() in HelloService class, you can avoid it being exposed via web service like this.
<bean id="myService" class="org.axis2m.spring.beans.ServiceBean">
<property name="serviceName" value="TestService" />
<property name="serviceBean" ref="myServiceClass" />
<property name="targetNameSpace" value="com.hello"/>
<property name="excludeOperationsNames" value="myHiddenMethod"/>
</bean>
<property name="serviceName" value="TestService" />
<property name="serviceBean" ref="myServiceClass" />
<property name="targetNameSpace" value="com.hello"/>
<property name="excludeOperationsNames" value="myHiddenMethod"/>
</bean>
Nice post.I am not able to find axis2m-spring artifact from Maven. Can you suggest where i can find it or there is alternate artifact ti use?
ReplyDeleteHi, you can find it here http://axis2m.sourceforge.net/repo/org/axis2m/axis2m-spring/1.0M2/
DeleteNice post by the way, help me a lot
I found your blog while searching for the updates, I am happy to be here. Very useful content and also easily understandable providing.. Believe me I did wrote an post about tutorials for beginners with reference of your blog.
ReplyDeleteonline Python training
python training in chennai