Skip to content

Commit c64d140

Browse files
committed
Store sports in an object, improve GET operation, update REST syntax
1 parent b66afef commit c64d140

File tree

5 files changed

+56
-26
lines changed

5 files changed

+56
-26
lines changed

Diff for: src/main/java/com/tutorialworks/camel/sportsapi/CamelSportsApiApplication.java

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import org.springframework.boot.SpringApplication;
44
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.context.annotation.Bean;
6+
import org.springframework.stereotype.Component;
57

68
@SpringBootApplication
79
public class CamelSportsApiApplication {

Diff for: 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
}

Diff for: 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
}

Diff for: src/main/java/com/tutorialworks/camel/sportsapi/SportRepository.java

+7-8
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,17 @@
66
import java.util.List;
77

88
@Component
9-
public class SportService {
9+
public class SportRepository {
1010

1111
private List<Sport> sports;
1212

13-
public SportService() {
13+
public SportRepository() {
1414
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"));
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+
2020
}
2121

2222
public List<Sport> getSports() {
@@ -27,5 +27,4 @@ public void addSport(Sport sport) {
2727
sports.add(sport);
2828
}
2929

30-
3130
}

Diff for: src/main/resources/application.properties

+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)