Skip to content

Commit 6bf006d

Browse files
authored
Merge pull request #2 from tutorialworks/renovate/camel.version
Update dependency org.apache.camel.springboot:camel-spring-boot-dependencies to v3.19.0
2 parents cac0c97 + d6ee93b commit 6bf006d

File tree

6 files changed

+140
-29
lines changed

6 files changed

+140
-29
lines changed

README.md

+62-10
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
1-
# Camel Sports API (JSON in REST services)
1+
# Using JSON in REST services in Apache Camel
22

3-
This is a demo that shows how to use Apache Camel on Spring Boot to expose a REST service.
3+
This is a demo that shows how to use Apache Camel on Spring Boot to expose a REST service with JSON payloads.
44

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.
5+
It shows how to use Camel's _REST binding mode_ to automatically convert POJOs (Plain Old Java Objects) into JSON in the response. In Camel, JSON support is provided by the `camel-jackson-starter` dependency.
66

77
For more info, check out the accompanying YouTube video and blog post!
88

9-
- [Video: Adding JSON to my REST API (YouTube)][youtube]
9+
- [Video: Adding JSON to my REST API (YouTube)][youtube] - This example has been slightly modified (some might say improved!) from the YouTube video.
1010
- [Blog: Create a REST service in Apache Camel (tomd.xyz)][blog]
1111

12+
And for more tutorials check out <https://www.tutorialworks.com>.
13+
1214
## To run
1315

16+
You'll need [Apache Maven][maven] installed. Then run:
17+
1418
mvn clean spring-boot:run
1519

1620
And the service will be accessible at:
@@ -19,17 +23,65 @@ And the service will be accessible at:
1923

2024
## To test
2125

22-
To test with `curl`:
26+
You can use an API testing tool like `curl` to test the GET operation of the service:
27+
28+
```bash
29+
curl http://localhost:8080/services/go-sports
30+
```
31+
32+
Which should return a response like this, showing all the sports in the repository. You can format the output nicely using a tool like [jq][jq]:
33+
34+
```json
35+
[
36+
{
37+
"name": "American Football",
38+
"players": 11,
39+
"league": "NFL"
40+
},
41+
{
42+
"name": "Basketball",
43+
"players": 5,
44+
"league": "NBA"
45+
},
46+
{
47+
"name": "Baseball",
48+
"players": 9,
49+
"league": "MLB"
50+
},
51+
{
52+
"name": "Volleyball",
53+
"players": 6,
54+
"league": "NVA"
55+
}
56+
]
57+
```
58+
59+
To add a new Sport to the database with `curl`, send a POST request:
2360

24-
curl -H 'Content-Type: application/json' -X POST --data '{ "name": "Sportsball" }' http://localhost:8080/services/go-sports
61+
curl -H 'Content-Type: application/json' -X POST --data '{ "name": "Football", "players": 11, "league": "Premier League" }' http://localhost:8080/services/go-sports
2562

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.
63+
If you're using another REST testing tool like [Insomnia][insomnia], make sure that it includes the header `Content-Type: application/json` in the request.
2764

28-
## Changing the URL
65+
## Changing the base API URL
2966

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`:
67+
If you don't like `/services/` and you want to change the root URL used for all Camel servlet endpoints (which includes REST services like this one) then edit this line in `application.properties`:
3168

32-
camel.component.servlet.mapping.context-path=/services/*
69+
camel.servlet.mapping.context-path=/services/*
70+
71+
## That's it!
72+
73+
That's all there is to it. You can now use the REST service to add and retrieve sports in JSON format.
74+
75+
We're keeping it simple here, using a simple Java object to store the sports. But in real usage you might swap out the `SportRepository` class with a different class to store and retrieve the data from a database.
76+
77+
## License
78+
79+
This project is licensed under the Apache License, Version 2.0. See the [LICENSE][license] file for more info.
3380

3481
[youtube]: https://www.youtube.com/watch?v=YpVVXDnZLPo
3582
[blog]: https://tomd.xyz/camel-rest/
83+
[maven]: https://maven.apache.org/
84+
[jq]: https://stedolan.github.io/jq/
85+
[insomnia]: https://insomnia.rest/
86+
[license]: LICENSE
87+

pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
<properties>
1515
<java.version>11</java.version>
16-
<camel.version>3.9.0</camel.version>
16+
<camel.version>3.19.0</camel.version>
1717
<spring.boot-version>2.4.4</spring.boot-version>
1818

1919
<maven.compiler.source>1.8</maven.compiler.source>

src/main/java/com/tutorialworks/camel/sportsapi/CamelSportsRouteBuilder.java

+18-7
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,15 @@
44
import org.apache.camel.Processor;
55
import org.apache.camel.builder.RouteBuilder;
66
import org.apache.camel.model.rest.RestBindingMode;
7+
import org.springframework.beans.factory.annotation.Autowired;
78
import org.springframework.stereotype.Component;
89

910
@Component
1011
public class CamelSportsRouteBuilder extends RouteBuilder {
1112

13+
@Autowired
14+
SportRepository sportRepository;
15+
1216
@Override
1317
public void configure() throws Exception {
1418
restConfiguration()
@@ -19,15 +23,20 @@ public void configure() throws Exception {
1923
.path("/go-sports")
2024

2125
.get()
22-
.route()
23-
.transform(simple("I'm your resource " +
24-
"for all the sports!"))
25-
.endRest()
26+
.to("direct:getSports")
2627

2728
.post()
2829
.type(Sport.class)
2930
.outType(SportResponse.class)
30-
.route()
31+
.to("direct:postSport");
32+
33+
34+
from("direct:getSports")
35+
// Get all the sports from the repository
36+
// Camel will marshal to JSON automatically!
37+
.bean(sportRepository, "getSports");
38+
39+
from("direct:postSport")
3140
.to("log:mylogger?showAll=true")
3241

3342
// An inline processor to generate the response
@@ -37,16 +46,18 @@ public void process(Exchange exchange) throws Exception {
3746
// Get the Body as an (unmarshalled!) Sport POJO
3847
Sport sport = exchange.getMessage().getBody(Sport.class);
3948

49+
sportRepository.addSport(sport);
50+
4051
// Build up the response using our SportResponse class
4152
SportResponse response = new SportResponse();
4253
response.setMessage("Thanks for submitting "
4354
+ sport.getName());
4455

4556
// Pop the response back in the body
57+
// Camel will marshal to JSON automatically!
4658
exchange.getMessage().setBody(response);
4759
}
48-
})
49-
.endRest();
60+
});
5061
}
5162

5263
}

src/main/java/com/tutorialworks/camel/sportsapi/Sport.java

+28-10
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,17 @@
33
public class Sport {
44

55
private String name;
6-
private int teamMembers;
7-
private String venue;
6+
private int players;
7+
private String league;
8+
9+
public Sport() {
10+
}
11+
12+
public Sport(String name, int players, String league) {
13+
this.name = name;
14+
this.players = players;
15+
this.league = league;
16+
}
817

918
public String getName() {
1019
return name;
@@ -14,19 +23,28 @@ public void setName(String name) {
1423
this.name = name;
1524
}
1625

17-
public int getTeamMembers() {
18-
return teamMembers;
26+
public int getPlayers() {
27+
return players;
28+
}
29+
30+
public void setPlayers(int players) {
31+
this.players = players;
1932
}
2033

21-
public void setTeamMembers(int teamMembers) {
22-
this.teamMembers = teamMembers;
34+
public String getLeague() {
35+
return league;
2336
}
2437

25-
public String getVenue() {
26-
return venue;
38+
public void setLeague(String league) {
39+
this.league = league;
2740
}
2841

29-
public void setVenue(String venue) {
30-
this.venue = venue;
42+
@Override
43+
public String toString() {
44+
return "Sport{" +
45+
"name='" + name + '\'' +
46+
", players=" + players +
47+
", league='" + league + '\'' +
48+
'}';
3149
}
3250
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.tutorialworks.camel.sportsapi;
2+
3+
import org.springframework.stereotype.Component;
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
@Component
9+
public class SportRepository {
10+
11+
private List<Sport> sports;
12+
13+
public SportRepository() {
14+
sports = new ArrayList<>();
15+
sports.add(new Sport("American Football", 11, "NFL"));
16+
sports.add(new Sport("Basketball", 5, "NBA"));
17+
sports.add(new Sport("Baseball", 9, "MLB"));
18+
sports.add(new Sport("Volleyball", 6, "NVA"));
19+
20+
}
21+
22+
public List<Sport> getSports() {
23+
return sports;
24+
}
25+
26+
public void addSport(Sport sport) {
27+
sports.add(sport);
28+
}
29+
30+
}
+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11

22

33
# Modify this line if you want to change the root URL of the Camel servlet
4-
camel.component.servlet.mapping.context-path=/services/*
4+
camel.servlet.mapping.context-path=/services/*

0 commit comments

Comments
 (0)