0

I created a Spring Boot application with spring-boot-starter-web using STS. Then created a RestController in a sub package and started the app. It showed no errors but rest endpoint not working. Hitting localhost:8080/ gives 404 error.

I tried using @ComponentScan, maven clean and maven install.

TestApplication.java

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class TestApplication {

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

}

TestController.java

package com.example.demo;

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

@RestController
public class TestController {

    @RequestMapping("/")
    public String test() {
        System.out.println("test");
        return "test";
    }
}

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>test</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>test</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jersey</artifactId>
        </dependency>
        <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>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

Logs:

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.3.RELEASE)

2019-03-27 14:05:56.588  INFO 12452 --- [           main] com.example.demo.TestApplication         : Starting TestApplication on W10INMUM01D1184 with PID 12452 (C:\Users\mhamdule\Documents\workspace-sts-3.9.6.RELEASE\test\target\classes started by mhamdule in C:\Users\mhamdule\Documents\workspace-sts-3.9.6.RELEASE\test)
2019-03-27 14:05:56.591  INFO 12452 --- [           main] com.example.demo.TestApplication         : No active profile set, falling back to default profiles: default
2019-03-27 14:05:57.207  INFO 12452 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2019-03-27 14:05:57.224  INFO 12452 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2019-03-27 14:05:57.224  INFO 12452 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.16]
2019-03-27 14:05:57.230  INFO 12452 --- [           main] o.a.catalina.core.AprLifecycleListener   : The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [C:\Program Files\Java\jdk1.8.0_171\bin;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;C:/Program Files/Java/jre1.8.0_171/bin/server;C:/Program Files/Java/jre1.8.0_171/bin;C:/Program Files/Java/jre1.8.0_171/lib/amd64;C:\Program Files (x86)\Common Files\Oracle\Java\javapath;C:\Program Files (x86)\Java\jdk1.8.0_144\bin;D:\app\client\Administrator\product\12.1.0\client_1\bin;D:\Oracle\client\product\12.1.0\client_1;D:\Oracle\client\product\12.1.0\client_1\bin;C:\Program Files (x86)\Intel\iCLS Client\;C:\Program Files\Intel\iCLS Client\;C:\ProgramData\Oracle\Java\javapath;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files\Intel\Intel(R) Management Engine Components\IPT;c:\sybase\dll;C:\Program Files\Microsoft SQL Server\130\Tools\Binn\;C:\Program Files (x86)\Microsoft SQL Server\120\DTS\Binn\;C:\Program Files (x86)\Microsoft SQL Server\140\DTS\Binn\;C:\Program Files\TortoiseSVN\bin;C:\Program Files (x86)\Microsoft SQL Server\120\Tools\Binn\ManagementStudio\;C:\Program Files (x86)\Microsoft SQL Server\120\Tools\Binn\;C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\PrivateAssemblies\;C:\Program Files (x86)\Microsoft SQL Server\Client SDK\ODBC\130\Tools\Binn\;C:\Program Files (x86)\Microsoft SQL Server\140\Tools\Binn\;C:\Program Files\nodejs\;C:\Users\mhamdule\AppData\Local\Microsoft\WindowsApps;C:\Users\mhamdule\AppData\Local\Programs\Git\cmd;C:\Users\mhamdule\Downloads\spring-tool-suite-3.9.6.RELEASE-e4.9.0-win32-x86_64\sts-bundle\sts-3.9.6.RELEASE;;.]
2019-03-27 14:05:57.337  INFO 12452 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2019-03-27 14:05:57.337  INFO 12452 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 717 ms
2019-03-27 14:05:57.535  INFO 12452 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2019-03-27 14:05:57.538  INFO 12452 --- [           main] com.example.demo.TestApplication         : Started TestApplication in 1.236 seconds (JVM running for 1.798)
g00glen00b
  • 41,995
  • 13
  • 95
  • 133

4 Answers4

0

The code looks fine, maybe some nested dependencies are not present, try to clean and build with maven, re-run and it should be fine.

MSH
  • 81
  • 1
  • 3
0

delete the version property from the pom.xml file

Mourad9
  • 5
  • 5
0

It works for any version of spring boot except 2.1.3 . I am using jdk_1.8.0171 . I am not sure if there is some issue with the latest release if spring boot 2.1.3.

0

In earlier versions of Spring Boot like 2.0.3, in the console, it used to show all the mappings.

<version>2.1.3.RELEASE</version>

But in the version you are using 2.1.3, it won't show the mappings in console when starting a Spring Boot application.

Ref : eclipse spring boot console log does not print mapped controller info

But from your code snippets, I think even though in the logs it is not shown as mapped, still you should be getting result when you hit the endpoint.

If that's not the case and you are getting an error, I can only think of one issue, the package structure is not what is mentioned in the code snippets. 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. Ensure that is the case.

Else, you can use something like @ComponentScan(basePackages = {"com.example.*"})

mkkr1009
  • 91
  • 3