Search code examples
springrestspring-integrationspring-tool-suite

Create a REST API in spring integration using Spring tool suite


I need to create Restful APIs in Spring Integration.I found a example for the same in https://github.com/spring-projects/spring-integration-samples/tree/master/intermediate/rest-http But somehow this example is not working for me. I was wondering if there is any way to create a Restful API in Spring Integration using STS. As STS provides graphs for Spring Integration, how can we create a REST API using STS graphs directly.

Thanks in Advance.


Solution

  • I don't know why you are having trouble with the sample, I just tested it and it worked ok.

    However, probably the simplest way to get started with STS is (using a recent version >= 3.7)...

    1. File | New... | Spring Starter Project
    2. Set the name to, e.g. rest
    3. Click next
    4. Select Web, Integration (under IO)
    5. Click Finish
    6. Open demo.RestApplication (where Rest is capitalized name from #2)
    7. Add @ImportResource("classpath:context.xml")
    8. Create context.xml in src/main/resources
    9. Run the application and hit http://localhost:8080/foo/bar in your browser - it will output BAR.

    RestApplication:

    @SpringBootApplication
    @ImportResource("classpath:context.xml")
    public class RestApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(RestApplication.class, args);
        }
    
    }
    

    context.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"
        xmlns:int="http://www.springframework.org/schema/integration"
        xmlns:int-http="http://www.springframework.org/schema/integration/http"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
            http://www.springframework.org/schema/integration/http http://www.springframework.org/schema/integration/http/spring-integration-http.xsd">
    
        <int-http:inbound-gateway request-channel="in"
                path="/foo/{id}"
                supported-methods="GET"
                request-payload-type="java.lang.String">
            <int-http:header name="requestedId" expression="#pathVariables.id" />
        </int-http:inbound-gateway>
    
        <int:transformer input-channel="in" expression="headers['requestedId'].toUpperCase()" />
    
    </beans>
    

    EDIT

    To make a deployable war, follow the Spring Boot instructions 'Create a deployable war' here.

    But see the note about old servlet containers that don't support servlet 3.x.

    Here's the updated RestApplication class...

    @SpringBootApplication
    @ImportResource("classpath:context.xml")
    public class RestApplication extends SpringBootServletInitializer {
    
        @Override
        protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
            return application.sources(RestApplication.class);
        }
    
        public static void main(String[] args) {
            SpringApplication.run(RestApplication.class, args);
        }
    
    }