Reputation:
I'm trying to create a basic controller on spring boot but I get:
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Fri Dec 14 21:21:36 ART 2018 There was an unexpected error (type=Not Found, status=404).
No message available
error. I have the controller in a subpackage of the package where the application is located as it should be.
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.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</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-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>
HomeController.java:
package com.example.demo.home;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class HomeController {
@RequestMapping("/")
public String home(){
return "home";
}
}
DemoApplication.java:
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
Console output:
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.1.1.RELEASE)
2018-12-14 21:16:46.179 INFO 5420 --- [ main] com.example.demo.DemoApplication : Starting DemoApplication on DESKTOP-BMTCH27 with PID 5420 (C:\Users\Usuario\eclipse-workspace\Project\target\classes started by Usuario in C:\Users\Usuario\eclipse-workspace\Project)
2018-12-14 21:16:46.179 INFO 5420 --- [ main] com.example.demo.DemoApplication : No active profile set, falling back to default profiles: default
2018-12-14 21:16:51.395 INFO 5420 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2018-12-14 21:16:51.433 INFO 5420 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2018-12-14 21:16:51.433 INFO 5420 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/9.0.13
2018-12-14 21:16:51.464 INFO 5420 --- [ 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\jre1.8.0_192\bin;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;C:/Program Files/Java/jre1.8.0_192/bin/server;C:/Program Files/Java/jre1.8.0_192/bin;C:/Program Files/Java/jre1.8.0_192/lib/amd64;C:\Program Files (x86)\Common Files\Oracle\Java\javapath;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\ATI Technologies\ATI.ACE\Core-Static;c:\Program Files (x86)\Microsoft SQL Server\110\Tools\Binn\;c:\Program Files\Microsoft SQL Server\110\Tools\Binn\;c:\Program Files\Microsoft SQL Server\110\DTS\Binn\;c:\Program Files (x86)\Microsoft SQL Server\110\Tools\Binn\ManagementStudio\;c:\Program Files (x86)\Microsoft SQL Server\110\DTS\Binn\;C:\Program Files\dotnet\;C:\Program Files\Microsoft SQL Server\130\Tools\Binn\;C:\WINDOWS\System32\OpenSSH\;C:\Users\Usuario\AppData\Local\Microsoft\WindowsApps;;C:\Users\Usuario\Desktop;;.]
2018-12-14 21:16:51.871 INFO 5420 --- [ main] org.apache.jasper.servlet.TldScanner : At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
2018-12-14 21:16:51.871 INFO 5420 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2018-12-14 21:16:51.871 INFO 5420 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 5489 ms
2018-12-14 21:16:52.449 INFO 5420 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2018-12-14 21:16:52.959 INFO 5420 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2018-12-14 21:16:52.975 INFO 5420 --- [ main] com.example.demo.DemoApplication : Started DemoApplication in 8.155 seconds (JVM running for 9.247)
2018-12-14 21:17:00.448 INFO 5420 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2018-12-14 21:17:00.448 INFO 5420 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2018-12-14 21:17:00.462 INFO 5420 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 13 ms
Upvotes: 1
Views: 3839
Reputation: 2755
@Controller
annotation represents the Spring MVC Controller. If you mark your class with @Controller
annotation you have to put @ResponseBody
on the handler methods.
On the other side, @RestController
annotation is a specialized version of @Controller
which adds @Controller
and @ResponseBody
annotation automatically so there is no need to add @ResponseBody
to our handler methods.
@RestController
public class HomeController {
@RequestMapping("/")
public String home(){
return "home";
}
}
or
@Controller
public class HomeController {
@RequestMapping("/")
@ResponseBody
public String home(){
return "home";
}
}
Upvotes: 1
Reputation: 29
It can be from @Controller annotion. Please change it to @RestController
From spring boot documentation https://spring.io/guides/gs/rest-service/
In Spring’s approach to building RESTful web services, HTTP requests are handled by a controller. These components are easily identified by the @RestController annotation, and the GreetingController below handles GET requests for /greeting by returning a new instance of the Greeting class:
Upvotes: 1
Reputation: 441
Thymeleaf should be included in project's pom.xml as a dependency:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
Also the home.html file should be moved from templates to static directory.
For more details have a look at Serving Web Content with Spring MVC.
Upvotes: 0
Reputation: 1201
For your code/configuration, it will look for the html
file under static
or webapp
folder, meanwhile your home controller should return home.html
not home
.
Upvotes: 0
Reputation: 3678
For information : If you do not specify any mapping this method will resolve all the http request i.e. you can send GET, POST, HEAD, OPTIONS, PUT, PATCH, DELETE, TRACE
request to the specified url and it will be resolved.
For you case, you change just your controller to look like :
package com.example.demo.home;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@RestController
public class HomeController {
@RequestMapping("/") // Or you can do : @GetMapping("/")
public String home(){
return "home";
}
}
The url:
http://localhost:8080/
Upvotes: 0