Spring Boot Controller not mapping

前端 未结 9 1464
慢半拍i
慢半拍i 2021-02-05 02:55

I have used STS and now I am using IntelliJ Ultimate Edition but I am still getting the same output. My controller is not getting mapped thus showing 404 error. I am completely

相关标签:
9条回答
  • 2021-02-05 03:04

    Adding @ComponentScan(com.webservice) in main class above @SpringBootApplication will resolve your problem. Refer below code

    package com.webservice.demo;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.context.annotation.ComponentScan;
    
    @ComponentScan(com.webservice)
    @SpringBootApplication
    public class DemoApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(DemoApplication.class, args);
        }
    }
    
    0 讨论(0)
  • 2021-02-05 03:05

    I too had the similar issue and was able to finally resolve it by correcting the source package structure following this

    Your Controller classes are not scanned by the Component scanning. Your Controller classes must be nested below in package hierarchy to the main SpringApplication class having the main() method, then only it will be scanned and you should also see the RequestMappings listed in the console output while Spring Boot is getting started.

    Tested on Spring Boot 1.5.8.RELEASE

    But in case you prefer to use your own packaging structure, you can always use the @ComponentScan annotation to define your basePackages to scan.

    0 讨论(0)
  • 2021-02-05 03:11

    In my case, it was missing the dependency from pom.xml, otherwise everything compiled just fine. The 404 and missing mappings info from Spring logs were the only hints.

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    
    0 讨论(0)
  • 2021-02-05 03:11

    I also had trouble with a similar issue and resolved it using the correct package structure as per below. After correction, it is working properly. e.g.

    • Spring Application Main Class is in package com.example
    • Controller Classes are in package com.example.controller
    0 讨论(0)
  • 2021-02-05 03:13

    I found the answer to this. This was occurring because of security configuration which is updated in newer versions of Spring Framework. So i just changed my version from 1.5.4 to 1.3.2

    0 讨论(0)
  • 2021-02-05 03:14

    In my opinion, this visibility problem comes when we leave the component scan to Spring which has a particular way of looking for the classes using standard convention. In this scenario as the Starter class(DemoApplication)is in com.webservice.demo package, putting Controller one level below will help Spring to find the classes using the default component scan mechanism. Putting HelloController under com.webservice.demo.controller should solve the issue.

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