You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
4
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.
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.
6
6
7
7
For more info, check out the accompanying YouTube video and blog post!
8
8
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.
10
10
-[Blog: Create a REST service in Apache Camel (tomd.xyz)][blog]
11
11
12
+
And for more tutorials check out <https://www.tutorialworks.com>.
13
+
12
14
## To run
13
15
16
+
You'll need [Apache Maven][maven] installed. Then run:
17
+
14
18
mvn clean spring-boot:run
15
19
16
20
And the service will be accessible at:
@@ -19,17 +23,65 @@ And the service will be accessible at:
19
23
20
24
## To test
21
25
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:
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.
27
64
28
-
## Changing the URL
65
+
## Changing the base API URL
29
66
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`:
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.
0 commit comments