English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

REST Example

REST applications follow REST architectural methods. We use REST applications to develop and design web applications. It generates HTTP requests to perform CRUD operations on data. Typically, it returns data in JSON or XML format.

Spring Boot REST API example

In the following example, we will create a REST application. In this application, we create a product list and return the same list. It returns data in JSON format.

Let's implement it in the RSET application and understand the REST methods by following the following steps.

Steps1: Open Spring Initializr https://start.spring.io/.

Steps2: Select Spring Boot version 2.3.0.M2 .

Steps3: provide groupname. We provide the group name com.w3codebox .

Steps4: provide artifacts. We provide Artifact spring-boot-rest-example example.

Steps5: Add Spring Web dependencies.

Steps6: Click the "Generate" button. When we click Generate button, it will package all the specifications related to the application into Jar the file and download it to the local system.

Steps7: Extract jar file.

Steps8: Copy the folder and paste it into the STS workspace.

Steps9: ImportProject.

File->Import->Existing Maven project->Next->Browse- >Select folder spring- spring-boot-rest-example->Select folder->Finish

It takes time to import the project. After successfully importing the project, we can view it in the IDE's Package Explorer you can see it in the part.

Steps10: create a modelpackage com.w3codebox class. We have created a class named productclass. In this class, please perform the following operations:

Create five variables id, pname, batchno, price, and noofproduct. Create the default constructor. Generate Using FieldsConstructor.
Right-click on the file->Source->Generate Constructor Using Fields->Select All->Generate
GenerateLetterand setters.

Right-click on the file->Source->Generate Getters and Setters->Select All->Generate

After completing all steps, the class is as follows.

Product.java

package com.w3codebox;
public class Product 
{
    private int id;
    private String pname;
    private String batchno;
    private double price;
    private int noofproduct;
    //Default constructor
    public Product()
    {
        
    }
    //Use the field constructor
    public Product(int id, String pname, String batchno, double price, int noofproduct) 
    {
        super();
        this.id = id;
        this.pname = pname;
        this.batchno = batchno;
        this.price = price;
        this.noofproduct = noofproduct;
    }
    //getters and setters
    public int getId() 
    {
        return id;
    }
    public void setId(int id) 
    {
        this.id = id;
    }
    public String getPname() 
    {
        return pname;
    }
    public void setPname(String pname) 
    {
        this.pname = pname;
    }
    public String getBatchno() 
    {
        return batchno;
    }
    public void setBatchno(String batchno) 
    {
        this.batchno = batchno;
    }
    public double getPrice() 
    {
        return price;
    }
    public void setPrice(double price) 
    {
        this.price = price;
    }
    public int getNoofproduct() 
    {
        return noofproduct;
    }
    public void setNoofproduct(int noofproduct) 
    {
        this.noofproduct = noofproduct;
    }
}

Now, we need to create a controller.

Steps11: in com.w3codebox In the package, create a Controller. We created a controller named ProductController controller.

use annotations @RestController annotation class. We have automatically connected IProductService interface. We will create it in the next step. We have used annotations @GetMapping Create a mapping/product. We have mapped the method getProduct()Map to/product . This method returns a list of products.

ProductController.java

package com.w3codebox;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ProductController 
{
    @Autowired
    private IProductService productService;
    //Map the getProduct() method to/product
    @GetMapping(value = "/(product)
    public List<Product> getProduct() 
    {
        //Find all products
        List<Product> products = productService.findAll();
        //return the list of products
        return products;
    }
}

Steps12: in the package com.w3codebox create a name of IProductService interface, and define findAll() method, which returns a list of products.

IProductService.java

package com.w3codebox;
import java.util.List;
public interface IProductService 
{
List<Product> findAll();
}

Steps13: create a Service class. We created a package com.w3codebox created a named ProductService service class.

use annotations @Service Annotation class, and implement IProductService interface. In this class, use annotations @Override Override findAll()The method. The ProductService class's findAll() method will override IProductService the interface's findAll() method. Create ArrayList the object. Addarray list of products. return the productList.

ProductService.java

package com.w3codebox;
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Service;
@Service
public class ProductService implements IProductService  
{
    @Override
    public List<Product> findAll()
    {
        //Create an ArrayList object
        ArrayList<Product> products = new ArrayList<Product>();
        //Add the product to the list
        products.add(new Product(100, "Mobile", "CLK98123", 9000.00, 6));
        products.add(new Product(101, "Smart TV", "LGST09167", 60000.00, 3));
        products.add(new Product(102, "Washing Machine", "38753BK9", 9000.00, 7));
        products.add(new Product(103, "Laptop", "LHP29OCP", 24000.00, 1));
        products.add(new Product(104, "Air Conditioner", "ACLG66721", 30000.00, 5));
        products.add(new Product(105, "Refrigerator ", "12WP9087", 10000.00, 4));
        //returns a list of product
        return products;
    }
}

Steps14: in static folder (src/main/resources/static) and create an HTML file. We have created a file named index The HTML file. In this file, we created Get all productslink.

index.html

<!DOCTYPE html>
<html>
<head>
<title>Home page</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<p>
<a href="product">Get all Products</a>
</p>
</body>
</html>

Now, we have created all the files and folders. After creating all the files, the project directory looks like it is deceiving:

Let's run the application.

Steps15: Open SpringBootRestExampleApplication.java File it as a Java application. By default, it runs on port 8080 above.

SpringBootRestExampleApplication.java

package com.w3codebox;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringBootRestExampleApplication 
{
    public static void main(String[] args) 
    {
        SpringApplication.run(SpringBootRestExampleApplication.class, args);
    }
}

When the application runs successfully, it will display the message as follows

Steps16: Open the browser and call the URL http://localhost:8080/index.html. It displays Get all productsas shown in the following figure.

Click the link Get all products. It is JSON Format to return the product list, and change the URL to http://localhost:8080/product.