Skip to content

Commit 7327666

Browse files
committed
Updated the code
1 parent 41b2a45 commit 7327666

File tree

6 files changed

+275
-22
lines changed

6 files changed

+275
-22
lines changed

other-resources/Spring_WebFlux_MongoDB.postman_collection.json

+133-1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,42 @@
4141
},
4242
"response": []
4343
},
44+
{
45+
"name": "CREATE Contact - functional",
46+
"request": {
47+
"method": "POST",
48+
"header": [
49+
{
50+
"key": "Content-Type",
51+
"type": "text",
52+
"value": "application/json"
53+
}
54+
],
55+
"body": {
56+
"mode": "raw",
57+
"raw": "{\n \"email\": \"[email protected]\",\n \"name\": \"Test from Postman\",\n \"phone\": \"110000000000\"\n}"
58+
},
59+
"url": {
60+
"raw": "{{baseURL}}/functional/contacts",
61+
"host": [
62+
"{{baseURL}}"
63+
],
64+
"path": [
65+
"functional",
66+
"contacts"
67+
],
68+
"query": [
69+
{
70+
"key": "",
71+
"value": null,
72+
"disabled": true
73+
}
74+
]
75+
},
76+
"description": "Create a Contact Object"
77+
},
78+
"response": []
79+
},
4480
{
4581
"name": "UPDATE Contact",
4682
"request": {
@@ -69,6 +105,34 @@
69105
},
70106
"response": []
71107
},
108+
{
109+
"name": "UPDATE Contact - functional",
110+
"request": {
111+
"method": "PUT",
112+
"header": [],
113+
"body": {
114+
"mode": "raw",
115+
"raw": "{\"id\":\"5f14887390baf8178f4b9283\",\"name\":\"Test from Postman updated\",\"email\":\"[email protected]\",\"phone\":\"110000000000\"}",
116+
"options": {
117+
"raw": {
118+
"language": "json"
119+
}
120+
}
121+
},
122+
"url": {
123+
"raw": "{{baseURL}}/functional/contacts/5f14887390baf8178f4b9283",
124+
"host": [
125+
"{{baseURL}}"
126+
],
127+
"path": [
128+
"functional",
129+
"contacts",
130+
"5f14887390baf8178f4b9283"
131+
]
132+
}
133+
},
134+
"response": []
135+
},
72136
{
73137
"name": "DELETE Contact",
74138
"request": {
@@ -88,6 +152,25 @@
88152
},
89153
"response": []
90154
},
155+
{
156+
"name": "DELETE Contact - functional",
157+
"request": {
158+
"method": "DELETE",
159+
"header": [],
160+
"url": {
161+
"raw": "{{baseURL}}/functional/contacts/5f14887390baf8178f4b9283",
162+
"host": [
163+
"{{baseURL}}"
164+
],
165+
"path": [
166+
"functional",
167+
"contacts",
168+
"5f14887390baf8178f4b9283"
169+
]
170+
}
171+
},
172+
"response": []
173+
},
91174
{
92175
"name": "GET by Id Contact",
93176
"request": {
@@ -113,6 +196,31 @@
113196
},
114197
"response": []
115198
},
199+
{
200+
"name": "GET by Id Contact - functional",
201+
"request": {
202+
"method": "GET",
203+
"header": [
204+
{
205+
"key": "Accept",
206+
"value": "application/json",
207+
"type": "text"
208+
}
209+
],
210+
"url": {
211+
"raw": "{{baseURL}}/functional/contacts/5f10619b385b012854646c59",
212+
"host": [
213+
"{{baseURL}}"
214+
],
215+
"path": [
216+
"functional",
217+
"contacts",
218+
"5f10619b385b012854646c59"
219+
]
220+
}
221+
},
222+
"response": []
223+
},
116224
{
117225
"name": "GET all Contacts",
118226
"request": {
@@ -137,6 +245,30 @@
137245
}
138246
},
139247
"response": []
248+
},
249+
{
250+
"name": "GET all Contacts - functional",
251+
"request": {
252+
"method": "GET",
253+
"header": [
254+
{
255+
"key": "Accept",
256+
"type": "text",
257+
"value": "application/json"
258+
}
259+
],
260+
"url": {
261+
"raw": "{{baseURL}}/functional/contacts",
262+
"host": [
263+
"{{baseURL}}"
264+
],
265+
"path": [
266+
"functional",
267+
"contacts"
268+
]
269+
}
270+
},
271+
"response": []
140272
}
141273
],
142274
"event": [
@@ -163,7 +295,7 @@
163295
],
164296
"variable": [
165297
{
166-
"id": "98d97b59-b13f-401d-8fb8-aaaa965345f8",
298+
"id": "407f1d83-0ca1-4362-9dfd-bc16fb76f99d",
167299
"key": "baseURL",
168300
"value": "http://localhost:8083"
169301
}

webflux-02-mongo-rest-api/pom.xml

+7-6
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,6 @@
3232
<version>4.1.4</version>
3333
</dependency>
3434

35-
<!--Swagger dependencies -->
36-
<!--<dependency>
37-
<groupId>io.springfox</groupId>
38-
<artifactId>springfox-boot-starter</artifactId>
39-
<version>3.0.0</version>
40-
</dependency>-->
4135
<dependency>
4236
<groupId>org.springframework.boot</groupId>
4337
<artifactId>spring-boot-starter-test</artifactId>
@@ -62,6 +56,13 @@
6256
<optional>true</optional>
6357
<scope>runtime</scope>
6458
</dependency>
59+
60+
<!--Swagger dependencies - http://localhost:8083/swagger-ui/#/contact-controller -->
61+
<dependency>
62+
<groupId>io.springfox</groupId>
63+
<artifactId>springfox-boot-starter</artifactId>
64+
<version>3.0.0</version>
65+
</dependency>
6566
</dependencies>
6667

6768
<repositories>

webflux-02-mongo-rest-api/src/main/java/c/jbd/webflux/functional/ContactRestHandler.java

+19-11
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
import org.springframework.web.reactive.function.server.ServerResponse;
1111
import reactor.core.publisher.Mono;
1212

13+
import static org.springframework.web.reactive.function.BodyInserters.*;
14+
1315
@Component
1416
public class ContactRestHandler {
1517
private final ContactRepository contactRepository;
@@ -25,45 +27,50 @@ public ContactRestHandler(ContactRepository contactRepository) {
2527
this.contactRepository = contactRepository;
2628
}
2729

28-
public Mono<ServerResponse> getAllContacts(ServerRequest request) {
29-
return ServerResponse.ok()
30-
.contentType(MediaType.APPLICATION_JSON)
31-
.body(contactRepository.findAll(), Contact.class);
32-
}
33-
30+
//GET - find a contact by id
3431
public Mono<ServerResponse> getById(ServerRequest request) {
3532
String id = request.pathVariable("id");
3633

3734
return contactRepository.findById(id)
3835
.flatMap(contact ->
3936
ServerResponse.ok()
4037
.contentType(MediaType.APPLICATION_JSON)
41-
.body(contact, Contact.class)
38+
.body(fromValue(contact))
4239
).switchIfEmpty(response404);
4340
}
4441

42+
//List all contacts
43+
public Mono<ServerResponse> getAllContacts(ServerRequest request) {
44+
return ServerResponse.ok()
45+
.contentType(MediaType.APPLICATION_JSON)
46+
.body(contactRepository.findAll(), Contact.class);
47+
}
48+
49+
//Find a Contact by email address.
4550
public Mono<ServerResponse> getByEmail(ServerRequest request) {
4651
String email = request.pathVariable("email");
4752

4853
return contactRepository.findFirstByEmail(email)
4954
.flatMap(contact ->
5055
ServerResponse.ok()
5156
.contentType(MediaType.APPLICATION_JSON)
52-
.body(contact, Contact.class)
57+
.body(fromValue(contact))
5358
).switchIfEmpty(response404);
5459
}
5560

61+
//Save a new Contact
5662
public Mono<ServerResponse> insertContact(ServerRequest request) {
5763
Mono<Contact> unsavedContact = request.bodyToMono(Contact.class);
5864

5965
return unsavedContact
6066
.flatMap(contact -> contactRepository.save(contact)
6167
.flatMap(savedContact -> ServerResponse.accepted()
6268
.contentType(MediaType.APPLICATION_JSON)
63-
.body(savedContact, Contact.class))
69+
.body(fromValue(savedContact)))
6470
).switchIfEmpty(response406);
6571
}
6672

73+
//Update an existing contact
6774
public Mono<ServerResponse> updateContact(ServerRequest request) {
6875
Mono<Contact> contact$ = request.bodyToMono(Contact.class);
6976
String id = request.pathVariable("id");
@@ -83,11 +90,12 @@ public Mono<ServerResponse> updateContact(ServerRequest request) {
8390
return updatedContact$.flatMap(contact ->
8491
ServerResponse.accepted()
8592
.contentType(MediaType.APPLICATION_JSON)
86-
.body(contact, Contact.class)
93+
.body(fromValue(contact))
8794
).switchIfEmpty(response404);
8895
}
8996

90-
public Mono<ServerResponse> deleteContact(ServerRequest request){
97+
//Delete a Contact
98+
public Mono<ServerResponse> deleteContact(ServerRequest request) {
9199
String id = request.pathVariable("id");
92100
Mono<Void> deleted = contactRepository.deleteById(id);
93101

webflux-02-mongo-rest-api/src/main/java/c/jbd/webflux/functional/ContactRestRouter.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public class ContactRestRouter {
1414
@Bean
1515
public RouterFunction<ServerResponse> routeContact(ContactRestHandler contactRestHandler) {
1616
return RouterFunctions
17-
.route(GET("/functional/contacts/")
17+
.route(GET("/functional/contacts")
1818
, contactRestHandler::getAllContacts)
1919
.andRoute(GET("/functional/contacts/{id}")
2020
, contactRestHandler::getById)

webflux-02-mongo-rest-api/src/main/resources/application.yml

+6-3
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,17 @@ server:
1717
port: 8083
1818
---
1919
spring:
20-
profiles: uat
20+
profiles: unit_test
2121
data:
2222
mongodb:
2323
host: localhost
2424
port: 27017
25-
database: local
25+
database: spring_reactive_test
26+
username: root
27+
password: root@123
28+
authentication-database: admin
2629
server:
27-
port: 8080
30+
port: 8083
2831
#---
2932
#spring:
3033
# profiles: prod

0 commit comments

Comments
 (0)