Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Implementing Spring HTTP Invoker for Remote Service Communication

Tech May 18 2

Server-Side Implementation

Service Enterface Definition

package com.example.service;

public interface MessageService {
    String processMessage(String input);
}

Service Implementation

package com.example.service.impl;

public class MessageServiceImpl implements MessageService {
    private NotificationService notificationService;

    @Override
    public String processMessage(String input) {
        String response = "Remote invocation successful: " + input;
        return response;
    }

    public void setNotificationService(NotificationService notificationService) {
        this.notificationService = notificationService;
    }
}

HTTP Invoker Configuration (message-servlet.xml)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="messageServiceBean" class="com.example.service.impl.MessageServiceImpl">
        <property name="notificationService" ref="notificationService"/>
    </bean>

    <bean name="/messageService" class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter">
        <property name="service" ref="messageServiceBean"/>
        <property name="serviceInterface" value="com.example.service.MessageService"/>
    </bean>
</beans>

Web Application Configuration (web.xml)

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/message-servlet.xml</param-value>
</context-param>

<servlet>
    <servlet-name>message</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>message</servlet-name>
    <url-pattern>/api/*</url-pattern>
</servlet-mapping>

Cleint-Side Implementation

Client Service Interface

package com.client.service;

// Interface method signatures must match server-side exactly
public interface MessageService {
    String processMessage(String input);
}

Client Proxy Configuration (client-config.xml)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd">


    <bean id="messageServiceProxy"
          class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean">
        <property name="serviceUrl"
                  value="http://localhost:8080/ServerApp/api/messageService"/>
        <property name="serviceInterface" value="com.client.service.MessageService"/>
    </bean>
</beans>

Action Class Integration

package com.client.actions;

import com.opensymphony.xwork2.ActionSupport;
import com.client.service.MessageService;

public class RemoteCallAction extends ActionSupport {

    private MessageService messageService;
    private String response;

    public String executeRemoteCall() {
        String requestData = "Sending data to server";
        response = messageService.processMessage(requestData);
        System.out.println("Response received: " + response);
        return SUCCESS;
    }

    public void setMessageService(MessageService messageService) {
        this.messageService = messageService;
    }

    public String getResponse() {
        return response;
    }

    public void setResponse(String response) {
        this.response = response;
    }
}

Web Configuration for Client (web.xml)

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/client-config.xml</param-value>
</context-param>

Spring Bean Wiring

<bean id="remoteCallAction" class="com.client.actions.RemoteCallAction"
      scope="prototype">
    <property name="messageService">
        <ref bean="messageServiceProxy"/>
    </property>
</bean>

Communication Flow

  1. Server exposes MessageService via HttpInvokerServiceExporter at /api/messageService
  2. Client configures HttpInvokerProxyFactoryBean pointing to the server endpoint
  3. Client invokes methods on the proxy as if calling a local service
  4. Spring serializes the request, transmits via HTTP, and deserializes the resposne

The HTTP Invoker mechanism provides a lightweight RMI alternative using standard Java serialization over HTTP, requiring both client and server to use compatible Spring versions.

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.