This tutorial will guide you through completing the User class. You will learn some about Optional<>, constructors and Factory Methods.
- Implement the first factory method.
- It should create and return a new
Userobject.
Some users do not have email address. Thus, we want to make this parameter optional.
You must modify the email field and related methods. Utilise Optional<String> type.
Remember that the constructor also needs to handle such cases. Try to use Optional.ofNullable().
Moreover, also email getter needs to be modified. Let's explore Optional methods, such as .orElse().
Please return a default company address if the user has no private email address.
Now it is time to create yet another Factory Method - this time without email address in parameters.
Run TaskNo1Test to verify your progress
You need to implement the method filter(Collection<User> users) so that it:
- Removes users whose name starts with the letter 'A' (case-sensitive).
- Removes users with invalid email addresses.
- Removes users whose phone number is not Polish (Polish numbers start with
+48).
Please try to keep the convention and explore Java Streams.
Run TaskNo2Test to verify your progress
It is time to create your first endpoint.
You need to tell Spring that the class UserEndpoint handles HTTP requests. Let's use @RestController annotation.
You need to map the createUser method to an HTTP POST request and read the User object from the request body.
- Add
@PostMapping("/users")above the method. - Add
@RequestBodyto the method parameter.
You need to:
- Add the new
Userto theuserslist. - Return a response with HTTP 200 OK and a JSON object containing the user ID.
For simplicity, you can simulate a generated ID using i.e. users.size().
Run TaskNo3Test to verify your progress