Friday 11 June 2021

Spring integration: Transformers

Transformers are used to transform/convert a message. Using transformer you can

a.   Update the message payload

b.   Update the message headers etc.,

 

In general, transformers are used to convert the message payload from one format to other. For example,

a.   Xml to string

b.   Xml to json

c.    Json to xml

d.   Json to csv etc.,

 

Can a transformer modify the header values?

Yes, a transformer can add, remove, or modify the message’s header values.

 

@Transformer annotation is used to transform a message.

 

Example

@Transformer(inputChannel = "inputChannel", outputChannel = "outputChannel")
public Message<String> convertToUppercase(Message<String> message) {

	String payload = message.getPayload();

	Message<String> messageInUppercase = MessageBuilder.withPayload(payload.toUpperCase()).build();

	return messageInUppercase;

}

 

Above snippet transform the message to uppercase.

 

Step 1: Create new maven project ‘transformer-demo’.

 

Step 2: Update pom.xml with maven dependencies.

 

pom.xml

 

<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.sample.app</groupId>
	<artifactId>transformer-demo</artifactId>
	<version>1</version>

	<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-parent -->
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.4.0</version>
	</parent>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-integration</artifactId>
		</dependency>

	</dependencies>

</project>

 

Step 3: Define gateway.

 

CustomGateway.java
package com.sample.app.gateway;

import org.springframework.integration.annotation.Gateway;
import org.springframework.integration.annotation.MessagingGateway;
import org.springframework.messaging.Message;

@MessagingGateway
public interface CustomGateway {

	@Gateway(requestChannel = "inputChannel")
	public void print(Message<String> message);

}

 

Step 4: Define endpoint.

 

ConsumerEndpoint.java

 

package com.sample.app.endpoints;

import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.messaging.Message;
import org.springframework.stereotype.Component;

@Component
public class ConsumerEndpoint {

	@ServiceActivator(inputChannel = "outputChannel")
	public void consumeStringMessage(Message<String> message) {
		System.out.println("Received message from outputChannel : " + message.getPayload());
	}
}

 

Step 5: Define application class.

 

App.java

 

package com.sample.app;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.integration.annotation.Transformer;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.messaging.Message;

import com.sample.app.gateway.CustomGateway;

@SpringBootApplication
@Configuration
public class App {

	@Transformer(inputChannel = "inputChannel", outputChannel = "outputChannel")
	public Message<String> convertToUppercase(Message<String> message) {

		String payload = message.getPayload();

		Message<String> messageInUppercase = MessageBuilder.withPayload(payload.toUpperCase()).build();

		return messageInUppercase;

	}

	@Autowired
	private CustomGateway customGateway;

	public static void main(String[] args) {
		SpringApplication.run(App.class, args);
	}

	@Bean
	public CommandLineRunner demo() {
		return (args) -> {

			Message<String> msg = MessageBuilder.withPayload("i am in small letters, but will print in capital letters")
					.build();

			customGateway.print(msg);
		};

	};

}

 

Total project structure looks like below.



Run App.java, you will see below messages in console.

Received message from outputChannel : I AM IN SMALL LETTERS, BUT WILL PRINT IN CAPITAL LETTERS


You can download complete working application from below link.

https://github.com/harikrishna553/springboot/tree/master/spring-integration/transformer-demo


 

 

  

Previous                                                    Next                                                    Home

1 comment:

  1. This comment has been removed by a blog administrator.

    ReplyDelete