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

前端 未结 18 1482
天命终不由人
天命终不由人 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:27

    You need to modify the Starter-Application class as shown below.

    @SpringBootApplication
    
    @EnableAutoConfiguration
    
    @ComponentScan(basePackages="com.nice.application")
    
    @EnableJpaRepositories("com.spring.app.repository")
    
    public class InventoryApp extends SpringBootServletInitializer {..........
    

    And update the Controller, Service and Repository packages structure as I mentioned below.

    Example: REST-Controller

    package com.nice.controller; --> It has to be modified as
    package com.nice.application.controller;

    You need to follow proper package structure for all packages which are in Spring Boot MVC flow.

    So, If you modify your project bundle package structures correctly then your spring boot app will work correctly.

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

    SpringBoot developers recommend to locate your main application class in a root package above other classes. Using a root package also allows the @ComponentScan annotation to be used without needing to specify a basePackage attribute. Detailed info But be sure that the custom root package exists.

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

    I had this issue and what you need to do is fix your packages. If you downloaded this project from http://start.spring.io/ then you have your main class in some package. For example if the package for the main class is: "com.example" then and your controller must be in package: "com.example.controller". Hope this helps.

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

    for me, I was adding spring-web instead of the spring-boot-starter-web into my pom.xml

    when i replace it from spring-web to spring-boot-starter-web, all maping is shown in the console log.

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

    Place your springbootapplication class in root package for example if your service,controller is in springBoot.xyz package then your main class should be in springBoot package otherwise it will not scan below packages

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

    Replace @RequestMapping( "/item" ) with @GetMapping(value="/item", produces=MediaType.APPLICATION_JSON_VALUE).

    Maybe it will help somebody.

    0 讨论(0)
提交回复
热议问题