Skip to content
Advertisement

How to create RESTful Web Service with Spring Boot and Apache Camel?

I’m learning Apache Camel in Spring Boot project and I try to create a Retful Webservice and the service is starting but the problem is that I get 404 when I call the endpoint.

JavaScript

I created this class to hardcode some data:

JavaScript

application.yml

JavaScript

pom.xml

JavaScript

The serviceis starting and this is the console log:

JavaScript

But when I try to call the endpoint http://localhost:8080/services/javadsl/weather/london

It looks like this endpoint doesn’t exist, the rest isn’t created. I used debugger and the method getWeatherData() isn’t called. And I think this log is not ok: Started route1 (rest://get:javadsl/weather/%7Bcity%7D), it should be something like that: route1 started and consuming from servlet:/javadsl/weather/%7Bcity%7D And the tutorial is from here: https://www.youtube.com/watch?v=spDjbC8mZf0&t=433s Thank you in advance!

Advertisement

Answer

You can start by creating example project using official camel-archetype-spring-boot maven archetype. You can generate project based on the archetype using your IDE or from command-line using following maven command.

JavaScript

Now in the maven project file pom.xml add following block to the dependencies

JavaScript

Note that version doesn’t need to be specified as it is provided by camel-spring-boot-dependencies BOM (bill of materials) in dependencyManagement section.

After that you can create new file ConfigureCamelContext.java where we can configure our CamelContext and provide it with RestConfiguration. We can do this by implementing CamelContextConfiguration and @Component annotation.

The annotation tells spring-framework that it should create instance of the class and inject it to objects that request it based on its class-type or interface. Camel is configured to automatically ask spring-framework for these RestConfiguration instances which it will proceed to use configure CamelContext.

JavaScript

This should work with RestDSL and rest-component.

Edit MySpringBootRouter.java to something like this:

JavaScript

Comment out the class under src/test/java/<grouId>/ as the example test provided by the archetype is not applicable for for MySpringBootRouter and will interrupt build.

To run the project you can use command

JavaScript

Now you should be able to open up localhost:8081/hello, localhost:8081/hello/<Name> or localhost:8081/test get response in plain text.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement