<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Edson Camacho</title>
    <description>The latest articles on DEV Community by Edson Camacho (@edson_camacho_96de9a185d3).</description>
    <link>https://dev.to/edson_camacho_96de9a185d3</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2630269%2Ffed24c5b-afd9-436a-961c-74a829372331.png</url>
      <title>DEV Community: Edson Camacho</title>
      <link>https://dev.to/edson_camacho_96de9a185d3</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/edson_camacho_96de9a185d3"/>
    <language>en</language>
    <item>
      <title>Building a RESTful API with Spring Boot: A Step-by-Step Tutorial</title>
      <dc:creator>Edson Camacho</dc:creator>
      <pubDate>Sun, 29 Dec 2024 18:19:40 +0000</pubDate>
      <link>https://dev.to/edson_camacho_96de9a185d3/building-a-restful-api-with-spring-boot-a-step-by-step-tutorial-4emc</link>
      <guid>https://dev.to/edson_camacho_96de9a185d3/building-a-restful-api-with-spring-boot-a-step-by-step-tutorial-4emc</guid>
      <description>&lt;p&gt;Spring Boot is one of the most popular frameworks for building Java-based applications. Its simplicity and productivity features make it a go-to choice for developers. In this tutorial, we will walk through the process of building a RESTful API using Spring Boot, from setting up the project to implementing a basic API endpoint.&lt;/p&gt;

&lt;p&gt;Setting Up the Project&lt;br&gt;
To begin, we need to set up our Spring Boot project. A quick way to do this is by using Spring Initializr. Head over to start.spring.io and generate a new project with the following dependencies:&lt;/p&gt;

&lt;p&gt;Spring Web&lt;br&gt;
Spring Data JPA&lt;br&gt;
H2 Database (for simplicity)&lt;br&gt;
Once the project is generated, open it in your preferred IDE, such as IntelliJ IDEA or Eclipse, and you’re ready to start coding.&lt;/p&gt;

&lt;p&gt;Creating the Model Class&lt;br&gt;
Start by creating a model class for the API. This model will represent an entity in our database, which, in this case, is a Product.&lt;/p&gt;

&lt;p&gt;`@Entity&lt;br&gt;
public class Product {&lt;br&gt;
    &lt;a class="mentioned-user" href="https://dev.to/id"&gt;@id&lt;/a&gt;&lt;br&gt;
    @GeneratedValue(strategy = GenerationType.IDENTITY)&lt;br&gt;
    private Long id;&lt;br&gt;
    private String name;&lt;br&gt;
    private double price;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Getters and Setters
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;}`&lt;/p&gt;

&lt;p&gt;Creating the Repository Interface&lt;br&gt;
Next, we need to create a repository interface to handle CRUD operations for our Product entity. Spring Data JPA will simplify this process for us.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;@Repository&lt;br&gt;
public interface ProductRepository extends JpaRepository&amp;lt;Product, Long&amp;gt; {&lt;br&gt;
}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Creating the Service Layer&lt;br&gt;
We then create the service layer, which will handle the business logic. The service will interact with the repository to fetch data from the database.&lt;/p&gt;

&lt;p&gt;`@Service&lt;br&gt;
public class ProductService {&lt;br&gt;
    private final ProductRepository productRepository;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@Autowired
public ProductService(ProductRepository productRepository) {
    this.productRepository = productRepository;
}

public List&amp;lt;Product&amp;gt; getAllProducts() {
    return productRepository.findAll();
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;}`&lt;/p&gt;

&lt;p&gt;Creating the Controller&lt;br&gt;
Now, let’s create the controller that will expose a REST API endpoint for retrieving the list of products. This controller will handle HTTP requests and respond with the appropriate data.&lt;/p&gt;

&lt;p&gt;`@RestController&lt;br&gt;
@RequestMapping("/api/products")&lt;br&gt;
public class ProductController {&lt;br&gt;
    private final ProductService productService;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@Autowired
public ProductController(ProductService productService) {
    this.productService = productService;
}

@GetMapping
public List&amp;lt;Product&amp;gt; getAllProducts() {
    return productService.getAllProducts();
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;}`&lt;/p&gt;

&lt;p&gt;Running the Application&lt;br&gt;
With everything in place, you can now run the Spring Boot application. Once the app is running, navigate to &lt;a href="http://localhost:8080/api/products" rel="noopener noreferrer"&gt;http://localhost:8080/api/products&lt;/a&gt; in your browser or use an API tool like Postman to test the endpoint. You should see a JSON response containing a list of products.&lt;/p&gt;

&lt;p&gt;For more in-depth tutorials and tips, feel free to check out my blog, where I cover a variety of topics on Java, Spring Boot, and software development.&lt;/p&gt;

&lt;p&gt;Conclusion&lt;br&gt;
In this tutorial, we’ve walked through the basics of creating a RESTful API with Spring Boot. We set up a project, created a model and repository, added a service layer, and exposed a simple endpoint. With Spring Boot, you can quickly get up and running with REST APIs, making it an ideal choice for building Java-based applications.&lt;/p&gt;

&lt;p&gt;About Me&lt;br&gt;
Hi! I’m Edson Camacho, a passionate full-stack developer and instructor. I specialize in Java, Spring Boot, and web development. I enjoy teaching others and sharing valuable insights through my blog and online courses. You can find more tutorials, tips, and resources on my blog &lt;a href="https://softwareengineeracademy.com/" rel="noopener noreferrer"&gt;Software Engineer's Academy&lt;/a&gt;. &lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
