GithubHelp home page GithubHelp logo

paulasanchez810 / spring-boot-rest-api Goto Github PK

View Code? Open in Web Editor NEW

This project forked from ada-school/spring-boot-rest-api

0.0 1.0 0.0 28 KB

In this codelab we will be implementing REST API Microservices using Spring Boot.

spring-boot-rest-api's Introduction

Spring Boot Rest API

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.

Growth Mindset 🤹🏽

"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.

Codelab 🧪

🗣️ "I hear and I forget I see and I remember I do and I understand." Confucius

Part 1: Implementing the Users Microservice RESTFUL API

  1. 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.
  1. Create a new repository on Github and commit the files generated in 1.
  2. Create a new package called dto and inside define your UserDto object with at least the following fields:
    • name.
    • email.
    • lastName.
    • createdAt.
  3. Create a new package called data and inside define your User data object with at least the following fields:
    • id.
    • name.
    • email.
    • lastName.
    • createdAt.
  4. 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

    }
  1. Create an implementation of the UserService using a HashMap data structure inside.
  2. Make your service implementation UserServiceHashMap injectable using the @Service annotation.
  3. Create a new package called controller and create a new class UserController inside.
  4. Annotate your UserController so it becomes a REST Controller:

Java:

  @RestController
  @RequestMapping( "/v1/user" )
  public class UserController
  {
  }

Kotlin:

 @RestController
 @RequestMapping( "/v1/user" )
 class UserController()
  1. 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)
  1. 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     
    }   
 
 
 }
  1. Download and install Postman and test ALL the endpoints of your API.

Part 2: Implementing the Tasks Microservice RESTFUL API

  1. 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.
  1. Create a new repository on Github and commit the files generated in 1.
  2. Create a new package called dto and inside define your TaskDto object with at least the following fields:
    • name.
    • description.
    • status [TODO, DOING, REVIEW and DONE].
    • assignedTo.
    • dueDate.
    • created.
  3. Create a new package called data and inside define your Task data object with at least the following fields:
    • id.
    • name.
    • description.
    • status [TODO, DOING, REVIEW and DONE].
    • assignedTo.
    • dueDate.
    • created.
  4. Create a new package called service an inside create the following interface:

Java:

    public interface TaskService
    {
        Task create( Task task );

        Task findById( String id );
        
        List<Task> all();

        void deleteById( String id );

        Task update( Task task, String id );
    }

Kotlin:

    interface TaskService {

       fun create( task: Task): Task

       fun findById( String id ): Task?
       
       fun  all(): List<Task>

       fun deleteById( String id )

       fun update( Task task, String id ): Task

    }
  1. Create an implementation of the TaskService using a HashMap data structure inside.
  2. Make your service implementation TaskServiceHashMap injectable using the @Service annotation.
  3. Implement you TaskController (try to avoid copy paste, use the User Microservice as reference but try doing it consciously).
  4. Test ALL the endpoints of your API using PostMan or any other tool of your preference.

spring-boot-rest-api's People

Contributors

sancarbar avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.