05 December 2023

Building a Simple Spring Boot Rest API for Auto Parts

 In today's fast-paced world, managing data efficiently is a crucial aspect of application development. In this blog post, we'll embark on a journey to create a straightforward Spring Boot Rest API for handling information about auto parts. Whether you're a seasoned developer or just starting, this guide will break down the code into simple terms.

AutoPart Class: Modeling Our Auto Parts

Our journey begins with the AutoPart class, serving as the blueprint for an auto part. In this class, we define three fundamental attributes: id for identification, partName for the name of the part, and price for its cost. Think of this class as a template that allows us to create instances representing different auto parts.


public class AutoPart {

    private int id;

    private String partName;

    private double price;


    // Constructors, getters, and setters

}

AutoPartService Interface: Declaring the Contract

Next up is the AutoPartService interface, where we declare a simple contract - a method named findAll(). This method is a promise that any class implementing this interface must keep. The promise is to provide a list of AutoPart objects.

public interface AutoPartService { List<AutoPart> findAll(); }

ShopService Class: Fulfilling the Promise

Now comes the ShopService class, a diligent worker that fulfills the promise made in the AutoPartService interface. This class is annotated with @Service, indicating that it's a special class managed by the Spring framework. Inside the findAll() method, we create a list of auto parts - engine oil and brake pads in this case - and return it.

@Service public class ShopService implements AutoPartService { @Override public List<AutoPart> findAll() { List<AutoPart> autoParts = new ArrayList<>(); autoParts.add(new AutoPart(100, "Engine Oil", 20.00)); autoParts.add(new AutoPart(101, "Brake Pads", 40.00)); return autoParts; } }

AutoPartController Class: Handling Requests
In the world of Rest APIs, the AutoPartController class takes center stage. Annotated with @RestController, it's responsible for handling incoming requests related to auto parts. The @Autowired annotation magically injects an instance of AutoPartService into the controller.


getAutoPart() Method: Fetching Auto Parts

The getAutoPart() method, mapped to the "/autopart" URL, retrieves a list of auto parts by calling the findAll() method of the injected service. This method is like a waiter taking your order and bringing back the requested items.


@RestController public class AutoPartController { @Autowired private AutoPartService autoPartService; @GetMapping(value = "/autopart") public List<AutoPart> getAutoPart() { return autoPartService.findAll(); } @PostMapping public String createUser() { return "create user called"; } }

createUser() Method: A Fun Side Note

Just for fun, the createUser() method is a quirky addition. It doesn't directly relate to auto parts but simply returns a string saying "create user called." It's a playful touch to demonstrate that controllers can have various methods handling different tasks.


SpringBootRestApiSpecimenApplication Class: Starting the Show

SpringBootRestApiSpecimenApplication class is the maestro orchestrating the whole performance. Annotated with @SpringBootApplication, it tells Spring Boot to do its behind-the-scenes magic. The main method acts as the conductor, starting the symphony of our Spring Boot application.

@SpringBootApplication
public class SpringBootRestApiSpecimenApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootRestApiSpecimenApplication.class, args);
}
}

Running the Application

To witness our creation in action, run the application by executing the main method in SpringBootRestApiSpecimenApplication. Once the application is up and running, visit http://localhost:8080/autopart to see the list of auto parts.

Congratulations! You've just crafted a simple Spring Boot Rest API for auto parts. This journey introduces you to the basics of creating models, services, controllers, and running a Spring Boot application. Feel free to explore and modify the code to suit your needs as you continue your coding adventure. Happy coding!




No comments:

Post a Comment