Close

Spring - Support for Hessian binary web service and client

[Updated: Aug 16, 2017, Created: Jun 1, 2017]

In Spring servlet based web application, we can transparently expose our Java interface based services using Hessian binary HTTP-based protocol. Let's see an example to understand how to do that. We are going to develop two projects; one for the server and other for the client.

Hessian server example

Service Interface

public interface OrderService {

  void placeOrder(String item, int quantity);

  List<Order> getOrderList();

}
public class Order implements Serializable {
  private String item;
  private int qty;
  private Date orderDate;
  //getters/setters
}

Service implementation

public class OrderServiceImpl implements OrderService {
  private List<Order> orders = new ArrayList<>();

  @Override
  public void placeOrder(String item, int quantity) {
      Order order = new Order();
      order.setItem(item);
      order.setQty(quantity);
      order.setOrderDate(new Date());
      System.out.println("placing order: " + order);
      orders.add(order);
  }

  @Override
  public List<Order> getOrderList() {
      return new ArrayList<>(orders);
  }
}

Registering HessianServiceExporter

@EnableWebMvc
@Configuration
public class AppConfig {

  @Bean
  public OrderService orderService() {
      return new OrderServiceImpl();
  }

  @Bean(name = "/OrderService")
  public HessianServiceExporter exporter() {
      HessianServiceExporter hse = new HessianServiceExporter();
      hse.setService(orderService());
      hse.setServiceInterface(OrderService.class);
      return hse;
  }
}

Note that, we registered HessianServiceExporter with name '/OrderService'; this name will be used as the URI of the exposed service.

To try examples, run embedded tomcat (configured in pom.xml of example project below):

mvn tomcat7:run-war

Server Project

Dependencies and Technologies Used:

  • spring-webmvc 4.3.8.RELEASE: Spring Web MVC.
  • hessian 4.0.38: Hessian is a compact binary protocol for connecting web services.
  • javax.servlet-api 3.1.0 Java Servlet API
  • JDK 1.8
  • Maven 3.3.9

Hessian Server Project Select All Download
  • hessian-service-exporter
    • src
      • main
        • java
          • com
            • logicbig
              • example
                • order
                • AppConfig.java

    Hessian service client

    For the client project, we will be using same OrderService interface. A proxy implementation of the service will be provided by HessianProxyFactoryBean.

    A bean accessing the remote service

    public class OrderBean {
    
      @Autowired
      private OrderService orderService;
    
      public void placeOrder() {
          System.out.println("-- placing orders --");
          orderService.placeOrder("ABC Tablet", 2);
          orderService.placeOrder("XYZ Desktop", 3);
      }
    
      public void listOrders() {
          System.out.println("-- getting order list from service --");
          List<Order> orderList = orderService.getOrderList();
          System.out.println(orderList);
      }
    }

    Registering HessianProxyFactoryBean and running the client

    @Configuration
    public class ExampleClient {
    
      @Bean
      public OrderBean orderBean() {
          return new OrderBean();
      }
    
      @Bean
      public HessianProxyFactoryBean exporter() {
          HessianProxyFactoryBean b = new HessianProxyFactoryBean();
          b.setServiceUrl("http://localhost:8080/OrderService");
          b.setServiceInterface(OrderService.class);
          return b;
      }
    
      public static void main(String[] args) {
          AnnotationConfigApplicationContext context =
                  new AnnotationConfigApplicationContext(ExampleClient.class);
          OrderBean bean = context.getBean(OrderBean.class);
          bean.placeOrder();
          bean.listOrders();
      }
    }

    Output

    -- placing orders --
    -- getting order list from service --
    [Order{item='ABC Tablet', qty=2, orderDate=Mon Jun 12 21:23:57 CDT 2017}, Order{item='XYZ Desktop', qty=3, orderDate=Mon Jun 12 21:23:57 CDT 2017}, Order{item='ABC Tablet', qty=2, orderDate=Mon Jun 12 21:24:05 CDT 2017}, Order{item='XYZ Desktop', qty=3, orderDate=Mon Jun 12 21:24:05 CDT 2017}]

    Client Project

    Dependencies and Technologies Used:

    • spring-webmvc 4.3.8.RELEASE: Spring Web MVC.
    • hessian 4.0.38: Hessian is a compact binary protocol for connecting web services.
    • JDK 1.8
    • Maven 3.3.9

    Hessian Client Project Select All Download
    • hessian-service-client
      • src
        • main
          • java
            • com
              • logicbig
                • example
                  • order
                  • ExampleClient.java

      See Also