1

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"]

1 Answers1

1

Try to add "EXPOSE yourPortHere" to your Dockerfile like so:

FROM openjdk:18-jdk-alpine
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java","-jar","/app.jar"]
EXPOSE 8080

And start your container with a port mapping:

docker run -d -p 8080:8080 yourImage/yourTag

If this doesn't work check your container port mapping by running:

docker port yourContainerName

See:

BigRocket
  • 11
  • 1
  • Even doing this it still doesn't work – Leonardo Hana Sep 26 '22 at 21:21
  • Hi @LeonardoHana Did you ever found a root cause of the issue? I am facing something similar for a jar file that I don't have code for. The jar file works well when I run it using: java -jar app.jar but end up getting error for the images that app might be hosting in the static resource folder : [{timestamp=Thu Aug 17 23:56:15 GMT 2023, status=404, error=Not Found, message=No message available, path=/%7B%7Bimageicon%7D%7D}] – Abhishek Shah Aug 18 '23 at 00:06