Spring Boot: Cannot access REST Controller on localhost (404)

前端 未结 18 1481
天命终不由人
天命终不由人 2020-11-27 12:05

I am trying to adapt the REST Controller example on the Spring Boot website. Unfortunately I\'ve got the following error when I am trying to access the localhost:8080/

相关标签:
18条回答
  • 2020-11-27 12:41

    I got the 404 problem, because of Url Case Sensitivity.

    For example @RequestMapping(value = "/api/getEmployeeData",method = RequestMethod.GET) should be accessed using http://www.example.com/api/getEmployeeData. If we are using http://www.example.com/api/getemployeedata, we'll get the 404 error.

    Note: http://www.example.com is just for reference which i mentioned above. It should be your domain name where you hosted your application.

    After a lot of struggle and apply all the other answers in this post, I got that the problem is with that url only. It might be silly problem. But it cost my 2 hours. So I hope it will help someone.

    0 讨论(0)
  • 2020-11-27 12:42

    Same 404 response I got after service executed with the below code

    @Controller
    @RequestMapping("/duecreate/v1.0")
    public class DueCreateController {
    
    }
    

    Response:

    {
    "timestamp": 1529692263422,
    "status": 404,
    "error": "Not Found",
    "message": "No message available",
    "path": "/duecreate/v1.0/status"
    }
    

    after changing it to below code I received proper response

    @RestController
    @RequestMapping("/duecreate/v1.0")
    public class DueCreateController {
    
    }
    

    Response:

    {
    "batchId": "DUE1529673844630",
    "batchType": null,
    "executionDate": null,
    "status": "OPEN"
    }
    
    0 讨论(0)
  • 2020-11-27 12:43

    The problem is with your package structure. Spring Boot Application has a specific package structure to allow spring context to scan and load various beans in its context.

    In com.nice.application is where your Main Class is and in com.nice.controller, you have your controller classes.

    Move your com.nice.controller package into com.nice.application so that Spring can access your beans.

    0 讨论(0)
  • 2020-11-27 12:45

    It could be that something else is running on port 8080, and you're actually connecting to it by mistake.

    Definitely check that out, especially if you have dockers that are bringing up other services you don't control, and are port forwarding those services.

    0 讨论(0)
  • 2020-11-27 12:48

    I had exact same error, I was not giving base package. Giving correct base package,ressolved it.

    package com.ymc.backend.ymcbe;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.context.annotation.ComponentScan;
    
    @SpringBootApplication
    @ComponentScan(basePackages="com.ymc.backend")
    public class YmcbeApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(YmcbeApplication.class, args);
        }
    
    }
    

    Note: not including .controller @ComponentScan(basePackages="com.ymc.backend.controller") because i have many other component classes which my project does not scan if i just give .controller

    Here is my controller sample:

    package com.ymc.backend.controller;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.CrossOrigin;
    import org.springframework.web.bind.annotation.PostMapping;
    import org.springframework.web.bind.annotation.RequestBody;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    
    @RestController
    @CrossOrigin
    @RequestMapping(value = "/user")
    public class UserController {
    
        @PostMapping("/sendOTP")
        public String sendOTP() {
            return "OTP sent";
        };
    
    
    }
    
    0 讨论(0)
  • 2020-11-27 12:50

    You can add inside the POM.

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-configuration-processor</artifactId>
        <version>XXXXXXXXX</version>
    </dependency>
    
    0 讨论(0)
提交回复
热议问题