Using Camel with Spring-boot to build a REST Application

Stephane Karagulmez

Basically, I want to be able to post an object in JSON and print the details of this object using Java.

In order to o that I want (and I have to) use SPRING-BOOT and Camel

This is the class representing my object :

    public class Response {
    private long id;
    private String content;

    public Response(){

    }

    public Response(long id, String content) {
        this.id = id;
        this.content = content;
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content){
        this.content = content;
    }

}

Then I have a Rest Controller :

    @RestController
public class BasicController {

    private static final String template = "Hello, %s!";
    private final AtomicLong counter = new AtomicLong();

    //Handle a get request 
    @RequestMapping("/test")
    public Response getResponse(@RequestParam(value="name", defaultValue="World") String name) {
        System.out.println("Handle by spring");
        return new Response(counter.incrementAndGet(),
                            String.format(template, name));
    }

    //Handle a post request
    @RequestMapping(value = "/post", method = RequestMethod.POST)
    public ResponseEntity<Response> update(@RequestBody Response rep) {

        if (rep != null) {
            rep.setContent("HANDLE BY SPRING");
        }  
        return new ResponseEntity<Response>(rep, HttpStatus.OK);
    }
}

With this code i'm able to handle a post request and print detail BUT I have to use Camel. So I tryed the following :

1) I added a bean conf

@SpringBootApplication
public class App 
{

    private static final String CAMEL_URL_MAPPING = "/camel/*";
    private static final String CAMEL_SERVLET_NAME = "CamelServlet";

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

    @Bean
    public ServletRegistrationBean servletRegistrationBean() {
        ServletRegistrationBean registration = new ServletRegistrationBean(new CamelHttpTransportServlet(), CAMEL_URL_MAPPING);
        registration.setName(CAMEL_SERVLET_NAME);
        return registration;
    }
}

2) Then I created 2 routes.

<?xml version="1.0" encoding="UTF-8"?>
<routes xmlns="http://camel.apache.org/schema/spring">
<!--      <route id="test"> -->
<!--          <from uri="timer://trigger"/> -->
<!--          <to uri="log:out"/> -->
<!--      </route> -->
     <route id="test2">
         <from uri="servlet:///test"/>
         <to uri="log:Handle by camel"/>
     </route>
 </routes>

With this I'm able to hand a request on camel. BUT I don't know how to make the link between Spring and camel. Is there a way to handle a request with my spring controller and then call a camel route? On the same URL..

jny

You can use autowired producerTemplate to call Camel routes. It will be created if you add the dependency:

<dependency>
    <groupId>org.apache.camel</groupId>
    <artifactId>camel-spring-boot</artifactId>
    <version>${camel.version}</version> <!-- use the same version as your Camel core version -->
</dependency>

For more info you can see Camel documentation here.

In your case you would call something like:

producerTemplate.sendBody...

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Using Camel with Spring-boot to build a REST Application

From Dev

NullPointerException while testing Spring boot camel application

From Dev

Apache Camel route and Spring boot: application startup

From Dev

Invoke external REST API using Camel and Spring

From Dev

Apache Camel Routing with Active MQ queue in a spring boot application

From Dev

how to build console command in spring boot web application using spring shell?

From Dev

how to build console command in spring boot web application using spring shell?

From Dev

Can I convert my application in servlet to Spring using REST web service and spring boot?

From Dev

Spring-Boot application does not build properly

From Dev

Spring boot REST application testing approach

From Dev

How to use Firebase with Spring boot REST Application?

From Dev

Connecting MySQL to a Spring Boot REST Application

From Dev

testing spring boot rest application with restAssured

From Dev

How to use Firebase with Spring boot REST Application?

From Dev

Handling gzipped requests in a Spring Boot REST application

From Dev

REST API with websocket using Spring boot

From Dev

how to build a spring boot jar exposing both rest and soap service

From Dev

Spring boot 2.0.0.BUILD-SNAPSHOT rest: Can not override _serializer?

From Dev

Spring boot and camel testing with @SpringBootTest

From Dev

Using Coda Hale Meter in Spring Boot application

From Dev

maven build failing when generating war package for spring boot application?

From Dev

Build jar file but (dockerize) Spring Boot Application starts

From Dev

Spring Boot Application test build fails on Travis CI

From Dev

easing the gradle build process in deploying spring boot and angular js application

From Dev

Getting Warning in Spring boot application While doing Gradle Clean build

From Dev

Error while Handling 404 error for Spring Boot REST application

From Dev

Getting "No message available" error with Spring Boot + REST application

From Dev

Securing JSON-PATCH paths in Spring Boot Data Rest application

From Dev

Can Spring Boot application have separate security for REST APIs?

Related Related

  1. 1

    Using Camel with Spring-boot to build a REST Application

  2. 2

    NullPointerException while testing Spring boot camel application

  3. 3

    Apache Camel route and Spring boot: application startup

  4. 4

    Invoke external REST API using Camel and Spring

  5. 5

    Apache Camel Routing with Active MQ queue in a spring boot application

  6. 6

    how to build console command in spring boot web application using spring shell?

  7. 7

    how to build console command in spring boot web application using spring shell?

  8. 8

    Can I convert my application in servlet to Spring using REST web service and spring boot?

  9. 9

    Spring-Boot application does not build properly

  10. 10

    Spring boot REST application testing approach

  11. 11

    How to use Firebase with Spring boot REST Application?

  12. 12

    Connecting MySQL to a Spring Boot REST Application

  13. 13

    testing spring boot rest application with restAssured

  14. 14

    How to use Firebase with Spring boot REST Application?

  15. 15

    Handling gzipped requests in a Spring Boot REST application

  16. 16

    REST API with websocket using Spring boot

  17. 17

    how to build a spring boot jar exposing both rest and soap service

  18. 18

    Spring boot 2.0.0.BUILD-SNAPSHOT rest: Can not override _serializer?

  19. 19

    Spring boot and camel testing with @SpringBootTest

  20. 20

    Using Coda Hale Meter in Spring Boot application

  21. 21

    maven build failing when generating war package for spring boot application?

  22. 22

    Build jar file but (dockerize) Spring Boot Application starts

  23. 23

    Spring Boot Application test build fails on Travis CI

  24. 24

    easing the gradle build process in deploying spring boot and angular js application

  25. 25

    Getting Warning in Spring boot application While doing Gradle Clean build

  26. 26

    Error while Handling 404 error for Spring Boot REST application

  27. 27

    Getting "No message available" error with Spring Boot + REST application

  28. 28

    Securing JSON-PATCH paths in Spring Boot Data Rest application

  29. 29

    Can Spring Boot application have separate security for REST APIs?

HotTag

Archive