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 ProjectDependencies 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
|