Spring Boot example –
- Go to Spring Initializer
- Select Project as Maven Project
- Select Language as Java
- Select Spring Boot with the latest version (not the SNAPSHOT version)
- Enter details in Project Metadata
- Group – Enter package name
- Artifact – Enter the project name
- Name – Enter the project name
- Add dependencies
- Web
- DevTools
- Generate project
- Extract the zip file and import the project in IDE
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.4.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.misstechack</groupId>
<artifactId>spring-boot-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-boot-demo</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-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</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>
Student.java
package com.misstechack.demo;
public class Student {
private String rollNo;
private String name;
public String getRollNo() {
return rollNo;
}
public void setRollNo(String rollNo) {
this.rollNo = rollNo;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Controller Class – StudentController.java
package com.misstechack.demo;
import java.util.HashMap;
import java.util.Map;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class StudentController {
private static Map<String, Student> repo = new HashMap<>();
static {
Student student1 = new Student();
student1.setRollNo("1");
student1.setName("Amar");
repo.put(student1.getRollNo(), student1);
Student student2 = new Student();
student2.setRollNo("2");
student2.setName("Dolly");
repo.put(student2.getRollNo(), student2);
}
@GetMapping("/Students")
public ResponseEntity<Object> getStudent() {
return new ResponseEntity<>(repo.values(), HttpStatus.OK);
}
@PostMapping("/Students")
public ResponseEntity<Object> createStudent(@RequestBody Student Student) {
repo.put(Student.getRollNo(), Student);
return new ResponseEntity<>("Student is created successfully", HttpStatus.CREATED);
}
@PutMapping("/Students/{id}")
public ResponseEntity<Object> updateStudent(@PathVariable("id") String id, @RequestBody Student Student) {
repo.remove(id);
Student.setRollNo(id);
repo.put(id, Student);
return new ResponseEntity<>("Student is updated successfully", HttpStatus.OK);
}
@DeleteMapping(value="/Students/{id}")
public ResponseEntity<Object> deleteStudent(@PathVariable("id") String id) {
repo.remove(id);
return new ResponseEntity<>("Student is deleted successfully", HttpStatus.OK);
}
}
Application Class
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}