Friday, October 1, 2010

Start to develop web service with Hessian

This article is to demonstrate how to develop web service APIs using hessian. ( access http://hessian.caucho.com/ to learn more details about hessian. )

Environment:
 JDK  v1.6.x
 Tomcat  v6.0.18
 Hessian  v4.0.7

 API development:
 1) create a dynamic web project with eclipse

 2) download hessian jar file and copy it to %Project_Home%/WebContent/WEB-INF/lib

 3) create a interface which contains all specific APIs. give an example below.

IServices.java
public interface IServices {
    public String changeEmail(String newEmail);
    public String retrieveEmail(String myId);
}

4) create a class to implement above interface

MyServicesImpl.java
public class MyServicesImpl extends  HessianServlet implements IServices {

  @Override
 public String changeEmail(String newEmail) {

  return "Your email address has been changed to " + newEmail;
 }

 @Override
 public String retrieveEmail(String myId) {

  return "User["+myId+"] email is ABC@gmail.com." }

};

5) Edit web.xml as follows

<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
 id="WebApp_ID" version="2.5">
 <display-name>hessiantest</display-name>

 <servlet>
  <servlet-name>HessianServlet</servlet-name>
  <servlet-class>com.caucho.hessian.server.HessianServlet</servlet-class>
  <init-param>
   <param-name>home-class</param-name>
   <param-value>mypackage.impl.MyServicesImpl</param-value>
  </init-param>
  <init-param>
   <param-name>home-api</param-name>
   <param-value>mypackage.IServices</param-value>
  </init-param>
 </servlet>

 <servlet-mapping>
  <servlet-name>HessianServlet</servlet-name>
  <url-pattern>/myService</url-pattern>
 </servlet-mapping>


 <welcome-file-list>
  <welcome-file>index.html</welcome-file>
  <welcome-file>index.htm</welcome-file>
  <welcome-file>index.jsp</welcome-file>
 </welcome-file-list>
</web-app>
 
6) compile the code, generate the war file and deploy to tomcat.

Test Class:

1) create a java project

2) generate a jar file which contains above interface class and its implementation classes.

 jar cvf myapi.jar *.*

After generating the jar file, put the jar file to classpath, and put hessian jar file to classpath as well.

3) create a test class to call above APIs.

ServiceTest.java
class MyServiceTest {

 public static void main(String[] args) throws Exception{
  String url = "http://localhost:9080/myapi/myService";
  HessianProxyFactory factory = new HessianProxyFactory();
  IServices service = (IServices) factory.create(IServices.class, url);

  System.out.println("Change Email Response: " + service.changeEmail("ABC@gmail.com"));
  System.out.println("Retrieve Email Response: " + service.retrieveEmail("My ID"));

 }
}

If everything is OK, you can see the result via eclipse console :
Change Email Response: Your email address has been changed to ABC@gmail.com
Retrieve Email Response: User[My D] email is ABC@gmail.com.

the myapi.jar file can only contain the interface class and its related POJO classes in the test class project.
If using C# in test project, need import hessian DLL file, and rewrite the interface class with C# but the same content writing in java. If such, client in .NET environment also can call the servlet developed by java.

No comments:

Post a Comment