Repository navigation
Expand file tree
/
Copy pathRestRoutes.java
More file actions
152 lines (126 loc) · 5.15 KB
/
Copy pathRestRoutes.java
File metadata and controls
152 lines (126 loc) · 5.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package com.travelstart.api;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.travelstart.api.handler.BookingHandler;
import com.travelstart.api.handler.ErrorHandler;
import com.travelstart.api.handler.LoggingHandler;
import com.travelstart.api.model.Booking;
import org.apache.camel.CamelContext;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.model.dataformat.JsonDataFormat;
import org.apache.camel.model.dataformat.JsonLibrary;
import org.apache.camel.model.rest.RestBindingMode;
import org.apache.camel.model.rest.RestParamType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class RestRoutes extends RouteBuilder {
@Autowired
private LoggingHandler loggingHandler;
@Autowired
private ErrorHandler errorHandler;
@Autowired
private CamelContext context;
@Autowired
private BookingHandler bookHandler;
@Override
public void configure() throws Exception {
// @formatter:off
restConfiguration("netty4-http")
.bindingMode(RestBindingMode.json)
.endpointProperty("nettySharedHttpServer", "#sharedNettyHttpServer")
.enableCORS(true)
//If you want to use your own headers, please add them to this list
.corsHeaderProperty("Access-Control-Allow-Headers", "Origin, Accept, X-Requested-With, Content-Type, Access-Control-Request-Method, Access-Control-Request-Headers")
.host(Boot.HOST)
.port(Boot.PORT)
// swagger docs
.apiContextPath("/api-doc")
.apiProperty("api.title", "REST Template API").apiProperty("api.version", "1.0")
// and enable CORS
.apiProperty("cors", "true")
.apiProperty("host", Boot.HOST + ":" + Boot.PORT)
;
// log requests
interceptFrom("*")
.to("log:headers?level=INFO&showHeaders=true&multiline=true")
.bean(loggingHandler, "logRequest")
;
// log responses
onCompletion()
.bean(loggingHandler, "logResponse")
;
// error handler
JsonDataFormat errorJson = new JsonDataFormat(JsonLibrary.Jackson);
errorJson.setPrettyPrint(true);
errorJson.setInclude(JsonInclude.Include.NON_NULL.toString());
onException(Exception.class)
.bean(errorHandler)
.handled(true)
.marshal(errorJson)
.stop()
;
// the REST API
rest().path("/api")
.produces("application/json")
.bindingMode(RestBindingMode.off)
.get("/ping")
.description("Checks if the API is healthy")
.responseMessage()
.code(200)
.message("If API is alive and well")
.endResponseMessage()
.responseMessage()
.code("!= 200")
.message("API not ok")
.endResponseMessage()
.route().transform(constant("{\"ok\":true}")).routeId("ping-get-route")
.endRest()
// booking put
.put("/booking")
.bindingMode(RestBindingMode.json)
.produces("application/json")
.description("create a booking")
.type(Booking.class)
.outType(String.class)
.responseMessage()
.code(200)
.message("if successful")
.responseModel(String.class)
.endResponseMessage()
.route().bean(bookHandler, "create")
.endRest()
.get("/booking/{id}")
.bindingMode(RestBindingMode.json)
.produces("application/json")
.description("retrieves a booking given the ID")
.outType(Booking.class)
.param()
.name("id").type(RestParamType.path).dataType("integer").required(true)
.endParam()
.responseMessage()
.code(200)
.message("booking retrieved")
.responseModel(Booking.class)
.endResponseMessage()
.route().bean(bookHandler, "retrieve")
.endRest()
.post("/booking/{id}")
.bindingMode(RestBindingMode.json)
.produces("application/json")
.description("update a booking given the id")
.type(Booking.class)
.outType(Integer.class)
.param()
.name("id").type(RestParamType.path).dataType("integer").required(true)
.endParam()
.responseMessage()
.code(200)
.message("if successful, returns the ID of the booking")
.responseModel(Integer.class)
.endResponseMessage()
.route().bean(bookHandler, "update")
.endRest()
;
// @formatter:on
}
}