Spring Boot Controller not showing

I have used STS and now I am using IntelliJ Ultimate Edition, but I still get the same result. My controller is not getting a mapping, thus displaying a 404 error. I am completely new to the Spring Framework.

DemoApplication.java

package com.webservice.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

      

HelloController.java

package com.webservice.demo;


import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

    @RequestMapping("/hello")
    public String sayHello(){
        return "Hey";
    }

}

      

Console output

com.webservice.demo.DemoApplication      : Starting DemoApplication on XFT000159365001 with PID 11708 (started by Mayank Khursija in C:\Users\Mayank Khursija\IdeaProjects\demo)
    2017-07-19 12:59:46.150  INFO 11708 --- [           main] com.webservice.demo.DemoApplication      : No active profile set, falling back to default profiles: default
    2017-07-19 12:59:46.218  INFO 11708 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@238e3f: startup date [Wed Jul 19 12:59:46 IST 2017]; root of context hierarchy
    2017-07-19 12:59:47.821  INFO 11708 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8211 (http)
    2017-07-19 12:59:47.832  INFO 11708 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
    2017-07-19 12:59:47.832  INFO 11708 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.15
    2017-07-19 12:59:47.944  INFO 11708 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
    2017-07-19 12:59:47.944  INFO 11708 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1728 ms
    2017-07-19 12:59:47.987  INFO 11708 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*]
    2017-07-19 12:59:48.510  INFO 11708 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
    2017-07-19 12:59:48.519  INFO 11708 --- [           main] o.s.c.support.DefaultLifecycleProcessor  : Starting beans in phase 0
    2017-07-19 12:59:48.634  INFO 11708 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8211 (http)
    2017-07-19 12:59:48.638  INFO 11708 --- [           main] com.webservice.demo.DemoApplication      : Started DemoApplication in 2.869 seconds (JVM running for 3.44)

      

+12


source to share


7 replies


I found the answer to this question. This was due to the security configuration being updated in newer versions of the Spring Framework. So I just changed my version from 1.5.4 to 1.3.2



0


source


I also had a similar problem and was able to finally solve it by fixing the source package structure following this instruction.

Your controller classes are not scanned when scanning components. Your controller classes should be nested below the package hierarchy in the main SpringApplication class having a main () method, then only it will be scanned and you should also see the RequestMappings listed in the console output when Spring Boot starts up.



Tested on Spring Boot 1.5.8. RELEASE

But if you prefer to use your own packaging structure, you can always use annotation @ComponentScan

to define yours basePackages

to scan.

+41


source


Because of DemoApplication.class

and HelloController.class

in the same package
Find your main application class in the root package above other classes
Check out the Spring Boot documentation. Defining the class of the main application.

Using the root package also allows you to scan components just for your project.

For example, in your case it looks like this:

com.webservice.demo.DemoApplication


com.webservice.demo.controller.HelloController

+7


source


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

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

      

+2


source


It depends on several properties:

  • server.contextPath

    property in application properties. If it's set to any value, you need to add that to your request url. If there is no such property, add this line to application.propertiesserver.contextPath=/

  • method

    in @RequestMapping

    seems to have no meaning and, therefore, according to the documentation , it should display all methods. However, if you want it to listen to any particular method, you can set it to saymethod = HttpMethod.GET

0


source


When I was creating a new project, checked Dependency Web >> Jersey (JAX-RS) and ended up in pom.xml:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jersey</artifactId>
</dependency>

      

Everything compiled, no display in the log, the same situation as yours.

So, I manually added to pox.xml:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

      

And everything starts to work.

After that I created a new project with Dependency only Web >> Web check in wizard and I got what I want right from the start.

In my case, I didn't have to do anything with the server.contextPath with any of the methods .

0


source


In my opinion, this visibility issue occurs when we leave component scanning in Spring, which has a special way of finding classes using a standard convention. In this scenario, since the Starter (DemoApplication) class is in the com.webservice.demo package, placing the controller one level down will help Spring find the classes using the default bean scanning engine. Putting HelloController in com.webservice.demo.controller should fix the problem.

0


source







All Articles