Skip to content

Commit 3c01dad

Browse files
House-keep + docs updates
1 parent cf32f12 commit 3c01dad

File tree

9 files changed

+63
-61
lines changed

9 files changed

+63
-61
lines changed

Dockerfile

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1+
# Author: Giri Jeedigunta
2+
# Git: https://github.com/giri-jeedigunta/java-spring-mongo-starter-reactive
3+
14
FROM openjdk:14-jdk-alpine
25

36
WORKDIR /app
47

5-
ADD build/libs/*.jar ReactiveRESTStarterApp-1.0.jar
8+
ADD build/libs/*.jar ReactiveRESTfulStarterApp-1.0.jar
69

7-
EXPOSE 8083
10+
EXPOSE 4000
811

912
ENV JAVA_XMS=-Xms1024m
1013
ENV JAVA_XMX=-Xmx1024m

README.md

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,56 @@
1-
# REST starter App with Java Spring-Boot, Gradle, MongoDB, Webflux
2-
This is a simple Java Reactive Spring boot starter app with MondoDB and Web Flux. This starter app have minimal code taking advantage of the spring framework and the insanely cool reactive dependencies !!!
1+
# RESTful API server app with Java Spring Reactive Framework
2+
A light-weight RESTful API server starter app built with Java Spring Reactive Framework, MongoDB, WebFlux and Gradle.
33

44
### Running the App with Docker
55
Use the below to run mongoDB and the app on docker containers.
66
```
7+
./gradlew clean build
78
docker-compose up
89
```
910

1011
### Running the App without Docker
1112
For this you will need to set up the MongoDB Locally on your machine(Refer the Local MongoDB Setup section towards the end.).
1213
Add the configurations in ```application.properties```
1314
```
15+
./gradlew clean build
1416
./gradlew bootRun
1517
```
1618

17-
### CRUD - Use cURL to access / test the REST APIs.
19+
### Try the CRUD - Use cURL to access / test the APIs.
1820
Once the server is running try the following APIs:
1921

2022

21-
GET /contacts/get
23+
GET /api/get
2224
```
23-
curl http://localhost:8083/contacts/get
25+
curl http://localhost:4000/api/get
2426
```
2527

26-
GET /contacts/get/{email}
28+
GET /api/get/{email}
2729

2830
```
29-
curl http://localhost:8083/contacts/get/{email}
31+
curl http://localhost:4000/api/get/{email}
3032
```
3133

32-
POST /contacts/add
34+
POST /api/add
3335

3436
```
35-
curl -X POST -d '{"name":"Giri Jeedigunta","email":"giri@yopmail.com"}' -H "Content-Type: application/json" http://localhost:8083/contacts/add -v
37+
curl -X POST -d '{"name":"Giri Jeedigunta","email":"giri@yopmail.com"}' -H "Content-Type: application/json" http://localhost:4000/api/add -v
3638
```
3739

38-
POST /contacts/udpate/{id}
40+
POST /api/udpate/{id}
3941

4042
```
41-
curl -X POST -d '{"name":"Giri J","email":"giri@giri.com"}' -H "Content-Type: application/json" http://localhost:8083/contacts/update/5f85c2c8b0abe7062019dd16
43+
curl -X POST -d '{"name":"Giri J","email":"giri@giri.com"}' -H "Content-Type: application/json" http://localhost:4000/api/update/5f85c2c8b0abe7062019dd16
4244
```
4345

44-
POST /contacts/delete/{id}
46+
POST /api/delete/{id}
4547

4648
```
47-
curl -X POST http://localhost:8083/contacts/delete/5f85c2c8b0abe7062019dd16
49+
curl -X POST http://localhost:4000/api/delete/5f85c2c8b0abe7062019dd16
4850
```
4951

5052
### Local MongoDB Setup(Mac OS):
51-
Use these steps if you are not familiar with docker.
53+
If you are not familiar with docker. Follow these steps to setup MongoDB on your machine locally. Here I have instructions for Mac OS.
5254

5355
#### Easy MongoDB Installation:
5456
```
@@ -74,16 +76,16 @@ db.createUser({
7476
user: "giri",
7577
pwd: "spring2020",
7678
roles:[
77-
{role: "dbAdmin", db:"springContacts"}
79+
{role: "dbAdmin", db:"springapi"}
7880
]
7981
});
8082
```
8183

8284
##### Creating New Collection:
8385
```
84-
use springContacts;
86+
use springapi;
8587
86-
db.createCollection( "contacts", {
88+
db.createCollection( "api", {
8789
validator: { $jsonSchema: {
8890
bsonType: "object",
8991
required: [ "name" ],
@@ -103,15 +105,15 @@ db.createCollection( "contacts", {
103105

104106
#####: Sample Data
105107
```
106-
db.contacts.insert({"name":"Giri Jeedigunta","email":"hello@spring.com"});
108+
db.api.insert({"name":"Giri Jeedigunta","email":"hello@spring.com"});
107109
```
108110

109111
#### Adding Configuration to Java App:
110-
Add all the MongoDB related config in the following format to ```application.properties```
112+
Add all the MongoDB related config in a simple URI format like below in the ```application.properties``` file.
111113

112114
```
113115
spring.data.mongodb.uri=mongodb://<Add_Your_UserName>:<Your_Password>@localhost:27017/<DB_NAME>?authSource=admin
114116
115117
// Example:
116-
spring.data.mongodb.uri=mongodb://giri:spring2020@localhost:27017/springContacts?authSource=admin
118+
spring.data.mongodb.uri=mongodb://giri:spring2020@localhost:27017/springapi?authSource=admin
117119
```

docker-compose.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# Author: Giri Jeedigunta
2+
# Git: https://github.com/giri-jeedigunta/java-spring-mongo-starter-reactive
3+
14
version: '3.7'
25

36
services:

settings.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
rootProject.name = 'ReactiveRESTStarterApp'
1+
rootProject.name = 'ReactiveRESTfulStarterApp'

src/main/java/com/restful/services/reactiveMongoStarter/repositories/ContactsRepository.java

Lines changed: 0 additions & 13 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
package com.restful.services.reactiveMongoStarter;
1+
package com.restful.services.springReactiveRESTApp;
22

33
import org.springframework.boot.SpringApplication;
44
import org.springframework.boot.autoconfigure.SpringBootApplication;
5-
import org.springframework.context.ApplicationContext;
65
import org.springframework.data.mongodb.repository.config.EnableReactiveMongoRepositories;
76
/*
87
Author: Giri Jeedigunta
@@ -11,8 +10,8 @@
1110

1211
@SpringBootApplication
1312
@EnableReactiveMongoRepositories
14-
public class ReactiveRESTStarterApp {
13+
public class Application {
1514
public static void main(String[] args) {
16-
SpringApplication.run(ReactiveRESTStarterApp.class, args);
15+
SpringApplication.run(Application.class, args);
1716
}
1817
}
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
package com.restful.services.reactiveMongoStarter.controller;
1+
package com.restful.services.springReactiveRESTApp.controller;
22

3-
import com.restful.services.reactiveMongoStarter.model.Contacts;
4-
import com.restful.services.reactiveMongoStarter.repositories.ContactsRepository;
3+
import com.restful.services.springReactiveRESTApp.model.Apis;
4+
import com.restful.services.springReactiveRESTApp.repositories.ApisRepository;
55
import lombok.extern.slf4j.Slf4j;
66
import org.springframework.beans.factory.annotation.Autowired;
77
import org.springframework.web.bind.annotation.*;
@@ -10,44 +10,44 @@
1010

1111
@RestController
1212
@Slf4j
13-
@RequestMapping("/contacts")
14-
public class ContactsAPIController {
13+
@RequestMapping("/api")
14+
public class RestAPIsController {
1515

1616
@Autowired
17-
private ContactsRepository contactsRepo;
17+
private ApisRepository apiRepo;
1818

1919
/* Sends All the data availabe in the collection. */
2020
@GetMapping("/get")
21-
public Flux<Contacts> index() {
22-
return contactsRepo.findAll();
21+
public Flux<Apis> index() {
22+
return apiRepo.findAll();
2323
}
2424

2525
/* Sends the data matching the email. */
2626
@GetMapping("/get/{email}")
27-
public Mono<Contacts> getContactByEmail(@PathVariable("email") String email) {
28-
return contactsRepo.findByEmail(email);
27+
public Mono<Apis> getContactByEmail(@PathVariable("email") String email) {
28+
return apiRepo.findByEmail(email);
2929
}
3030

3131
/* Add new contact */
3232
@PostMapping(path = "/add", consumes = "application/json")
33-
public Mono<Contacts> addNewContact(@RequestBody Contacts contact) {
34-
return contactsRepo.save(contact);
33+
public Mono<Apis> addNewContact(@RequestBody Apis contact) {
34+
return apiRepo.save(contact);
3535
}
3636

3737
/* Update existing contact */
3838
@PostMapping(path = "/update/{id}", consumes = "application/json")
39-
public Mono<Contacts> updateContactById(@PathVariable("id") String id, @RequestBody Contacts contact) {
40-
return contactsRepo.findById(id)
39+
public Mono<Apis> updateContactById(@PathVariable("id") String id, @RequestBody Apis contact) {
40+
return apiRepo.findById(id)
4141
.flatMap(existingContact -> {
4242
existingContact.setName(contact.getName());
4343
existingContact.setEmail(contact.getEmail());
44-
return contactsRepo.save(existingContact);
44+
return apiRepo.save(existingContact);
4545
});
4646
}
4747

4848
/* Delete existing contact */
4949
@PostMapping(path = "/delete/{id}")
5050
public Mono<String> deleteContactById(@PathVariable("id") String id) {
51-
return contactsRepo.deleteById(id).thenReturn("Successfully deleted -> "+id);
51+
return apiRepo.deleteById(id).thenReturn("Successfully deleted -> "+id);
5252
}
5353
}

src/main/java/com/restful/services/reactiveMongoStarter/model/Contacts.java renamed to src/main/java/com/restful/services/springReactiveRESTApp/model/Apis.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
1-
package com.restful.services.reactiveMongoStarter.model;
1+
package com.restful.services.springReactiveRESTApp.model;
22

3-
import lombok.AllArgsConstructor;
43
import lombok.Data;
5-
import lombok.NoArgsConstructor;
6-
import lombok.ToString;
74
import org.springframework.data.annotation.Id;
85
import org.springframework.data.mongodb.core.mapping.Document;
96

107
@Document
118
@Data
12-
public class Contacts {
9+
public class Apis {
1310
@Id
1411
private String id;
1512

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.restful.services.springReactiveRESTApp.repositories;
2+
3+
import com.restful.services.springReactiveRESTApp.model.Apis;
4+
import org.springframework.data.mongodb.repository.ReactiveMongoRepository;
5+
import org.springframework.stereotype.Repository;
6+
import reactor.core.publisher.Mono;
7+
8+
@Repository
9+
public interface ApisRepository extends ReactiveMongoRepository<Apis, String> {
10+
Mono<Apis> findByEmail(String email);
11+
}

0 commit comments

Comments
 (0)