Java springframework remoting httpinvoker replacement

4.5k Views Asked by At

The spring framework in 5.3 and higher deprecated org.springframework.remoting.httpinvoker

when we upgrade Spring to 5.3

wondering what is the replacement

our existing code base have:

import org.springframework.remoting.httpinvoker.HttpComponentsHttpInvokerRequestExecutor;

import org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean;

HttpInvokerProxyFactoryBean proxy = new HttpInvokerProxyFactoryBean();

HttpComponentsHttpInvokerRequestExecutor re = new HttpComponentsHttpInvokerRequestExecutor();

what I could use to replace the class

1

There are 1 best solutions below

0
On
All the classes under org.springframework.remoting.httpinvoker are deprecated when I am trying to migrate recently .But you can write some custom classes as similar to the below implementation .

    import java.io.*;
    import java.net.HttpURLConnection;
    import java.net.URL;
    
    public class CustomHttpInvokerClient {
        private String serviceUrl;
    
        public CustomHttpInvokerClient(String serviceUrl) {
            this.serviceUrl = serviceUrl;
        }
    
        public int add(int a, int b) throws IOException {
            return (int) invokeRemoteMethod("add", a, b);
        }
    
        public int subtract(int a, int b) throws IOException {
            return (int) invokeRemoteMethod("subtract", a, b);
        }
    
        public int multiply(int a, int b) throws IOException {
            return (int) invokeRemoteMethod("multiply", a, b);
        }
    
        public int divide(int a, int b) throws IOException {
            return (int) invokeRemoteMethod("divide", a, b);
        }
    
        private Object invokeRemoteMethod(String methodName, Object... arguments) throws IOException {
            URL url = new URL(serviceUrl);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("POST");
            connection.setDoOutput(true);
           
          System.out.println("No error till now");
          
          BufferedReader br = null;
          if (connection.getResponseCode() == 200) {
              br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
              String strCurrentLine;
                  while ((strCurrentLine = br.readLine()) != null) {
                         System.out.println(strCurrentLine);
                  }
          } else {
              br = new BufferedReader(new InputStreamReader(connection.getErrorStream()));
              String strCurrentLine;
                  while ((strCurrentLine = br.readLine()) != null) {
                         System.out.println(strCurrentLine);
                  }
          }
          
          
    
            try (ObjectOutputStream oos = new ObjectOutputStream(connection.getOutputStream())) {
                oos.writeObject(methodName);
                oos.writeObject(arguments);
            }
    
            try (ObjectInputStream ois = new ObjectInputStream(connection.getInputStream())) {
                return ois.readObject();
            } catch (ClassNotFoundException e) {
                throw new RuntimeException("Error deserializing response.", e);
            } finally {
                connection.disconnect();
            }
        }
    }
    
    import java.io.IOException;
    
    public class MainApplication {
        public static void main(String[] args) {
            // Replace with the actual URL of your server-side application
            String serviceUrl = "http://localhost:9011/{your service name}/CalculatorService";
    
            CustomHttpInvokerClient customHttpInvokerClient = new CustomHttpInvokerClient(serviceUrl);
    
            try {
                int resultAdd = customHttpInvokerClient.add(10, 5);
                int resultSubtract = customHttpInvokerClient.subtract(10, 5);
                int resultMultiply = customHttpInvokerClient.multiply(10, 5);
                int resultDivide = customHttpInvokerClient.divide(10, 5);
    
                System.out.println("Add: " + resultAdd);
                System.out.println("Subtract: " + resultSubtract);
                System.out.println("Multiply: " + resultMultiply);
                System.out.println("Divide: " + resultDivide);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

import org.springframework.stereotype.Service;

import java.io.IOException;

@Service
public class CalculatorServiceImpl implements CalculatorService {

    @Override
    public int add(int a, int b) throws IOException {
        return a + b;
    }

    @Override
    public int subtract(int a, int b) throws IOException {
        return a - b;
    }

    @Override
    public int multiply(int a, int b) throws IOException {
        return a * b;
    }

    @Override
    public int divide(int a, int b) throws IOException {
        if (b == 0) {
            throw new ArithmeticException("Division by zero");
        }
        return a / b;
    }
}

import java.io.IOException;

public interface CalculatorService {
    int add(int a, int b) throws IOException;
    int subtract(int a, int b) throws IOException;
    int multiply(int a, int b) throws IOException;
    int divide(int a, int b) throws IOException;
}