I'm trying to make a simple CRUD of Students using Spring Boot. I am storing data in a class inside Spring, and not in files or a DB. So my changes are lost everytime I run it again. I'm planning to use MongoDB, but now I'm just trying this simple example.
When I first tested it, it worked fine. The problem was when I tried to run it on an image on Docker. Although my container is running, my requests don't work anymore.
I set the mapping to be in localhost:8080/api/v1/person
This is an example of a working POST method before the Docker:
Body:
{
"ra": "20124987",
"name": "Norman"
}
Response:
{
"status": "Success",
"message": "Succesfully added student Norman",
"code": 200
}
And now this is what I get in the response:
{
"timestamp": "2022-09-21T23:19:39.296+00:00",
"status": 404,
"error": "Not Found",
"path": "/api/v1/person"
}
It also doesn't work with any method that I created, and they used to work before.
This is my Controller Class:
package br.com.qtivate.server.api;
import br.com.qtivate.server.model.Response;
import br.com.qtivate.server.model.Student;
import br.com.qtivate.server.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.UUID;
@RequestMapping("api/v1/person")
@RestController
public class StudentController {
private final StudentService studentService;
@Autowired
public StudentController(StudentService studentService) {
this.studentService = studentService;
}
@PostMapping
public Response addStudent(@RequestBody Student student) {
studentService.addStudent(student);
return new Response("Success", "Succesfully added student " + student.getName(), 200);
}
@GetMapping
public List<Student> getAllStudents() {
return studentService.getAllStudents();
}
@GetMapping(path = "{id}")
public Student getStudentById(@PathVariable("id") UUID id) {
return studentService.getStudentById(id)
.orElse(null);
}
@PutMapping(path = "{id}")
public Response updateStudentById(@PathVariable("id") UUID id, @RequestBody Student student) {
if (studentService.updateStudentById(id, student) == 0)
return new Response("Error", "Student not found", 400);
return new Response("Success", "Successfully updated student", 200);
}
@DeleteMapping(path = "{id}")
public Response deleteStudentById(@PathVariable("id") UUID id) {
if (studentService.deleteStudentById(id) == 0)
return new Response("Error", "Student not found", 400);
return new Response("Success", "Successfully deleted student", 200);
}
}
And this is my Dockerfile:
FROM openjdk:18-jdk-alpine
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java","-jar","/app.jar"]