diff --git a/src/main/java/com/reactive/examples/ReactiveSpringBootApplication.java b/src/main/java/com/reactive/examples/ReactiveSpringBootApplication.java index 3bf58cd..bb058ba 100644 --- a/src/main/java/com/reactive/examples/ReactiveSpringBootApplication.java +++ b/src/main/java/com/reactive/examples/ReactiveSpringBootApplication.java @@ -5,8 +5,8 @@ @SpringBootApplication public class ReactiveSpringBootApplication { - public static void main(String[] args){ - SpringApplication.run(ReactiveSpringBootApplication.class,args); + public static void main(String[] args) { + SpringApplication.run(ReactiveSpringBootApplication.class, args); } } diff --git a/src/main/java/com/reactive/examples/client/UserClient.java b/src/main/java/com/reactive/examples/client/UserClient.java index b16b060..6e5dda9 100644 --- a/src/main/java/com/reactive/examples/client/UserClient.java +++ b/src/main/java/com/reactive/examples/client/UserClient.java @@ -11,24 +11,25 @@ @Component @Slf4j public class UserClient { - private WebClient client = WebClient.create("http://localhost:8080"); - public Mono getUser(String userId){ - return client.get() + private final WebClient client = WebClient.create("http://localhost:8080"); + + public Mono getUser(String userId) { + return client.get() .uri("/users/{userId}", userId) .retrieve() .bodyToMono(User.class).log(" User fetched "); } - public Flux getAllUsers(){ + public Flux getAllUsers() { return client.get() .uri("/users") .exchange().flatMapMany(clientResponse -> clientResponse.bodyToFlux(User.class)).log("Users Fetched : "); } - public Mono createUser(User user){ + public Mono createUser(User user) { Mono userMono = Mono.just(user); return client.post().uri("/users").contentType(MediaType.APPLICATION_JSON) - .body(userMono,User.class).retrieve().bodyToMono(User.class).log("Created User : "); + .body(userMono, User.class).retrieve().bodyToMono(User.class).log("Created User : "); } diff --git a/src/main/java/com/reactive/examples/controller/UserClientController.java b/src/main/java/com/reactive/examples/controller/UserClientController.java index 96f31e6..3cd2aca 100644 --- a/src/main/java/com/reactive/examples/controller/UserClientController.java +++ b/src/main/java/com/reactive/examples/controller/UserClientController.java @@ -13,24 +13,27 @@ @RequestMapping("/client/users") public class UserClientController { - @Autowired - private UserClient userClient; + private final UserClient userClient; + + public UserClientController(UserClient userClient) { + this.userClient = userClient; + } @GetMapping("/{userId}") - public Mono> getUserById(@PathVariable String userId){ + public Mono> getUserById(@PathVariable String userId) { Mono user = userClient.getUser(userId); - return user.map( u -> ResponseEntity.ok(u)) + return user.map(u -> ResponseEntity.ok(u)) .defaultIfEmpty(ResponseEntity.notFound().build()); } @GetMapping - public Flux getAllUsers(){ + public Flux getAllUsers() { return userClient.getAllUsers(); } @PostMapping @ResponseStatus(HttpStatus.CREATED) - public Mono create(@RequestBody User user){ + public Mono create(@RequestBody User user) { return userClient.createUser(user); } diff --git a/src/main/java/com/reactive/examples/controller/UserController.java b/src/main/java/com/reactive/examples/controller/UserController.java index e21a2a7..6ca11f8 100644 --- a/src/main/java/com/reactive/examples/controller/UserController.java +++ b/src/main/java/com/reactive/examples/controller/UserController.java @@ -15,38 +15,41 @@ @RestController @RequestMapping("/users") public class UserController { -@Autowired -private UserService userService; + private final UserService userService; + + public UserController(UserService userService) { + this.userService = userService; + } @PostMapping @ResponseStatus(HttpStatus.CREATED) - public Mono create(@RequestBody User user){ + public Mono create(@RequestBody User user) { return userService.createUser(user); } @GetMapping - public Flux getAllUsers(){ + public Flux getAllUsers() { return userService.getAllUsers(); } @GetMapping("/{userId}") - public Mono> getUserById(@PathVariable Integer userId){ + public Mono> getUserById(@PathVariable Integer userId) { Mono user = userService.findById(userId); - return user.map( u -> ResponseEntity.ok(u)) + return user.map(u -> ResponseEntity.ok(u)) .defaultIfEmpty(ResponseEntity.notFound().build()); } @PutMapping("/{userId}") - public Mono> updateUserById(@PathVariable Integer userId, @RequestBody User user){ - return userService.updateUser(userId,user) + public Mono> updateUserById(@PathVariable Integer userId, @RequestBody User user) { + return userService.updateUser(userId, user) .map(updatedUser -> ResponseEntity.ok(updatedUser)) .defaultIfEmpty(ResponseEntity.badRequest().build()); } @DeleteMapping("/{userId}") - public Mono> deleteUserById(@PathVariable Integer userId){ + public Mono> deleteUserById(@PathVariable Integer userId) { return userService.deleteUser(userId) - .map( r -> ResponseEntity.ok().build()) + .map(r -> ResponseEntity.ok().build()) .defaultIfEmpty(ResponseEntity.notFound().build()); } @@ -61,7 +64,7 @@ public Flux fetchUsersByIds(@RequestBody List ids) { } @GetMapping("/{userId}/department") - public Mono fetchUserAndDepartment(@PathVariable Integer userId){ + public Mono fetchUserAndDepartment(@PathVariable Integer userId) { return userService.fetchUserAndDepartment(userId); } diff --git a/src/main/java/com/reactive/examples/initialize/UserInitializer.java b/src/main/java/com/reactive/examples/initialize/UserInitializer.java index 4d9b38a..6ba85bd 100644 --- a/src/main/java/com/reactive/examples/initialize/UserInitializer.java +++ b/src/main/java/com/reactive/examples/initialize/UserInitializer.java @@ -19,26 +19,29 @@ @Slf4j public class UserInitializer implements CommandLineRunner { - @Autowired - private UserRepository userRepository; + private final UserRepository userRepository; + + private final DepartmentRepository departmentRepository; + + public UserInitializer(UserRepository userRepository, DepartmentRepository departmentRepository) { + this.userRepository = userRepository; + this.departmentRepository = departmentRepository; + } - @Autowired - private DepartmentRepository departmentRepository; - @Override public void run(String... args) { - initialDataSetup(); + initialDataSetup(); } - private List getData(){ - return Arrays.asList(new User(null,"Suman Das",30,10000), - new User(null,"Arjun Das",5,1000), - new User(null,"Saurabh Ganguly",40,1000000)); + private List getData() { + return Arrays.asList(new User(null, "Suman Das", 30, 10000), + new User(null, "Arjun Das", 5, 1000), + new User(null, "Saurabh Ganguly", 40, 1000000)); } - private List getDepartments(){ - return Arrays.asList(new Department(null,"Mechanical",1,"Mumbai"), - new Department(null,"Computer",2,"Bangalore")); + private List getDepartments() { + return Arrays.asList(new Department(null, "Mechanical", 1, "Mumbai"), + new Department(null, "Computer", 2, "Bangalore")); } private void initialDataSetup() { diff --git a/src/main/java/com/reactive/examples/repository/DepartmentRepository.java b/src/main/java/com/reactive/examples/repository/DepartmentRepository.java index 8a11dcd..c4c573a 100644 --- a/src/main/java/com/reactive/examples/repository/DepartmentRepository.java +++ b/src/main/java/com/reactive/examples/repository/DepartmentRepository.java @@ -4,6 +4,6 @@ import org.springframework.data.repository.reactive.ReactiveCrudRepository; import reactor.core.publisher.Mono; -public interface DepartmentRepository extends ReactiveCrudRepository { +public interface DepartmentRepository extends ReactiveCrudRepository { Mono findByUserId(Integer userId); } diff --git a/src/main/java/com/reactive/examples/repository/UserRepository.java b/src/main/java/com/reactive/examples/repository/UserRepository.java index 24f19b0..728f2be 100644 --- a/src/main/java/com/reactive/examples/repository/UserRepository.java +++ b/src/main/java/com/reactive/examples/repository/UserRepository.java @@ -5,7 +5,7 @@ import org.springframework.data.repository.reactive.ReactiveCrudRepository; import reactor.core.publisher.Flux; -public interface UserRepository extends ReactiveCrudRepository { +public interface UserRepository extends ReactiveCrudRepository { @Query("select * from users where age >= $1") Flux findByAge(int age); } diff --git a/src/main/java/com/reactive/examples/service/UserService.java b/src/main/java/com/reactive/examples/service/UserService.java index 9f8ab8e..ddca2be 100644 --- a/src/main/java/com/reactive/examples/service/UserService.java +++ b/src/main/java/com/reactive/examples/service/UserService.java @@ -21,25 +21,28 @@ @Transactional public class UserService { - @Autowired - private UserRepository userRepository; + private final UserRepository userRepository; - @Autowired - private DepartmentRepository departmentRepository; + private final DepartmentRepository departmentRepository; - public Mono createUser(User user){ + public UserService(UserRepository userRepository, DepartmentRepository departmentRepository) { + this.userRepository = userRepository; + this.departmentRepository = departmentRepository; + } + + public Mono createUser(User user) { return userRepository.save(user); } - public Flux getAllUsers(){ + public Flux getAllUsers() { return userRepository.findAll(); } - public Mono findById(Integer userId){ + public Mono findById(Integer userId) { return userRepository.findById(userId); } - public Mono updateUser(Integer userId, User user){ + public Mono updateUser(Integer userId, User user) { return userRepository.findById(userId) .flatMap(dbUser -> { dbUser.setAge(user.getAge()); @@ -48,13 +51,13 @@ public Mono updateUser(Integer userId, User user){ }); } - public Mono deleteUser(Integer userId){ + public Mono deleteUser(Integer userId) { return userRepository.findById(userId) .flatMap(existingUser -> userRepository.delete(existingUser) - .then(Mono.just(existingUser))); + .then(Mono.just(existingUser))); } - public Flux findUsersByAge(int age){ + public Flux findUsersByAge(int age) { return userRepository.findByAge(age); } @@ -66,11 +69,11 @@ public Flux fetchUsers(List userIds) { .ordered((u1, u2) -> u2.getId() - u1.getId()); } - private Mono getDepartmentByUserId(Integer userId){ + private Mono getDepartmentByUserId(Integer userId) { return departmentRepository.findByUserId(userId); } - public Mono fetchUserAndDepartment(Integer userId){ + public Mono fetchUserAndDepartment(Integer userId) { Mono user = findById(userId).subscribeOn(Schedulers.elastic()); Mono department = getDepartmentByUserId(userId).subscribeOn(Schedulers.elastic()); return Mono.zip(user, department, userDepartmentDTOBiFunction);