Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring REST API controller is not called

i have following code

Controller

@RestController 
@RequestMapping("/") 
public class RequestHandler {
    @RequestMapping(value = "/demo", method = RequestMethod.POST)
     @ResponseBody 
    public Object showDemo() {

        return "Post method";
    }


    @RequestMapping(value = { "/getdemo" }, method = RequestMethod.GET)
      @ResponseBody 
    public  Object showgetDemo() {
  System.out.println("hello");
        return "get Method";
    }
}

My pom xml

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>Website2015</groupId>
  <artifactId>Website2015</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.2.1.RELEASE</version>
        <relativePath />
    </parent>
    <dependencies>
<dependency> 
<groupId>org.springframework.boot</groupId> 
<artifactId>spring-boot-starter-web</artifactId> 
</dependency> 

<dependency> 
<groupId>org.springframework.boot</groupId> 
<artifactId>spring-boot-starter-test</artifactId> 
<scope>test</scope> 
</dependency> 
</dependencies>  
  <build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.3</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
      <plugin>
        <artifactId>maven-war-plugin</artifactId>
        <version>2.6</version>
        <configuration>
          <warSourceDirectory>WebContent</warSourceDirectory>
          <failOnMissingWebXml>false</failOnMissingWebXml>
        </configuration>
      </plugin>
    </plugins>

  </build>
</project>

and to launch Spring boot application :

@Configuration
@EnableWebMvc
@SpringBootApplication
public class AppLauncher  {

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

    }

    private static Class<AppLauncher> applicationClass = AppLauncher.class;

}

Server logs

    2015-10-21 16:07:59.314  INFO 12892 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2015-10-21 16:07:59.470  INFO 12892 --- [           main] o.apache.catalina.core.StandardService   : Starting service Tomcat
2015-10-21 16:07:59.471  INFO 12892 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.0.15
2015-10-21 16:07:59.561  INFO 12892 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2015-10-21 16:07:59.561  INFO 12892 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1158 ms
2015-10-21 16:07:59.716  INFO 12892 --- [ost-startStop-1] o.s.b.c.e.ServletRegistrationBean        : Mapping servlet: 'dispatcherServlet' to [/]
2015-10-21 16:07:59.719  INFO 12892 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'characterEncodingFilter' to: [/*]
2015-10-21 16:07:59.921  INFO 12892 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2015-10-21 16:07:59.921  INFO 12892 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],methods=[],params=[],headers=[],consumes=[],produces=[text/html],custom=[]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest)
2015-10-21 16:08:00.119  INFO 12892 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@25af5db5: startup date [Wed Oct 21 16:07:58 CEST 2015]; root of context hierarchy
2015-10-21 16:08:00.275  INFO 12892 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2015-10-21 16:08:00.322  INFO 12892 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2015-10-21 16:08:00.323  INFO 12892 --- [           main] c.xx.yy.misc.AppLauncher   : Started AppLauncher in 2.16 seconds (JVM running for 2.496)

I am using POSTMAN Rest client .All i am getting is

{ "timestamp": 1445436550585, "status": 404, "error": "Not Found", "message": "No message available", "path": "/get" }

i cant find mapping in server logs as well . My best guess after spending a lot of time is false project structure.

I created new Dynamic web project and converted it into Maven , ofcourse i tried it with simple java project and then converted it into maven .

Please give me some pointers and if more info is required please let me know thanks.

like image 563
lesnar Avatar asked Dec 19 '22 23:12

lesnar


1 Answers

The default @SpringBootApplication enables component scan only for the package of the application class and all subpackages. Since the package of AppLauncher is com.demo.misc and the package of RequestHandler is com.demo.controller, component scan won't detect the bean class.

Solution:
If you are using Spring Boot 1.2.x:

Either add the following annotation to your TestApp class:

@ComponentScan({"com.demo.controller", "com.demo.misc"})

or move the RequestHandler to a subpackage of com.demo.misc, e.g. com.demo.misc.controller

If you are already using Spring Boot 1.3.x:

Add the following parameter to your @SpringBootApplication annotation:

@SpringBootApplication(scanBasePackages = {"com.demo.controller", "com.demo.misc"})    
like image 57
dunni Avatar answered Dec 26 '22 00:12

dunni