<?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: Mohamed Manbar</title>
    <description>The latest articles on DEV Community by Mohamed Manbar (@mohamed_manbar).</description>
    <link>https://dev.to/mohamed_manbar</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%2F1724135%2Faafccbef-8bec-4e1a-8d7a-ae0b6e724640.jpeg</url>
      <title>DEV Community: Mohamed Manbar</title>
      <link>https://dev.to/mohamed_manbar</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mohamed_manbar"/>
    <language>en</language>
    <item>
      <title>Building a REST API with Spring Boot: A Step-by-Step Guide</title>
      <dc:creator>Mohamed Manbar</dc:creator>
      <pubDate>Mon, 22 Jul 2024 21:24:38 +0000</pubDate>
      <link>https://dev.to/mohamed_manbar/building-a-rest-api-with-spring-boot-a-step-by-step-guide-1a7p</link>
      <guid>https://dev.to/mohamed_manbar/building-a-rest-api-with-spring-boot-a-step-by-step-guide-1a7p</guid>
      <description>&lt;p&gt;To create a REST API using Spring Boot and publish an article about it on Medium, here are the general steps to follow:&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;1. Initialize a Spring Boot Project&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Step 1:&lt;/strong&gt; Use Spring Initializr&lt;br&gt;
Go to &lt;a href="https://start.spring.io/" rel="noopener noreferrer"&gt;Spring Initializr&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Configure your project:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fj7ejirz94vpiuso1x25p.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fj7ejirz94vpiuso1x25p.png" alt=" " width="800" height="547"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Project&lt;/em&gt;: Maven Project&lt;br&gt;
&lt;em&gt;Language&lt;/em&gt;: Java&lt;br&gt;
&lt;em&gt;Spring Boot&lt;/em&gt;: 3.3.x or a more recent stable version&lt;br&gt;
&lt;em&gt;Project Metadata&lt;/em&gt;: Fill in the group, artifact, and other necessary information&lt;br&gt;
&lt;em&gt;Dependencies&lt;/em&gt;: Add Spring Web, Spring Data JPA, and H2 Database (for an in-memory database)&lt;br&gt;
Click “Generate” to download the project.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2&lt;/strong&gt;: Unzip and Import the Project&lt;br&gt;
Unzip the downloaded ZIP file.&lt;br&gt;
Import the project into your preferred IDE (Eclipse, IntelliJ IDEA, etc.).&lt;/p&gt;
&lt;h2&gt;
  
  
  &lt;strong&gt;2. Configure the Project&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Step 3:&lt;/strong&gt; Configure the H2 Database&lt;br&gt;
In the application.properties file, add the following configurations for H2:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;spring.h2.console.enabled=true
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=password
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.jpa.hibernate.ddl-auto=update
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 4: Create the JPA Entity&lt;/strong&gt;&lt;br&gt;
Create an Article class representing an article:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package com.example.demo.model;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Article {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String title;
    private String content;
    // Getters and Setters
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 5: Create the JPA Repository&lt;/strong&gt;&lt;br&gt;
Create an ArticleRepository interface:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package com.example.demo.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import com.example.demo.model.Article;
public interface ArticleRepository extends JpaRepository&amp;lt;Article, Long&amp;gt; {
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;strong&gt;3. Create Services and Controllers&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Step 6: Create the Service&lt;/strong&gt;&lt;br&gt;
Create an ArticleService class:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package com.example.demo.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.example.demo.repository.ArticleRepository;
import com.example.demo.model.Article;
import java.util.List;
@Service
public class ArticleService {
    @Autowired
    private ArticleRepository articleRepository;
    public List&amp;lt;Article&amp;gt; getAllArticles() {
        return articleRepository.findAll();
    }
    public Article getArticleById(Long id) {
        return articleRepository.findById(id).orElse(null);
    }
    public Article saveArticle(Article article) {
        return articleRepository.save(article);
    }
    public void deleteArticle(Long id) {
        articleRepository.deleteById(id);
    }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 7: Create the Controller&lt;/strong&gt;&lt;br&gt;
Create an ArticleController class:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package com.example.demo.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.example.demo.service.ArticleService;
import com.example.demo.model.Article;
import java.util.List;
@RestController
@RequestMapping("/api/articles")
public class ArticleController {
    @Autowired
    private ArticleService articleService;
    @GetMapping
    public List&amp;lt;Article&amp;gt; getAllArticles() {
        return articleService.getAllArticles();
    }
    @GetMapping("/{id}")
    public Article getArticleById(@PathVariable Long id) {
        return articleService.getArticleById(id);
    }
    @PostMapping
    public Article createArticle(@RequestBody Article article) {
        return articleService.saveArticle(article);
    }
    @DeleteMapping("/{id}")
    public void deleteArticle(@PathVariable Long id) {
        articleService.deleteArticle(id);
    }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;**&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Test the Application
&lt;/h2&gt;

&lt;p&gt;**&lt;br&gt;
&lt;strong&gt;Step 8: Run the Application&lt;/strong&gt;&lt;br&gt;
Run the main class of the project (annotated with @SpringBootApplication).&lt;br&gt;
Access the H2 console via &lt;a href="http://localhost:8080/h2-console" rel="noopener noreferrer"&gt;http://localhost:8080/h2-console&lt;/a&gt; and use the configured connection details to verify the data.&lt;br&gt;
&lt;strong&gt;Step 9: Test the Endpoints&lt;/strong&gt;&lt;br&gt;
Use tools like Postman to test the endpoints:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;GET /api/articles: Retrieves all articles&lt;/li&gt;
&lt;li&gt;GET /api/articles/{id}: Retrieves an article by ID&lt;/li&gt;
&lt;li&gt;POST /api/articles: Creates a new article&lt;/li&gt;
&lt;li&gt;DELETE /api/articles/{id}: Deletes an article by ID&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>programming</category>
      <category>java</category>
      <category>springboot</category>
      <category>backend</category>
    </item>
  </channel>
</rss>
