Open In App
Related Articles

Spring – Remoting by HTTP Invoker

Improve Article
Save Article
Like Article

HTTP Invoker is a remote communication mechanism in the Spring framework that enables remote communication between Java objects over HTTP. It allows Java objects to invoke methods on remote Java objects, just as if they were local objects. In this article, we will learn how to implement Spring remoting by HTTP Invoker.

Step by Step Implementation

Step 1: Create a Java project in your favorite IDE and add the following dependencies to the pom.xml file:


<!-- Adding spring-context and spring-web dependencies -->

Step 2: Create an interface called which will define the methods that can be remotely invoked.


// Define the methods that can be remotely invoked
public interface CalculatorService {
   int add(int a, int b);
   int subtract(int a, int b);
   int multiply(int a, int b);
   int divide(int a, int b);

Step 3: Create a class called that implements the CalculatorService interface. This class will be the implementation of remote service.


// Implementation of the remote service
public class CalculatorServiceImpl implements CalculatorService {
   public int add(int a, int b) {
      return a + b;
   public int subtract(int a, int b) {
      return a - b;
   public int multiply(int a, int b) {
      return a * b;
   public int divide(int a, int b) {
      return a / b;

Step 4: Create a class called that will be used to configure the remote service using the HTTP invoker.


// Configuration of the remote service using HTTP Invoker
public class HttpInvokerConfig {
   public HttpInvokerServiceExporter httpInvokerServiceExporter(CalculatorService calculatorService) {
      HttpInvokerServiceExporter exporter = new HttpInvokerServiceExporter();
      // Setting the remote service to be exported
      // Setting the interface of the remote service
      return exporter;

Step 5:Create a class called that will act as a client to the remote service.


// Client to the remote service
public class Client {
    public static void main(String[] args)
        // Get the ApplicationContext
        ApplicationContext context
            = new ClassPathXmlApplicationContext(
        // Get the remote service bean
        CalculatorService calculatorService
            = (CalculatorService)context.getBean(
        // Invoke the remote methods
        System.out.println("Add: "
                           + calculatorService.add(1, 2));
            "Subtract: "
            + calculatorService.subtract(1, 2));
            "Multiply: "
            + calculatorService.multiply(1, 2));
            "Divide: " + calculatorService.divide(1, 2));

Step 6: Create an XML configuration file called applicationContext.xml that will contain the configuration for the client.


<?xml version="1.0" encoding="UTF-8"?>
  <bean id="calculatorService" class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean">
      <property name="serviceUrl" value="http://localhost:8080/calculatorService" />
      <property name="serviceInterface" value="com.geeksforgeeks.CalculatorService" />

Step 7: Run the class as a Java application and you should see the following output:


Add: 3
Subtract: -1
Multiply: 2
Divide: 0


In this article, we learned about the Spring HTTP Invoker and how to implement it step by step. We also covered some further subtopics to give you an idea of the various ways in which you can use the Spring HTTP Invoker in your projects.

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 23 Feb, 2023
Like Article
Save Article
Similar Reads
Complete Tutorials