How to use Spring Integration Http Inbound with Spring Boot

In the tutorial, JavaSampleApproach will guide you how to use Spring Http Inbound with Spring Boot.
Related Post:
How to start Spring Integration 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

Http Inbound Channel Adapter or Gateway is used to receive http messages.
inbound-channel-adapter:


In case, the kind of request need a returning with a customized message, use Http Inbound Gateway.


In the tutorial, we create http RESTful services, use: http:inbound-gateway for GET request with dedicated returning result, use: http:inbound-channel-adapter for POST, PUT & DELETE requests without customized result for returning.

http:inbound-channel-adapter for: POST, PUT & DELETE requests
Spring Boot Spring Integration Http Inbound Channel - inbound adapter

http:inbound-gateway for GET requests
Spring Boot Spring Integration Http Inbound Channel - inbound gateways

1. Project Structure

Spring Boot Spring Integration Http Inbound Channel - project structure

2. Step to do

– Create Spring Boot project
– Create simple Customer model
– Create Customer Services
– Implement Endpoints for GET, POST, PUT, DELETE
– Config Inbound Adapter & Inbound Gateway
– Run & Enjoys 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
	SpringIntegrationInboundComponent
	0.0.1
	jar

	SpringIntegrationInboundComponent
	SpringIntegrationInboundComponent

	
		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 simple Customer model
package com.javasampleapproach.springintegration.inbound.model;

public class Customer {
	private int id;
	private String name;
	private int age;
	
	public Customer(){
	}
	
	public Customer(int id, String name, int age){
		this.id = id;
		this.name = name;
		this.age = age;
	}
	
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}
	
	public String toString(){
		String info = String.format("Customer with id = %d, name = %s, age = %d", id, name, age);
		return info;
	}
}

3. Create Customer Services

Create a simple customer services for: insert, update, delete, get.

package com.javasampleapproach.springintegration.inbound.services;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import javax.annotation.PostConstruct;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.javasampleapproach.springintegration.inbound.model.Customer;

@Service
public class CustomerService {
	Map customerStorage = new HashMap();
	
	
	Logger log = LoggerFactory.getLogger(this.getClass().getName());
	
	@PostConstruct
	public void init(){
		Customer jack = new Customer(1, "Jack", 20);
		Customer peter = new Customer(2, "Peter", 30);
		
		customerStorage.put(jack.getId(), jack);
		customerStorage.put(peter.getId(), peter);
	}
	
	public void insert(Customer customer){
		customerStorage.put(customer.getId(), customer);
		log.info("Customers after POST:");
		for (Map.Entry entry : customerStorage.entrySet()) {
			log.info(entry.getValue().toString());
		}
	}
	
	public List getAll(){
		
		List result = customerStorage.entrySet().stream()
		        .map(entry -> entry.getValue())
		        .collect(Collectors.toList());
		
		return result;
	}
	
	public void delete(int id){
		try{
			customerStorage.remove(id);			
		}catch(Exception e){
		}

	}
	
	public void change(Customer newCust){
		customerStorage.put(newCust.getId(), newCust);
		log.info("Customers after PUT:");
		for (Map.Entry entry : customerStorage.entrySet()) {
			log.info(entry.getValue().toString());
		}
	}
}
4. Implement Service Activator – Endpoint for GET, POST, PUT, DELETE
package com.javasampleapproach.springintegration.inbound.endpoint;

import java.util.List;

import javax.annotation.Resource;

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.inbound.model.Customer;
import com.javasampleapproach.springintegration.inbound.services.CustomerService;

@Component
public class InboundEnpoint {
	private Logger log = LoggerFactory.getLogger(this.getClass().getName());
	
	@Resource
	CustomerService custService;

	public Message get(Message msg) {
		log.info("GET method");
		List custLst = custService.getAll();
		return MessageBuilder.withPayload(custLst).copyHeadersIfAbsent(msg.getHeaders())
				.setHeader("http_statusCode", HttpStatus.OK).build();
	}
	
	public void post(Message msg){
		log.info("POST method");
		custService.insert(msg.getPayload());
	}
	
	public void put(Message msg){
		log.info("PUT method");
		custService.change(msg.getPayload());
	}
	
	public void delete(Messagemsg){
		log.info("DELETE method");
		int id = Integer.valueOf(msg.getPayload());
		custService.delete(id);
	}
}

5. Config Inbound Adapter & Inbound Gateway

For GET requests, use http:inbound-gateway to handle


 
     

Use method get of InboundEnpoint for service-activator:


Full Config: create http-inbound-gateway.xml:



 
    
    
    
    
 
        
    
 
    
 

For POST, PUT & DELETE requests, use inbound-channel-adapte

POST & PUT:



		

DELETE:


    
    	

Full Config: create http-inbound-adapter.xml file:




	
	
	
	
	
	
	
	
	

		
	

	
		
		
	

	
	
	
	
	
	
	
	
    
    	
	
	
	



status-code-expression: By default, the channel adapter will returns a 200 status code. In the case we specify a different status code in this attribute: 204 No Content status code.
request-payload-type: used to specify a class to convert request body.

int:router is used for routing and filtering logic within the Spring Integration flow.
In the tutorial case, it is used for mapping with PUT or POST requests to process.


		
		

– Config @ImportResource({“classpath:http-inbound-adapter.xml”, “classpath:http-inbound-gateway.xml”}) in main class:

package com.javasampleapproach.springintegration.inbound;

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

@SpringBootApplication
@ImportResource({"classpath:http-inbound-adapter.xml", "classpath:http-inbound-gateway.xml"})
public class SpringIntegrationInboundComponentApplication {

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

6. Run & Enjoys Results

Build & Run the project with Spring Boot App mode.
Makes requests:
GET request:
Spring Boot Spring Integration Http Inbound Channel - get result

POST request:
Spring Boot Spring Integration Http Inbound Channel - post result

PUT request:
Spring Boot Spring Integration Http Inbound Channel - put result

DELETE request:
Spring Boot Spring Integration Http Inbound Channel - delete result

GET request:
Spring Boot Spring Integration Http Inbound Channel - last get result

IV. Sourcecode

SpringIntegrationInboundComponent

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