javaspring-bootspring-restcontroller

problem - "404 error" for localhost:8081/question/allQuestions


application properties:

# Database Connection Properties
spring.datasource.url=jdbc:mysql://localhost:3306/quizmaker
spring.datasource.username=root
spring.datasource.password=Corona@123

# Specify the JDBC driver for MySQL
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

# Hibernate properties
spring.jpa.hibernate.ddl-auto=update 

# # Connection pool settings
# spring.datasource.hikari.connection-timeout=20000
# spring.datasource.hikari.minimum-idle=5
# spring.datasource.hikari.maximum-pool-size=20
# spring.datasource.hikari.idle-timeout=300000

# Logging SQL statements
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true
server.port=8081

postman picture result

i tried to excute above program..but i got same error for both @Getmapping and @PostMapping

{"timestamp":"2024-02-11T11:22:56.192+00:00",
"status":404,
"error":"Not Found",
"path":"/question/allQuestions"}

controller:

@RestController
@RequestMapping(path = "/question")

public class QuestionController{

    private QuestionService questionService;

    public QuestionController (QuestionService questionService)
    { 
        this.questionService = questionService;
    }
    // ------Read Operation----
    @GetMapping(path = "/allQuestions")

        public List<com.example.model.Question> getAllQuestions () {
        return questionService.getAllQuestions();
    }
    @GetMapping(path = "/category/{category}")
    public List<com.example.model.Question> getQuestionsByCategory (@PathVariable String category) {
    return questionService.getQuestionsByCategory( category);
    }
    //------Create Operation---
    @PostMapping(path = "/create")
    public String createQuestion (@RequestBody Question question) {
    return questionService.createQuestion ((com.example.model.Question) question);
    }
}

postman result

it doesn't show any error on terminal...still i didn't get desired output


Solution

  • The path of getAllQuestions() has been defined with /allQuestions but you invoke API with /allQuestion API path.