Learning Objectives
- Explain what needs to be donde to achieve the Level 2 on a RESTFUL API on the Richardson Maturity Model.
- Implement a Level 2 Users RESTFUL API Microservice.
- Implement a Level 2 Tasks RESTFUL API Microservice.
- User dependencies injections to create a decoupled architecture.
"Individuals who believe their talents can be developed (through hard work, good strategies, and input from others) have a growth mindset. They tend to achieve more than those with a more fixed mindset (those who believe their talents are innate gifts)" What Having a "Growth Mindset" Actually means - Harvard Business Review
Main Topics
- Microservices.
- RESTFUL API.
- Richardson Maturity Model.
- Dependencies Injection.
🗣️ "I hear and I forget I see and I remember I do and I understand." Confucius
- Create a new project using the Spring Initializr
- Use either Java or Kotlin as programming language.
- Use Gradle as project option(if your computer is slow then use Maven)
- Add Spring Web dependency before generating the project.
- Create a new repository on Github and commit the files generated in 1.
- Create a new package called dto and inside define your UserDto object with at least the following fields:
- name.
- email.
- lastName.
- createdAt.
- Create a new package called data and inside define your User data object with at least the following fields:
- id.
- name.
- email.
- lastName.
- createdAt.
- Create a new package called service an inside create the following interface:
Java:
public interface UserService
{
User create( User user );
User findById( String id );
List<User> all();
void deleteById( String id );
User update( User user, String userId );
}
Kotlin:
interface UserService {
fun create( user: User): User
fun findById( String: id ): User?
fun all(): List<User>
fun deleteById( String: id )
fun update( User: user, String: userId ): User
}
- Create an implementation of the UserService using a HashMap data structure inside.
- Make your service implementation UserServiceHashMap injectable using the @Service annotation.
- Create a new package called controller and create a new class UserController inside.
- Annotate your UserController so it becomes a REST Controller:
Java:
@RestController
@RequestMapping( "/v1/user" )
public class UserController
{
}
Kotlin:
@RestController
@RequestMapping("/v1/user")
class UserController
- Inject your UserService implementation inside the UserController via the constructor:
Java:
@RestController
@RequestMapping( "/v1/user" )
public class UserController
{
private final UserService userService;
public UserController(@Autowired UserService userService )
{
this.userService = userService;
}
}
Kotlin:
@RestController
@RequestMapping( "/v1/user" )
class UserController(@Autowired private val userService: UserService)
- Implement all the endpoints needed to interact with you UserService. Use the following method signatures to help you achieve the Level 2 RESTFUL Maturity:
Java:
@RestController
@RequestMapping( "/v1/user" )
public class UserController
{
private final UserService userService;
public UserController( UserService userService )
{
this.userService = userService;
}
@GetMapping
public ResponseEntity<List<User>> all()
{
//TODO implement this method using UserService
return null;
}
@GetMapping( "/{id}" )
public ResponseEntity<User> findById( @PathVariable String id )
{
//TODO implement this method using UserService
return null;
}
@PostMapping
public ResponseEntity<User> create( @RequestBody UserDto userDto )
{
//TODO implement this method using UserService
return null;
}
@PutMapping( "/{id}" )
public ResponseEntity<User> update( @RequestBody UserDto userDto, @PathVariable String id )
{
//TODO implement this method using UserService
return null;
}
@DeleteMapping( "/{id}" )
public ResponseEntity<Boolean> delete( @PathVariable String id )
{
//TODO implement this method using UserService
return null;
}
}
Kotlin:
@RestController
@RequestMapping( "/v1/user" )
class UserController(@Autowired private val userService: UserService)
{
@GetMapping
fun all(): ResponseEntity<List<User>>
{
//TODO implement this method using UserService
return null
}
@GetMapping( "/{id}" )
fun findById( @PathVariable id: String ): ResponseEntity<User>
{
//TODO implement this method using UserService
return null
}
@PostMapping
fun create( @RequestBody userDto: UserDto): ResponseEntity<User>
{
//TODO implement this method using UserService
return null
}
@PutMapping( "/{id}" )
fun update( @RequestBody userDto: UserDto, @PathVariable id: String): ResponseEntity<User>
{
//TODO implement this method using UserService
return null
}
@DeleteMapping( "/{id}" )
fun delete( @PathVariable id: String): ResponseEntity<Boolean>
{
//TODO implement this method using UserService
return null
}
}
- Download and install Postman and test ALL the endpoints of your API.