Rest api definition and setup with Spring boot
A REST API (Representational State Transfer) is a web service that follows the principles of REST architecture to allow communication between different systems over the web using HTTP methods.
REST APIs are stateless, with each client request being transient, requiring all necessary information for the server to understand and process it. They are favored for their simplicity, scalability, and easy integration with different systems and platforms.
HTTP Methods:
- REST APIs use standard HTTP methods to perform operations on resources:
- GET: Retrieve data from the server (read operation).
- POST: Submit data to the server to create a new resource (create operation).
- PUT: Update an existing resource on the server (update operation).
- DELETE: Remove a resource from the server (delete operation).
Statelessness:
- Each API call is stateless and independent; furthermore, the server does not store any session information about the client. Therefore, every request must contain all necessary information for the server to understand and process it, much like how an ATM machine operates.https://myntr.it/BkDyfZ8

Format:
- REST APIs commonly use JSON (JavaScript Object Notation) or XML for data exchange.
What is Spring boot ?
Spring Boot is a framework that simplifies the development of Spring-based applications by offering a set of tools to create production-ready applications with minimal configuration. It streamlines the development process, allowing developers to focus on business logic without worrying about boilerplate code or complex configuration.
Key Features of Spring Boot:
1. Auto-Configuration:
- Spring Boot uses auto-configuration to automatically configure the application based on the dependencies on the classpath. For example, if you add
spring-boot-starter-web, it automatically configures components needed for a web application like a dispatcher servlet, Embedded Tomcat, etc. - This helps avoid manual setup of configurations in
applicationContext.xmlor@Configurationclasses.
2. Opinionated Defaults:
- Spring Boot provides opinionated defaults, which means it comes with sensible defaults for most use cases to get applications up and running quickly.
- For example, default settings for embedded web servers, logging, database connections, and data sources are predefined but can be easily customized if needed.
3. Embedded Servers:
- Spring Boot applications can run as standalone applications without needing an external application server.
- It supports embedded web servers like Tomcat, Jetty, and Undertow, which are automatically started when the application runs, making deployment simpler (just
java -jarthe executable JAR file).
4. Spring Boot Starters:
- Starters are dependency descriptors that bundle common dependencies together. For example:
spring-boot-starter-web: Includes dependencies for building web applications using Spring MVC.spring-boot-starter-data-jpa: Bundles dependencies for working with databases using JPA and Hibernate.
- These starters simplify dependency management by avoiding the need to specify multiple dependencies manually.
5. Production-Ready Features:
- Spring Boot includes built-in tools to make applications production-ready with minimal effort:
- Spring Boot Actuator: Provides metrics, health checks, and monitoring endpoints to get insights into the application’s performance.
- Externalized Configuration: Allows easy configuration of different environments (development, production) using application.properties or application.yml files. You can also use environment variables, command-line arguments, or external configuration sources.
- Health Monitoring: With Actuator, you can monitor the application’s health status, logging, and memory metrics, among others.
6. Spring Boot CLI:
- The Spring Boot CLI (Command Line Interface) is a tool that allows you to run Groovy scripts to create Spring applications quickly. It allows for rapid prototyping without needing a full build environment or IDE.
7. Microservices Development:
- Spring Boot is ideal for building microservices due to its support for REST APIs, lightweight containerization, and easy deployment.
- It also integrates well with tools and libraries used for service discovery, distributed configuration, and circuit breakers (e.g., with Spring Cloud).
8. Simplified Data Access:
- Spring Boot integrates with Spring Data to simplify database access through JPA, MongoDB, Redis, and more.
- With Spring Data JPA, for instance, you can use Repository interfaces to perform database operations without writing boilerplate code.
9. Testing Support:
- Spring Boot comes with excellent testing support:
- MockMvc for testing web layer without starting a full HTTP server.
- Spring Boot Test: Provides annotations like
@SpringBootTest,@WebMvcTest, and more to test different layers of your application.
- It simplifies testing by providing configurations that are tailored for various testing scenarios, making it easier to write and run tests.
10. Dependency Injection:
- Like Spring Framework, Spring Boot utilizes dependency injection (via
@Autowired,@Component, etc.) to manage the lifecycle of objects and promote loose coupling. - It supports constructor injection, field injection, and setter injection, while promoting constructor injection for better testability.
11. DevTools:
- Spring Boot DevTools is a developer-friendly feature that provides:
- Automatic restart: Automatically restarts the application when code changes are detected.
- Live reload: Automatically refreshes the browser when changes are made to static files.
- These features help speed up development time and improve the feedback loop.
12. Security:
- Spring Boot integrates well with Spring Security to add authentication and authorization to your applications easily.
- Using Spring Boot starters (
spring-boot-starter-security), you can quickly set up basic security configurations and customize them as needed.
13. CommandLineRunner and ApplicationRunner:
- You can implement CommandLineRunner or ApplicationRunner interfaces to run code at startup, useful for initializing resources, loading initial data, or performing any task after the application context is loaded.
14. Modular Architecture:
- Spring Boot applications are modular, allowing you to break down the application into independent modules for better maintenance and scalability, making it especially useful for microservices-based architectures.
15. Spring Boot Initializr:
- Spring Boot Initializr is a web-based tool (https://start.spring.io/) that helps generate Spring Boot project skeletons with your selected dependencies. It’s a quick way to bootstrap new Spring projects.
16. Logging:
- Spring Boot supports logging using libraries like Logback, Log4j2, and Java Util Logging (JUL) out of the box.
- Log settings can be customized through the
application.propertiesorapplication.ymlfile.
Endpoints:
- Endpoints are the URLs where the resources are accessed. They define the base URI followed by the resource path.
- Example: http://localhost:8080/welcome to access the users’ resource.
Example of a Simple REST API with Spring Boot
Step-1 first goes to start.spring.io

Step-2 Select Maven and Java .
Maven is a powerful build automation and project management tool primarily used in Java projects. It is a part of the Apache Software Foundation and has become a standard in the Java ecosystem for managing project dependencies, compiling code, running tests, and building project artifacts like JARs or WARs.
Step-3 Enter the GroupId
- GroupId is a unique identifier that defines the group or organization under which a particular project is categorized.
- The GroupId is often associated with the company or organization that maintains the project or the primary package structure of the project.
<groupId>com.rkdigitalschool.security.test</groupId>
<artifactId>rest_security</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>rest_security</name>
Step-4 Enter the ArtifactId
The ArtifactId in Maven is a unique identifier for a specific project or module within a group. It represents the name of the artifact that Maven will generate after the build process.
Additionally, The ArtifactId is combined with the GroupId and version to create a unique identifier for each artifact in the Maven repository
<groupId>com.rkdigitalschool.security.test</groupId>
<artifactId>rest_security</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>rest_security</name>
Step-5 Add dependency Spring web and Dev tools
Spring web dependency: The Spring Web dependency is a core module in the Spring Framework that provides essential components and libraries for building web applications and RESTful web services.
Additionally, the Spring Web dependency in your project provides features for building web applications with the Spring MVC architecture
Dev tools dependency : The Spring Boot DevTools dependency is a module in the Spring Boot framework that provides several utilities to enhance the development experience.
Additionally, It enhances the efficiency of building and testing Spring Boot applications with features like automatic restarts, live reloads, and development-specific configurations
【4K 60FPS ACTION CAMERA】- Professional 4K/60FPS, 2.7K/30FPS, 1080P/120FPS, 720P/120FPS Video, and 20MP photo resolution enables you to capture all your adventures in high resolution. This action camera also comes with a built-in microphone to record audio (Audio may be less audible when using the waterproof case).
Step-6 Generate the Skelton project and import in your workspace .
- RkDigitalSchoolbankApplication.java
package com.rkdigitalschool.security.test;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class RkDigitalSchoolbankApplication {
public static void main(String[] args) {
SpringApplication.run(RkDigitalSchoolbankApplication.class, args);
}
}
2 WelcomeContoller.java
package com.rkdigitalschool.security.test.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class WelcomeContoller {
@GetMapping(“/welcome”)
public String sayWelcome() {
System.out.println(“Welcome to Spring Application with out security”);
return “Welcome to Spring Application with out security”;
}
}
Step-7 deploy the project test through postman

Rest Api With Spring Security
Update the below file
pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
application.propeties
spring.application.name=rest_security
spring.security.user.name=user
spring.security.user.password=12345
logging.level.org.springframework.security=TRACE
After making the changes, please proceed with deploying the application.

Please provide the username and password as specified in the application.properties file.