How to start Spring Integration with Spring Boot

In the tutorial, JavaSampleApproach will show you how to build a simple Spring Integration with Spring Boot.

Related post:
How to use Spring Integration Http Inbound with Spring Boot


I. Technologies

– Java 1.8
– Maven 3.3.9
– Spring Tool Suite – Version 3.8.1.RELEASE
– Spring Boot: 1.5.1.RELEASE

II. Overview

In the tutorial, we create a simple Spring Integration Appliation with Http Inbound Gateway for support-method: GET.

spring boot spring integration overview http inbound gate

1. Project Structure

spring boot spring integration overview project

2. Step to do

– Create Spring Boot project
– Create a simple message
– Create a Service Activator
– Config Http Inbound GateWay
– Run and Enjoy Results

III. Practices
1. Create Spring Boot project

– Open Spring Tool Suite, on main menu, choose File->New->Spring Starter Project, input project info. Press Next then Finish, a Spring Boot project will be created successfully.

Add dependencies:
spring-boot-starter-integration
spring-integration-http
spring-boot-starter-web
Details:



	4.0.0

	com.javasampleapproach.springintegration
	SpringIntegration
	0.0.1
	jar

	SpringIntegration
	SpringIntegration

	
		org.springframework.boot
		spring-boot-starter-parent
		1.5.1.RELEASE
		 
	

	
		UTF-8
		UTF-8
		1.8
	

	

		
			org.springframework.boot
			spring-boot-starter-integration
		

		
			org.springframework.integration
			spring-integration-http
		

		
			org.springframework.boot
			spring-boot-starter
		
		
		
			org.springframework.boot
			spring-boot-starter-web
		

		
			org.springframework.boot
			spring-boot-starter-test
			test
		
	

	
		
			spring-releases
			Spring Releases
			http://repo.spring.io/libs-release
		
	

	
		
			spring-releases
			http://repo.spring.io/libs-release
		
	

	
		
			
				org.springframework.boot
				spring-boot-maven-plugin
			
		
	




2. Create a simple message
package com.javasampleapproach.springintegration.msg;

public class HelloMsg {
	private String msg;
	private String currentTime;
	
	public HelloMsg(){}
	
	public HelloMsg(String msg, String currentTime){
		this.msg = msg;
		this.currentTime = currentTime;
	}
	
	public String getMsg() {
		return msg;
	}
	public void setMsg(String msg) {
		this.msg = msg;
	}
	public String getCurrentTime() {
		return currentTime;
	}
	public void setCurrentTime(String currentTime) {
		this.currentTime = currentTime;
	}
}
3. Create a Service Activator
package com.javasampleapproach.springintegration.endpoint;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.messaging.Message;
import org.springframework.stereotype.Component;

import com.javasampleapproach.springintegration.msg.HelloMsg;

@Component
public class WelcomeEndpoint {
	private Logger log = LoggerFactory.getLogger(this.getClass().getName());
	
	public Message get(Message msg) {
        String name = msg.getPayload();
        // Log
        log.info("Request with name = " + name);
        
        // Get currentTime
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
        LocalDateTime now = LocalDateTime.now();
        String currentTime = dtf.format(now);
        
        String strMsg = "Hello " + name + "! " + "Welcome to Spring Integration with Spring Boot!";
        
        HelloMsg returnMsg = new HelloMsg(strMsg, currentTime);
        
        return MessageBuilder.withPayload(returnMsg)
            .copyHeadersIfAbsent(msg.getHeaders())
            .setHeader("http_statusCode", HttpStatus.OK)
            .build();
    }
}
4. Config Http Inbound GateWay

Create a xml config file integration.xml:




	
	
	
	

		
	

	


Use int-http:inbound-gateway for GET http method.
The pathvariable name will be payload message:
path="/welcome/{name}" payload-expression="#pathVariables.name"
Element: service-activator is used to define an endpoint: WelcomeEndpoint:


Config @ImportResource(“classpath:integration.xml”) in main class:

package com.javasampleapproach.springintegration;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;

@SpringBootApplication
@ImportResource("classpath:integration.xml")
public class SpringIntegrationApplication {

	public static void main(String[] args) {
		SpringApplication.run(SpringIntegrationApplication.class, args);
	}
}
5. Run and Enjoy Results

Build and Run the project with SpringBoot App mode.
Make a request: http://localhost:8080/welcome/Jack with header Content-Type:application/json .
Result:
spring boot spring integration result

IV. Sourcecode

SpringIntegration

0 0 votes
Article Rating
Subscribe
Notify of
guest
535 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments