Close

Spring - Exposing services using HTTP invoker

[Last Updated: Aug 16, 2017]

Hessian and Burlap (spring support for Burlap has been deprecated for inactivity), are both lightweight protocols and use their own serialization mechanisms. Spring HTTP invoker, on the other hand, uses the standard Java serialization mechanism to expose services through HTTP, that's the reason all Serializable Java types (as opposed to Hessian case) can be transported through the HTTP invoker.

In this tutorial, we are going to develop two projects; one for the server and other for the client.

HTTP invoker 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 LocalDateTime 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(LocalDateTime.now());
      System.out.println("placing order: " + order);
      orders.add(order);
  }

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

Registering HttpInvokerServiceExporter

@EnableWebMvc
@Configuration
public class AppConfig {

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

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

Note that, we registered HttpInvokerServiceExporter 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.
  • javax.servlet-api 3.1.0 Java Servlet API
  • JDK 1.8
  • Maven 3.3.9

Http Invoker Server Project Select All Download
  • http-invoker-exporter
    • src
      • main
        • java
          • com
            • logicbig
              • example
                • order
                • AppConfig.java

    HTTP invoker client

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

    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 HttpInvokerProxyFactoryBean and running the client

    @Configuration
    public class ExampleClient {
    
      @Bean
      public OrderBean orderBean() {
          return new OrderBean();
      }
    
      @Bean
      public HttpInvokerProxyFactoryBean exporter() {
          HttpInvokerProxyFactoryBean b = new HttpInvokerProxyFactoryBean();
          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=2017-06-12T21:54:00.699}, Order{item='XYZ Desktop', qty=3, orderDate=2017-06-12T21:54:00.714}]

    Client Project

    Dependencies and Technologies Used:

    • spring-webmvc 4.3.8.RELEASE: Spring Web MVC.
    • JDK 1.8
    • Maven 3.3.9

    Http Invoker Client Project Select All Download
    • http-invoker-client
      • src
        • main
          • java
            • com
              • logicbig
                • example
                  • order
                  • ExampleClient.java

      See Also