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

104 Views Asked by At

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

3

There are 3 best solutions below

0
Faramarz Afzali On BEST ANSWER

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

0
Rami toumi On

Try to check your controller to investigate the cause of the 404 error. The error typically occurs when your application is unable to find the requested resource, such as the /question/allQuestion endpoint, in the controller.

1
Andrey Strelets On

Did you correctly register the paths in the controller?

For example:

@RestController
@RequestMapping(path = "/question")
public class QuestionController {

    @GetMapping(path = "/allQuestion")
    ResponseEntity<?> getAllQuestions() {
        return ResponseEntity.ok();
    }
}