Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
W
Web Flux Demo
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Lokesh Singh
Web Flux Demo
Commits
171e9d49
Commit
171e9d49
authored
Feb 25, 2023
by
Lokesh Singh
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
animal rest api added
parent
5839fb0e
Changes
12
Show whitespace changes
Inline
Side-by-side
Showing
12 changed files
with
168 additions
and
18 deletions
+168
-18
README.md
README.md
+1
-0
pom.xml
pom.xml
+10
-1
MongoConfig.java
src/main/java/com/lokesh/webfluxdemo/config/MongoConfig.java
+23
-0
AnimalController.java
...a/com/lokesh/webfluxdemo/controller/AnimalController.java
+27
-0
EmployeeController.java
...com/lokesh/webfluxdemo/controller/EmployeeController.java
+15
-15
ExampleHandler.java
.../com/lokesh/webfluxdemo/functionalapi/ExampleHandler.java
+1
-1
ExampleRouter.java
...a/com/lokesh/webfluxdemo/functionalapi/ExampleRouter.java
+2
-1
Animal.java
src/main/java/com/lokesh/webfluxdemo/model/Animal.java
+20
-0
AnimalRepository.java
...a/com/lokesh/webfluxdemo/repository/AnimalRepository.java
+9
-0
AnimalResource.java
.../java/com/lokesh/webfluxdemo/resource/AnimalResource.java
+19
-0
DataIntializer.java
...ava/com/lokesh/webfluxdemo/sampledata/DataIntializer.java
+37
-0
application.yml
src/main/resources/application.yml
+4
-0
No files found.
README.md
View file @
171e9d49
...
...
@@ -2,4 +2,5 @@
learning exercise repository.
## Spring boot
differnce between reactive stack and servlet stack

pom.xml
View file @
171e9d49
...
...
@@ -21,7 +21,16 @@
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-webflux
</artifactId>
</dependency>
<!-- No need extra configuration class, Spring Boot will enable reactive support for MongoDB in this project.
ReactiveMongoTemplate and ReactiveMongoRepository will be configured automatically.-->
<dependency>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-data-mongodb-reactive
</artifactId>
</dependency>
<dependency>
<groupId>
org.projectlombok
</groupId>
<artifactId>
lombok
</artifactId>
</dependency>
<dependency>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-test
</artifactId>
...
...
src/main/java/com/lokesh/webfluxdemo/config/MongoConfig.java
0 → 100644
View file @
171e9d49
package
com
.
lokesh
.
webfluxdemo
.
config
;
import
com.mongodb.reactivestreams.client.MongoClient
;
import
com.mongodb.reactivestreams.client.MongoClients
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.data.mongodb.config.AbstractReactiveMongoConfiguration
;
import
org.springframework.data.mongodb.repository.config.EnableReactiveMongoRepositories
;
@EnableReactiveMongoRepositories
(
basePackageClasses
=
{
MongoConfig
.
class
})
public
class
MongoConfig
extends
AbstractReactiveMongoConfiguration
{
@Value
(
"${mongo.uri}"
)
String
mongoUri
;
@Bean
public
MongoClient
mongoClient
()
{
return
MongoClients
.
create
();
}
@Override
protected
String
getDatabaseName
()
{
return
"animal"
;
}
}
src/main/java/com/lokesh/webfluxdemo/controller/AnimalController.java
0 → 100644
View file @
171e9d49
package
com
.
lokesh
.
webfluxdemo
.
controller
;
import
com.lokesh.webfluxdemo.model.Animal
;
import
com.lokesh.webfluxdemo.repository.AnimalRepository
;
import
com.lokesh.webfluxdemo.resource.AnimalResource
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.http.MediaType
;
import
org.springframework.web.bind.annotation.GetMapping
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.RestController
;
import
reactor.core.publisher.Flux
;
import
java.time.Duration
;
@RestController
@RequestMapping
(
"/api/v1/animal"
)
public
class
AnimalController
{
@Autowired
private
AnimalRepository
animalRepository
;
@GetMapping
(
produces
=
MediaType
.
APPLICATION_STREAM_JSON_VALUE
)
public
Flux
<
AnimalResource
>
findAll
()
{
return
animalRepository
.
findAll
()
.
map
(
AnimalResource:
:
new
)
.
delayElements
(
Duration
.
ofSeconds
(
1L
));
}
}
src/main/java/com/lokesh/webfluxdemo/controller/EmployeeController.java
View file @
171e9d49
...
...
@@ -12,19 +12,19 @@ import reactor.core.publisher.Mono;
@RestController
@RequestMapping
(
"/employees"
)
public
class
EmployeeController
{
private
final
EmployeeRepository
employeeRepository
;
public
EmployeeController
(
EmployeeRepository
employeeRepository
)
{
this
.
employeeRepository
=
employeeRepository
;
}
@GetMapping
(
"/{id}"
)
private
Mono
<
Employee
>
getEmployeeById
(
@PathVariable
String
id
)
{
return
employeeRepository
.
findEmployeeById
(
id
);
}
@GetMapping
private
Flux
<
Employee
>
getAllEmployees
()
{
return
employeeRepository
.
findAllEmployees
();
}
//
private final EmployeeRepository employeeRepository;
//
//
public EmployeeController(EmployeeRepository employeeRepository) {
//
this.employeeRepository = employeeRepository;
//
}
//
//
@GetMapping("/{id}")
//
private Mono<Employee> getEmployeeById(@PathVariable String id) {
//
return employeeRepository.findEmployeeById(id);
//
}
//
//
@GetMapping
//
private Flux<Employee> getAllEmployees() {
//
return employeeRepository.findAllEmployees();
//
}
}
src/main/java/com/lokesh/webfluxdemo/ExampleHandler.java
→
src/main/java/com/lokesh/webfluxdemo/
functionalapi/
ExampleHandler.java
View file @
171e9d49
package
com
.
lokesh
.
webfluxdemo
;
package
com
.
lokesh
.
webfluxdemo
.
functionalapi
;
import
org.springframework.http.MediaType
;
import
org.springframework.stereotype.Component
;
...
...
src/main/java/com/lokesh/webfluxdemo/ExampleRouter.java
→
src/main/java/com/lokesh/webfluxdemo/
functionalapi/
ExampleRouter.java
View file @
171e9d49
package
com
.
lokesh
.
webfluxdemo
;
package
com
.
lokesh
.
webfluxdemo
.
functionalapi
;
import
com.lokesh.webfluxdemo.functionalapi.ExampleHandler
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.http.MediaType
;
...
...
src/main/java/com/lokesh/webfluxdemo/model/Animal.java
0 → 100644
View file @
171e9d49
package
com
.
lokesh
.
webfluxdemo
.
model
;
import
lombok.*
;
import
org.springframework.data.annotation.Id
;
import
org.springframework.data.mongodb.core.mapping.Document
;
@Data
@Builder
@Document
@ToString
@NoArgsConstructor
@AllArgsConstructor
public
class
Animal
{
@Id
private
String
id
;
private
String
name
;
private
String
kingdom
;
}
src/main/java/com/lokesh/webfluxdemo/repository/AnimalRepository.java
0 → 100644
View file @
171e9d49
package
com
.
lokesh
.
webfluxdemo
.
repository
;
import
com.lokesh.webfluxdemo.model.Animal
;
import
org.springframework.data.mongodb.repository.ReactiveMongoRepository
;
import
org.springframework.stereotype.Repository
;
@Repository
public
interface
AnimalRepository
extends
ReactiveMongoRepository
<
Animal
,
String
>
{
}
src/main/java/com/lokesh/webfluxdemo/resource/AnimalResource.java
0 → 100644
View file @
171e9d49
package
com
.
lokesh
.
webfluxdemo
.
resource
;
import
com.lokesh.webfluxdemo.model.Animal
;
import
lombok.Data
;
@Data
public
class
AnimalResource
{
private
String
id
;
private
String
name
;
private
String
kingdom
;
public
AnimalResource
(
Animal
animal
)
{
this
.
id
=
animal
.
getId
();
this
.
name
=
animal
.
getName
();
this
.
kingdom
=
animal
.
getKingdom
();
}
}
src/main/java/com/lokesh/webfluxdemo/sampledata/DataIntializer.java
0 → 100644
View file @
171e9d49
package
com
.
lokesh
.
webfluxdemo
.
sampledata
;
import
com.lokesh.webfluxdemo.model.Animal
;
import
com.lokesh.webfluxdemo.repository.AnimalRepository
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.boot.CommandLineRunner
;
import
org.springframework.stereotype.Component
;
import
reactor.core.publisher.Flux
;
@Component
@Slf4j
public
class
DataIntializer
implements
CommandLineRunner
{
private
final
AnimalRepository
animalRepository
;
public
DataIntializer
(
AnimalRepository
animalRepository
){
this
.
animalRepository
=
animalRepository
;
}
@Override
public
void
run
(
String
[]
args
)
{
log
.
info
(
"start data initialization..."
);
this
.
animalRepository
.
deleteAll
()
.
thenMany
(
Flux
.
just
(
"Cat"
,
"Dog"
,
"Horse"
,
"Cow"
)
.
flatMap
(
name
->
this
.
animalRepository
.
save
(
Animal
.
builder
().
name
(
name
).
kingdom
(
"Vertebrate"
).
build
())
)
)
.
log
()
.
subscribe
(
null
,
null
,
()
->
log
.
info
(
"done initialization..."
)
);
}
}
src/main/resources/application.yml
0 → 100644
View file @
171e9d49
spring
:
data
:
mongodb
:
uri
:
mongodb://localhost:27017/animal
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment