Skip to content

Commit b66afef

Browse files
committed
Update README
1 parent b2a99ec commit b66afef

File tree

2 files changed

+93
-10
lines changed

2 files changed

+93
-10
lines changed

README.md

Lines changed: 62 additions & 10 deletions
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+
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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 SportService {
10+
11+
private List<Sport> sports;
12+
13+
public SportService() {
14+
sports = new ArrayList<>();
15+
sports.add(new Sport("Football"));
16+
sports.add(new Sport("Basketball"));
17+
sports.add(new Sport("Baseball"));
18+
sports.add(new Sport("Hockey"));
19+
sports.add(new Sport("Soccer"));
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+
31+
}

0 commit comments

Comments
 (0)