@SpringBootApplication public class SpringApiApplication extends SpringBootServletInitializer implements WebApplicationInitializer { @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(SpringApiApplication.class); } public static void main(String[] args) throws Exception{ SpringApplication.run(SpringApiApplication.class, args); } }
*This is my controller page*
package com.javaproject.springapi.controller;
import java.util.List;
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.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.javaproject.springapi.model.Student;
import com.javaproject.springapi.service.StudentService;
@RestController
@RequestMapping("/api" )
public class StudentController {
private StudentService studentService;
public StudentController(StudentService studentService) {
super();
this.studentService = studentService;
}
@PostMapping()
public ResponseEntity< Student> addStudent(@RequestBody Student student){
return new ResponseEntity<Student> (studentService.addStudent(student),HttpStatus.CREATED);
}
@GetMapping
public List<Student> getStudents(){
return studentService.getStudents();
}
* get student by id*
@GetMapping("{id}")
public ResponseEntity<Student> getStudentById(@PathVariable("id")long stuId){
return new ResponseEntity<Student>(studentService.getStudentById(stuId),HttpStatus.OK);
}
*Update Method*
@PutMapping("{id}")
public ResponseEntity<Student> updateStudent(@PathVariable("id")long id
,@RequestBody Student student){
return new ResponseEntity<Student> (studentService.updateStudent(student, id),HttpStatus.OK);
}
* //Build Delete Method*
@DeleteMapping("{id}")
public ResponseEntity<String> deleteStudent(@PathVariable("id")long id){
studentService.deleteStudent(id);
return new ResponseEntity<String>("Employee Deleted Successfully!.",HttpStatus.OK);
}
}
This is my pom.xml 4.0.0 org.springframework.boot spring-boot-starter-parent 2.7.3 com.springboot.app springboot-war-demo 0.0.1-SNAPSHOT *I created a war * war springboot-war-demo Spring Boot WAR Demo <java.version>17</java.version> org.springframework.boot spring-boot-starter-web
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</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>