Spring Boot – REST Example


In this tutorial, we will see Spring Boot REST Example which is quick start guide.

Java and Basic Spring framework knowledge prerequisite for this tutorial:

Versions:

  • Java: 8
  • Spring Boot : 2.7
  • IDE: Download STS is recommended for Spring boot applications

Create New REST Project:

  1. In STS, click on File -> New -> Spring Starter Project

The below window will be opned

Maven Configuration: Like shown in below, select Java Version , provide group , artifact details:

Spring Starter Dependencies Selection:

As shown in below, select Spring Boot Version and select Spring Web as Starter which required from REST.

Generated pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.7.10</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.javasavvy.tutorial</groupId>
	<artifactId>product-catalog-service</artifactId>
	<version>1.0.0</version>
	<name>product-catalog-service</name>
	<description>Demo project for Spring Boot</description>
	<properties>
		<java.version>1.8</java.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

Spring Boot Main Class file

package com.javasavvy.tutorial;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ProductCatalogServiceApplication {

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

}

@SpringBootApplication is equivalent to below annotations:

  • @Configuration
  • @EnableAutoConfiguration
  • @ComponentScan.

Spring REST Controller:

package com.javasavvy.tutorial.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.javasavvy.tutorial.model.ProductItem;

@RestController()
@RequestMapping(path="/products")
public class ProductCatalogController {

	
	@GetMapping
	public ResponseEntity<?> getProducts(){
		List<ProductItem> items = new ArrayList<>();
		ProductItem item = new ProductItem();
		item.setId(1001L);
		item.setName("Car");
		items.add(item);
		return new ResponseEntity<>(items,HttpStatus.OK);
	}
}

Project Hierarchy:

One question you might get that how to access REST service such context path and port. you can find those details in the logs and it will print context path and port number. Construct URL by analyzing startup logs: In this case, the URL is http://localhost:8080/products (context path is empty)

o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
o.apache.catalina.core.StandardService   : Starting service [Tomcat]
main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.73]
o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1637 ms
main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
.j.t.ProductCatalogServiceApplication   : Started ProductCatalogServiceApplication in 3.59 seconds (JVM running for 4.585)
[nio-8080-exec-6] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
[nio-8080-exec-6] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
[nio-8080-exec-6] o.s.web.servlet.DispatcherServlet        : Completed initialization in 4 ms

Test the URL in PostMan:

I hope this helps! Let me know if you have any questions.

Refer below link to change to Custom Port instead of 8080:

Refer below link to know all important annotations: