Skip to content

This project is based on the "Creating Rest Services" chapter of "Spring in Action" book, where I learned how to define REST endpoints in Spring MVC, use Spring Data repositories to automatically expose REST endpoints, and consume REST APIs.

yacineXP/spring-rest-api

Repository files navigation


spring
Spring REST [Learning]

Project Description | Tech Stack and Libraries | How it Works | Code Examples | Acknowledgements

🚀 Project Description

This project is based on the "Creating Rest Services" chapter of "Spring in Action" book, where I learned how to define REST endpoints in Spring MVC, use Spring Data repositories to automatically expose REST endpoints, and consume REST APIs.

🛠️ Tech Stack and Libraries

This project was built using the following tech stack and libraries:

  • Java Spring
  • Spring MVC
  • Spring Data
  • JUnit

⚙️ How it Works

This project uses Java Spring to provide RESTful endpoints for the Taco Cloud application. The endpoints are defined in Spring MVC controllers, which handle incoming HTTP requests and return the appropriate HTTP responses. Spring Data repositories are used to automatically expose REST endpoints for the Taco Cloud application's database entities.

💻 Code Examples

1. An exemple of the tacoController:

@RestController
@RequestMapping(path="/api/tacos",                
                produces="application/json")
@CrossOrigin(origins="http://localhost:8080")       
public class TacoController {
  private TacoRepository tacoRepo;

  public TacoController(TacoRepository tacoRepo) {
    this.tacoRepo = tacoRepo;
  }

  @GetMapping(params="recent")
  public Iterable<Taco> recentTacos() {            
    PageRequest page = PageRequest.of(
            0, 12, Sort.by("createdAt").descending());
    return tacoRepo.findAll(page).getContent();
  }

  @PostMapping(consumes="application/json")
  @ResponseStatus(HttpStatus.CREATED)
  public Taco postTaco(@RequestBody Taco taco) {
    return tacoRepo.save(taco);
  }

  @GetMapping("/{id}")
  public Taco tacoById(@PathVariable("id") Long id) {
    Optional<Taco> optTaco = tacoRepo.findById(id);
    if (optTaco.isPresent()) {
      return optTaco.get();
    }
    return null;
  }

}

The TacoController is a Spring REST controller that handles HTTP requests related to tacos. It is annotated with @RestController, @RequestMapping, and @CrossOrigin annotations. The @RestController annotation indicates that this class contains RESTful web service methods. The @RequestMapping annotation is used to map HTTP requests to the class level and @CrossOrigin is used to enable Cross-Origin Resource Sharing (CORS) for the web service.

The class has three methods for handling GET and POST requests. The recentTacos() method handles GET requests with the "recent" parameter and returns a list of the most recently added tacos. The postTaco() method handles POST requests with JSON data and saves the taco to the database. The tacoById() method handles GET requests with a specific ID parameter and returns the taco with that ID if it exists in the database.

📚 Acknowledgements

This project was created with the help of the book "Spring in Action" by Craig Walls and Ryan Breidenbach. Many of the concepts and techniques used in this project were learned from this valuable resource.

About

This project is based on the "Creating Rest Services" chapter of "Spring in Action" book, where I learned how to define REST endpoints in Spring MVC, use Spring Data repositories to automatically expose REST endpoints, and consume REST APIs.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published