From 0b1672898e3a5ffcd86871f3ffc658dde6a7752b Mon Sep 17 00:00:00 2001 From: reza ganji Date: Sat, 30 Mar 2024 13:13:45 +0330 Subject: [PATCH] BAEL-7540 --- .../controller/UserController.java | 35 +++++++++++++++++++ .../ex/NotFoundException.java | 12 +++++++ .../exceptionhandeling/model/User.java | 29 +++++++++++++++ .../repository/UserRepository.java | 24 +++++++++++++ 4 files changed, 100 insertions(+) create mode 100644 spring-5-webflux-2/src/main/java/com/baeldung/webflux/exceptionhandeling/controller/UserController.java create mode 100644 spring-5-webflux-2/src/main/java/com/baeldung/webflux/exceptionhandeling/ex/NotFoundException.java create mode 100644 spring-5-webflux-2/src/main/java/com/baeldung/webflux/exceptionhandeling/model/User.java create mode 100644 spring-5-webflux-2/src/main/java/com/baeldung/webflux/exceptionhandeling/repository/UserRepository.java diff --git a/spring-5-webflux-2/src/main/java/com/baeldung/webflux/exceptionhandeling/controller/UserController.java b/spring-5-webflux-2/src/main/java/com/baeldung/webflux/exceptionhandeling/controller/UserController.java new file mode 100644 index 0000000000..6490b997af --- /dev/null +++ b/spring-5-webflux-2/src/main/java/com/baeldung/webflux/exceptionhandeling/controller/UserController.java @@ -0,0 +1,35 @@ +package com.baeldung.webflux.exceptionhandeling.controller; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RestController; + +import com.baeldung.webflux.exceptionhandeling.ex.NotFoundException; +import com.baeldung.webflux.exceptionhandeling.repository.UserRepository; +import com.baeldung.webflux.zipwhen.model.User; + +import reactor.core.publisher.Mono; + +@RestController +public class UserController { + + private final UserRepository userRepository; + + @Autowired + public UserController(UserRepository userRepository) { + this.userRepository = userRepository; + } + + @GetMapping("/user/{id}") + public Mono getUserByIdThrowingException(@PathVariable String id) { + return userRepository.findById(id) + .switchIfEmpty(Mono.error(new NotFoundException("User not found"))); + } + + @GetMapping("/user/{id}") + public Mono getUserByIdUsingMonoError(@PathVariable String id) { + return userRepository.findById(id) + .switchIfEmpty(Mono.error(() -> new NotFoundException("User not found"))); + } +} diff --git a/spring-5-webflux-2/src/main/java/com/baeldung/webflux/exceptionhandeling/ex/NotFoundException.java b/spring-5-webflux-2/src/main/java/com/baeldung/webflux/exceptionhandeling/ex/NotFoundException.java new file mode 100644 index 0000000000..5401a4d133 --- /dev/null +++ b/spring-5-webflux-2/src/main/java/com/baeldung/webflux/exceptionhandeling/ex/NotFoundException.java @@ -0,0 +1,12 @@ +package com.baeldung.webflux.exceptionhandeling.ex; + +public class NotFoundException extends RuntimeException { + + public NotFoundException(String message) { + super(message); + } + + public NotFoundException(String message, Throwable cause) { + super(message, cause); + } +} diff --git a/spring-5-webflux-2/src/main/java/com/baeldung/webflux/exceptionhandeling/model/User.java b/spring-5-webflux-2/src/main/java/com/baeldung/webflux/exceptionhandeling/model/User.java new file mode 100644 index 0000000000..42e1293ee8 --- /dev/null +++ b/spring-5-webflux-2/src/main/java/com/baeldung/webflux/exceptionhandeling/model/User.java @@ -0,0 +1,29 @@ +package com.baeldung.webflux.exceptionhandeling.model; + +public class User { + private String id; + private String username; + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getUsername() { + return username; + } + + public void setUsername(String username) { + this.username = username; + } + + public User(String userId, String userName) { + this.id=userId; + this.username=userName; + + } +} + diff --git a/spring-5-webflux-2/src/main/java/com/baeldung/webflux/exceptionhandeling/repository/UserRepository.java b/spring-5-webflux-2/src/main/java/com/baeldung/webflux/exceptionhandeling/repository/UserRepository.java new file mode 100644 index 0000000000..7d4ac0af86 --- /dev/null +++ b/spring-5-webflux-2/src/main/java/com/baeldung/webflux/exceptionhandeling/repository/UserRepository.java @@ -0,0 +1,24 @@ +package com.baeldung.webflux.exceptionhandeling.repository; + +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +import org.springframework.stereotype.Repository; + +import com.baeldung.webflux.zipwhen.model.User; + +import reactor.core.publisher.Mono; + +@Repository +public class UserRepository { + private final Map userDatabase = new ConcurrentHashMap<>(); + + public UserRepository() { + userDatabase.put("1", new User("1", "John Doe")); + userDatabase.put("2", new User("2", "Jane Smith")); + } + + public Mono findById(String id) { + return Mono.justOrEmpty(userDatabase.get(id)); + } +}