Skip to content

Commit e0c2602

Browse files
committed
Initial commit
0 parents  commit e0c2602

File tree

9 files changed

+279
-0
lines changed

9 files changed

+279
-0
lines changed

.gitignore

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
HELP.md
2+
target/
3+
!.mvn/wrapper/maven-wrapper.jar
4+
!**/src/main/**/target/
5+
!**/src/test/**/target/
6+
7+
### STS ###
8+
.apt_generated
9+
.classpath
10+
.factorypath
11+
.project
12+
.settings
13+
.springBeans
14+
.sts4-cache
15+
16+
### IntelliJ IDEA ###
17+
.idea
18+
*.iws
19+
*.iml
20+
*.ipr
21+
22+
### NetBeans ###
23+
/nbproject/private/
24+
/nbbuild/
25+
/dist/
26+
/nbdist/
27+
/.nb-gradle/
28+
build/
29+
!**/src/main/**/build/
30+
!**/src/test/**/build/
31+
32+
### VS Code ###
33+
.vscode/

README.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Camel Sports API (JSON in REST services)
2+
3+
This is a demo that shows how to use Apache Camel on Spring Boot to expose a REST service.
4+
5+
It also shows how to use Camel's _REST binding mode_ to automatically convert Java POJOs to JSON in the response. JSON support is provided by the `camel-jackson-starter` dependency.
6+
7+
For more info, check out the accompanying YouTube video and blog post!
8+
9+
- [Video: Adding JSON to my REST API (YouTube)][youtube]
10+
- [Blog: Create a REST service in Apache Camel (tomd.xyz)][blog]
11+
12+
## To run
13+
14+
mvn clean spring-boot:run
15+
16+
And the service will be accessible at:
17+
18+
http://localhost:8080/services/go-sports
19+
20+
## To test
21+
22+
To test with `curl`:
23+
24+
curl -H 'Content-Type: application/json' -X POST --data '{ "name": "Sportsball" }' http://localhost:8080/services/go-sports
25+
26+
If you're using another REST testing tool like Insomnia, just make sure it includes the header `Content-Type: application/json` in the request.
27+
28+
## Changing the URL
29+
30+
If you don't like `/services/` and you want to change the root URL used for all Camel servlet endpoints (e.g. REST services) then edit this line in `application.properties`:
31+
32+
camel.component.servlet.mapping.context-path=/services/*
33+
34+
[youtube]: https://www.youtube.com/watch?v=YpVVXDnZLPo
35+
[blog]: https://tomd.xyz/camel-rest/

pom.xml

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>com.tutorialworks</groupId>
7+
<artifactId>camel-sports-api</artifactId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
10+
<name>camel-sports-api</name>
11+
12+
<description>Camel REST API with JSON support</description>
13+
14+
<properties>
15+
<java.version>11</java.version>
16+
<camel.version>3.9.0</camel.version>
17+
<spring.boot-version>2.4.4</spring.boot-version>
18+
19+
<maven.compiler.source>1.8</maven.compiler.source>
20+
<maven.compiler.target>1.8</maven.compiler.target>
21+
</properties>
22+
23+
<dependencyManagement>
24+
<dependencies>
25+
<!-- Spring Boot BOM -->
26+
<dependency>
27+
<groupId>org.springframework.boot</groupId>
28+
<artifactId>spring-boot-dependencies</artifactId>
29+
<version>${spring.boot-version}</version>
30+
<type>pom</type>
31+
<scope>import</scope>
32+
</dependency>
33+
<!-- Camel BOM -->
34+
<dependency>
35+
<groupId>org.apache.camel.springboot</groupId>
36+
<artifactId>camel-spring-boot-dependencies</artifactId>
37+
<version>${camel.version}</version>
38+
<type>pom</type>
39+
<scope>import</scope>
40+
</dependency>
41+
</dependencies>
42+
</dependencyManagement>
43+
44+
<dependencies>
45+
<dependency>
46+
<groupId>org.springframework.boot</groupId>
47+
<artifactId>spring-boot-starter-web</artifactId>
48+
</dependency>
49+
<dependency>
50+
<groupId>org.springframework.boot</groupId>
51+
<artifactId>spring-boot-starter-actuator</artifactId>
52+
</dependency>
53+
54+
<dependency>
55+
<groupId>org.apache.camel.springboot</groupId>
56+
<artifactId>camel-spring-boot-starter</artifactId>
57+
</dependency>
58+
<dependency>
59+
<groupId>org.apache.camel.springboot</groupId>
60+
<artifactId>camel-servlet-starter</artifactId>
61+
</dependency>
62+
<dependency>
63+
<groupId>org.apache.camel.springboot</groupId>
64+
<artifactId>camel-jackson-starter</artifactId>
65+
</dependency>
66+
67+
<dependency>
68+
<groupId>org.springframework.boot</groupId>
69+
<artifactId>spring-boot-starter-test</artifactId>
70+
<scope>test</scope>
71+
</dependency>
72+
</dependencies>
73+
74+
<build>
75+
<plugins>
76+
<plugin>
77+
<groupId>org.springframework.boot</groupId>
78+
<artifactId>spring-boot-maven-plugin</artifactId>
79+
</plugin>
80+
</plugins>
81+
</build>
82+
83+
</project>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.tutorialworks.camel.sportsapi;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class CamelSportsApiApplication {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(CamelSportsApiApplication.class, args);
11+
}
12+
13+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.tutorialworks.camel.sportsapi;
2+
3+
import org.apache.camel.Exchange;
4+
import org.apache.camel.Processor;
5+
import org.apache.camel.builder.RouteBuilder;
6+
import org.apache.camel.model.rest.RestBindingMode;
7+
import org.springframework.stereotype.Component;
8+
9+
@Component
10+
public class CamelSportsRouteBuilder extends RouteBuilder {
11+
12+
@Override
13+
public void configure() throws Exception {
14+
restConfiguration()
15+
.component("servlet")
16+
.bindingMode(RestBindingMode.auto);
17+
18+
rest()
19+
.path("/go-sports")
20+
21+
.get()
22+
.route()
23+
.transform(simple("I'm your resource " +
24+
"for all the sports!"))
25+
.endRest()
26+
27+
.post()
28+
.type(Sport.class)
29+
.outType(SportResponse.class)
30+
.route()
31+
.to("log:mylogger?showAll=true")
32+
33+
// An inline processor to generate the response
34+
.process(new Processor() {
35+
@Override
36+
public void process(Exchange exchange) throws Exception {
37+
// Get the Body as an (unmarshalled!) Sport POJO
38+
Sport sport = exchange.getMessage().getBody(Sport.class);
39+
40+
// Build up the response using our SportResponse class
41+
SportResponse response = new SportResponse();
42+
response.setMessage("Thanks for submitting "
43+
+ sport.getName());
44+
45+
// Pop the response back in the body
46+
exchange.getMessage().setBody(response);
47+
}
48+
})
49+
.endRest();
50+
}
51+
52+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.tutorialworks.camel.sportsapi;
2+
3+
public class Sport {
4+
5+
private String name;
6+
private int teamMembers;
7+
private String venue;
8+
9+
public String getName() {
10+
return name;
11+
}
12+
13+
public void setName(String name) {
14+
this.name = name;
15+
}
16+
17+
public int getTeamMembers() {
18+
return teamMembers;
19+
}
20+
21+
public void setTeamMembers(int teamMembers) {
22+
this.teamMembers = teamMembers;
23+
}
24+
25+
public String getVenue() {
26+
return venue;
27+
}
28+
29+
public void setVenue(String venue) {
30+
this.venue = venue;
31+
}
32+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.tutorialworks.camel.sportsapi;
2+
3+
public class SportResponse {
4+
5+
private String message;
6+
7+
public String getMessage() {
8+
return message;
9+
}
10+
11+
public void setMessage(String message) {
12+
this.message = message;
13+
}
14+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
3+
# Modify this line if you want to change the root URL of the Camel servlet
4+
camel.component.servlet.mapping.context-path=/services/*
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.tutorialworks.camel.sportsapi;
2+
3+
import org.junit.jupiter.api.Test;
4+
import org.springframework.boot.test.context.SpringBootTest;
5+
6+
@SpringBootTest
7+
class CamelSportsApiApplicationTests {
8+
9+
@Test
10+
void contextLoads() {
11+
}
12+
13+
}

0 commit comments

Comments
 (0)