Springboot won't load my HTML page using Thymeleaf - java

Already been on these topics but haven't worked for me:
Topic1 No #EnableWebMvc annotation in my project
This is the dependency i used for Thymeleaf:
<!-- https://mvnrepository.com/artifact/org.thymeleaf/thymeleaf -->
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf</artifactId>
<version>3.0.11.RELEASE</version>
</dependency>
My new Controller named ViewController
package myrest.Viewcontroller;
//Imports
#Controller
public class ViewController {
public static String uploadDirectory = System.getProperty("user.dir")+"/uploads";
#RequestMapping("/uploadendpoint")
public String uploadPage(Model model)
{
return "uploadView";
}
}
The HTML named uploadView.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8"></meta>
<title>Insert title here</title>
</head>
<body>
<form action = "/upload" method="post" enctype="multipart/form-data">
<input type="file" name="files" multiple>
<input type="submit" value="Upload Files"></input>
</form>
</body>
</html>
Project's structure:
My Comments: I'm still getting a Whitelabel Error Page , at localhost:8082/uploadendpoint
Edit1:
My #SpringBootApplication class
package myrest;
import java.io.File;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import myrest.Viewcontroller.ViewController;
import myrest.controller.MainController;
#SpringBootApplication
public class RemoteapiApplication {
public static void main(String[] args) {
new File(ViewController.uploadDirectory).mkdir();
SpringApplication.run(RemoteapiApplication.class, args);
}
}
application.properties file:
server.port:8082
spring.servlet.multipart.max-file-size=15MB
spring.servlet.multipart.max-request-size=15MB
Server's logs
2020-01-07 14:37:56.708 INFO 9317 --- [ restartedMain] myrest.RemoteapiApplication : Starting RemoteapiApplication on ZenbookPro with PID 9317 (/home/pihill/Documents/workspace-sts-3.9.9.RELEASE/remoteapi/target/classes started by pihill in /home/pihill/Documents/workspace-sts-3.9.9.RELEASE/remoteapi)
2020-01-07 14:37:56.708 INFO 9317 --- [ restartedMain] myrest.RemoteapiApplication : No active profile set, falling back to default profiles: default
2020-01-07 14:37:56.770 WARN 9317 --- [ restartedMain] org.apache.tomcat.util.modeler.Registry : The MBean registry cannot be disabled because it has already been initialised
2020-01-07 14:37:56.781 INFO 9317 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8082 (http)
2020-01-07 14:37:56.782 INFO 9317 --- [ restartedMain] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2020-01-07 14:37:56.782 INFO 9317 --- [ restartedMain] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.29]
2020-01-07 14:37:56.784 INFO 9317 --- [ restartedMain] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2020-01-07 14:37:56.784 INFO 9317 --- [ restartedMain] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 75 ms
2020-01-07 14:37:56.801 INFO 9317 --- [ restartedMain] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2020-01-07 14:37:56.814 INFO 9317 --- [ restartedMain] o.s.b.d.a.OptionalLiveReloadServer : LiveReload server is running on port 35729
2020-01-07 14:37:56.820 INFO 9317 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8082 (http) with context path ''
2020-01-07 14:37:56.820 INFO 9317 --- [ restartedMain] myrest.RemoteapiApplication : Started RemoteapiApplication in 0.119 seconds (JVM running for 2930.389)
2020-01-07 14:37:56.821 INFO 9317 --- [ restartedMain] .ConditionEvaluationDeltaLoggingListener : Condition evaluation unchanged
2020-01-07 14:37:59.725 INFO 9317 --- [nio-8082-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2020-01-07 14:37:59.726 INFO 9317 --- [nio-8082-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2020-01-07 14:37:59.727 INFO 9317 --- [nio-8082-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 1 ms
Whitelabel error page:

It is like M.Deinum said. You are using Spring Boot so get the benefits from using the starter packages. In the spring-boot-starter-thymleaf package are more dependencies included than just in the org.thymeleaf 3.0.11.RELEASE.
Replace this in your pom.xml:
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf</artifactId>
<version>3.0.11.RELEASE</version>
</dependency>
with this:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
Then it works like charming.

Add dependency in your pom.xml:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
Add your Html file in src/main/resource/templates/test.html(select any name)
Then add this with html tag:
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
Create a Controller to open your html file:
#Controller
public class DemoController {
#GetMapping("/OpenTestFile")
public String OpenTestFile(Model model) {
return "test";
}
}
Open with this Url:
http://localhost:8082/OpenTestFile

Some times the IDE your using like intelliJ community version will not support MVC, please check with your IDE supports the requirements

Related

GET/POST method is not reading data sent by Postman but the request is reaching backend and status code is 200

#RestController
#RequestMapping("/incident")
public class EncryptJsonString {
#GetMapping("/test")
public String getTest(String a) {
System.out.println("yuty "+a);
return a;
}
}
-----------------------------------------------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 https://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.5.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.incident_tool</groupId>
<artifactId>incdt_tool</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>Incdt-Tracker</name>
<description>webservice for incident management tool</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</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----------------------------------------------------
2021-06-09 06:51:48.018 INFO 16256 --- [ main] c.incident.tool.IncdtTrackerApplication : Starting IncdtTrackerApplication using Java 15.0.2 on LAPTOP-03TT5H9L with PID 16256 (C:\Users\sk464\eclipse-workspace\Incdt-Tracker\target\classes started by sk464 in C:\Users\sk464\eclipse-workspace\Incdt-Tracker)
2021-06-09 06:51:48.021 INFO 16256 --- [ main] c.incident.tool.IncdtTrackerApplication : No active profile set, falling back to default profiles: default
2021-06-09 06:51:48.680 INFO 16256 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2021-06-09 06:51:48.733 INFO 16256 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 43 ms. Found 1 JPA repository interfaces.
2021-06-09 06:51:49.285 INFO 16256 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2021-06-09 06:51:49.296 INFO 16256 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2021-06-09 06:51:49.296 INFO 16256 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.46]
2021-06-09 06:51:49.442 INFO 16256 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2021-06-09 06:51:49.442 INFO 16256 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1377 ms
2021-06-09 06:51:49.643 INFO 16256 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2021-06-09 06:51:49.921 INFO 16256 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
2021-06-09 06:51:50.004 INFO 16256 --- [ main] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [name: default]
2021-06-09 06:51:50.062 INFO 16256 --- [ main] org.hibernate.Version : HHH000412: Hibernate ORM core version 5.4.31.Final
2021-06-09 06:51:50.255 INFO 16256 --- [ main] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations {5.1.2.Final}
2021-06-09 06:51:50.399 INFO 16256 --- [ main] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.PostgreSQL10Dialect
2021-06-09 06:51:51.282 INFO 16256 --- [ main] o.h.e.t.j.p.i.JtaPlatformInitiator : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
2021-06-09 06:51:51.294 INFO 16256 --- [ main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2021-06-09 06:51:51.364 WARN 16256 --- [ main] JpaBaseConfiguration$JpaWebConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
2021-06-09 06:51:51.994 INFO 16256 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2021-06-09 06:51:52.005 INFO 16256 --- [ main] c.incident.tool.IncdtTrackerApplication : Started IncdtTrackerApplication in 4.38 seconds (JVM running for 5.189)
2021-06-09 06:51:52.006 INFO 16256 --- [ main] o.s.b.a.ApplicationAvailabilityBean : Application availability state LivenessState changed to CORRECT
2021-06-09 06:51:52.007 INFO 16256 --- [ main] o.s.b.a.ApplicationAvailabilityBean : Application availability state ReadinessState changed to ACCEPTING_TRAFFIC
2021-06-09 06:52:18.351 INFO 16256 --- [nio-8080-exec-2] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2021-06-09 06:52:18.351 INFO 16256 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2021-06-09 06:52:18.354 INFO 16256 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet : Completed initialization in 1 ms
Output when Get request is made via Postman:
yuty null
screenshot of postman request screenshot of postman headers
I have tested it via Jax-RS Rest and it working fine, the problem is only occurring for Spring-boot. Postman is hitting the backend code successfully but the data is not getting maapped in method paramater. I am sending text/plain from Postman.
Try with this:
#RestController
#RequestMapping("/incident")
public class EncryptJsonString {
#GetMapping("/test/{a}")
public String getTest(#PathVariable("a") String a) {
System.out.println("yuty "+a);
return a;
}
}
and then use http://localhost:8080/incident/test/whatever. It should print yuty whatever.
You have 2 approaches here. One is the one that Alessandro just write. (Thats the best, your method is a GET so you must use PathVariables or QueryParams).
If you're actually trying to send plain text in the body of the request, you should add the #RequestBody annotation to your method argument
Like this:
#GetMapping("/test")
public String getTest(#RequestBody String a) {
System.out.println("yuty "+a);
return a;
}
But remember, you should avoid sending a body if your request is a GET.

Spring Boot Kotlin API returns 404 response for all endpoints

I'm using Spring Boot with Kotlin . The rest controller returns 404 error for all endpoints. In the starting logs the endpoints in the controller are not present. Any endpoint returns the same 404 response.
These are the steps I have tried,
Tried clearing cache/target
Moved all classes into single directory
Tried adding ComponentScan annotation but it is not accepted.
I'm using cmd line to run the project
Same output seen when running java -jar $jarfile
Checked annotations Repository , Service , RestController
Controller:
package com.example.controller
import org.springframework.web.bind.annotation.RestController
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.http.HttpStatus
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.PutMapping
import org.springframework.web.bind.annotation.DeleteMapping
import org.springframework.web.bind.annotation.RequestBody
import Cricketer
import CricketerRepository
import CricketerService
import org.springframework.web.bind.annotation.RequestMapping
#RestController
#RequestMapping("/api")
class CricketerController(private val cricketerService: CricketerService , private val cricketerRepository: CricketerRepository){
#GetMapping("/cricketers/{id}")
fun getCricketer(#PathVariable("id") id: Long):ResponseEntity<Cricketer> {
val cricketer = cricketerService.findById(id)
return ResponseEntity<Cricketer>(cricketer as Cricketer, HttpStatus.OK);
}
#GetMapping("/cricketers/")
fun getAllCricketers() :ResponseEntity<List<Cricketer>> {
var cricketersList: ArrayList<Cricketer> = cricketerService.getAllPlayers() as (ArrayList<Cricketer>)
return ResponseEntity<List<Cricketer>>(cricketersList, HttpStatus.OK)
}
#PostMapping("/cricketer/")
fun addCricketer(#RequestBody cricketer:Cricketer):ResponseEntity<Cricketer> {
val cCricketer : Cricketer = Cricketer(name = cricketer.name
, country = cricketer.country
, highestScore = cricketer.highestScore)
cricketerRepository.save(cCricketer)
return ResponseEntity<Cricketer>(cricketer , HttpStatus.OK)
}
#PutMapping("/cricketer/{id}")
fun updateCricketer(#PathVariable("id") id: Long, #RequestBody cricketer: Cricketer ):ResponseEntity<Cricketer> {
val cCricketer = Cricketer(name = cricketer.name
, country = cricketer.country
, highestScore = cricketer.highestScore)
cricketerRepository.save(cCricketer)
return ResponseEntity<Cricketer>(cricketer, HttpStatus.OK)
}
#DeleteMapping("/cricketer/{id}")
fun deleteCricketer(#PathVariable("id") id:Long ):ResponseEntity<String> {
val cCricketer :Cricketer = cricketerService.findById(id) as Cricketer
cricketerRepository.delete(cCricketer)
return ResponseEntity<String>("cricketer removed", HttpStatus.OK)
}
}
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>
<groupId>com.example</groupId>
<artifactId>Spring-Kotlin-Rest-API</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Spring-Kotlin-Rest-API</name>
<description>Spring Boot Kotlin REST API Example</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<kotlin.version>1.2.41</kotlin.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-kotlin</artifactId>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib-jdk8</artifactId>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-reflect</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory>
<testSourceDirectory>${project.basedir}/src/test/kotlin</testSourceDirectory>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<artifactId>kotlin-maven-plugin</artifactId>
<groupId>org.jetbrains.kotlin</groupId>
<configuration>
<args>
<arg>-Xjsr305=strict</arg>
</args>
<compilerPlugins>
<plugin>spring</plugin>
</compilerPlugins>
<jvmTarget>1.8</jvmTarget>
</configuration>
<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-allopen</artifactId>
<version>${kotlin.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>
Startup logs
2018-05-21 22:31:19.490 INFO 3061 --- [ main] c.e.d.SpringKotlinRestApiApplicationKt : Starting SpringKotlinRestApiApplicationKt on nirmal-desktop with PID 3061 (/home/nirmal/code/workspace-sts-3.9.1.RELEASE/Spring-Kotlin-Rest-API/target/classes started by root in /home/nirmal/code/workspace-sts-3.9.1.RELEASE/Spring-Kotlin-Rest-API)
2018-05-21 22:31:19.512 INFO 3061 --- [ main] c.e.d.SpringKotlinRestApiApplicationKt : No active profile set, falling back to default profiles: default
2018-05-21 22:31:19.592 INFO 3061 --- [ main] ConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext#5bab7c16: startup date [Mon May 21 22:31:19 IST 2018]; root of context hierarchy
2018-05-21 22:31:29.492 INFO 3061 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration' of type [org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration$$EnhancerBySpringCGLIB$$28d3b505] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2018-05-21 22:31:33.847 INFO 3061 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2018-05-21 22:31:34.312 INFO 3061 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2018-05-21 22:31:34.313 INFO 3061 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.31
2018-05-21 22:31:34.389 INFO 3061 --- [ost-startStop-1] 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: [/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib]
2018-05-21 22:31:36.190 INFO 3061 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2018-05-21 22:31:36.190 INFO 3061 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 16602 ms
2018-05-21 22:31:36.560 INFO 3061 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Servlet dispatcherServlet mapped to [/]
2018-05-21 22:31:36.564 INFO 3061 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]
2018-05-21 22:31:36.566 INFO 3061 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2018-05-21 22:31:36.566 INFO 3061 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2018-05-21 22:31:36.566 INFO 3061 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]
2018-05-21 22:31:37.743 INFO 3061 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2018-05-21 22:31:39.038 INFO 3061 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
2018-05-21 22:31:39.222 INFO 3061 --- [ main] j.LocalContainerEntityManagerFactoryBean : Building JPA container EntityManagerFactory for persistence unit 'default'
2018-05-21 22:31:39.480 INFO 3061 --- [ main] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [
name: default
...]
2018-05-21 22:31:40.064 INFO 3061 --- [ main] org.hibernate.Version : HHH000412: Hibernate Core {5.2.17.Final}
2018-05-21 22:31:40.066 INFO 3061 --- [ main] org.hibernate.cfg.Environment : HHH000206: hibernate.properties not found
2018-05-21 22:31:40.302 INFO 3061 --- [ main] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
2018-05-21 22:31:42.180 INFO 3061 --- [ main] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.MySQL5InnoDBDialect
2018-05-21 22:31:44.235 INFO 3061 --- [ main] o.h.t.schema.internal.SchemaCreatorImpl : HHH000476: Executing import script 'org.hibernate.tool.schema.internal.exec.ScriptSourceInputNonExistentImpl#7d95eb4a'
2018-05-21 22:31:44.237 INFO 3061 --- [ main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2018-05-21 22:31:44.688 INFO 3061 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-05-21 22:31:46.829 INFO 3061 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for #ControllerAdvice: org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext#5bab7c16: startup date [Mon May 21 22:31:19 IST 2018]; root of context hierarchy
2018-05-21 22:31:46.894 WARN 3061 --- [ main] aWebConfiguration$JpaWebMvcConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
2018-05-21 22:31:47.072 INFO 3061 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2018-05-21 22:31:47.073 INFO 3061 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2018-05-21 22:31:47.121 INFO 3061 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-05-21 22:31:47.122 INFO 3061 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-05-21 22:31:49.030 INFO 3061 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2018-05-21 22:31:49.033 INFO 3061 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Bean with name 'dataSource' has been autodetected for JMX exposure
2018-05-21 22:31:49.047 INFO 3061 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Located MBean 'dataSource': registering with JMX server as MBean [com.zaxxer.hikari:name=dataSource,type=HikariDataSource]
2018-05-21 22:31:50.682 INFO 3061 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2018-05-21 22:31:50.695 INFO 3061 --- [ main] c.e.d.SpringKotlinRestApiApplicationKt : Started SpringKotlinRestApiApplicationKt in 34.697 seconds (JVM running for 49.043)
I guess your CricketerController is not in a subpackage relative to your main class. So you basically have two options:
Place your main class directly under the package com.example
Add the annotation at the bottom of this answer to your main class.
Using any of these two methods makes the controller class visible for Spring at startup and therefore should create your mappings.
#ComponentScan(basePackages = { "com.example.controller"} )

Springboot executable jar can not access index page

I created an application with springboot, restapi and angularjs. I will turn this project as executable jar. but when i do java -jar patchinit.jar, it does not reload index page. it gives
Whitelabel Error Page (There was an unexpected error (type=Not Found,
status=404 No message available)
Trying to http://localhost:8080/
Am i missing something? I almost tried everything desperately..
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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.ama.ist</groupId>
<artifactId>patchinit</artifactId>
<packaging>jar</packaging>
<version>0.0.1</version>
<name>patchinit Maven Webapp</name>
<url>http://maven.apache.org</url>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.5.RELEASE</version>
</parent>
<properties>
<java.version>1.7</java.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<!-- <scope>provided</scope> -->
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<!-- <scope>provided</scope> -->
</dependency>
<dependency>
<groupId>org.tmatesoft.svnkit</groupId>
<artifactId>svnkit</artifactId>
<version>1.7.8</version>
</dependency>
</dependencies>
<build>
<finalName>patchinit</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Webapplication.java
package com.ama.ist;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.web.SpringBootServletInitializer;
import org.springframework.web.bind.annotation.RequestMapping;
#SpringBootApplication
public class WebApplication extends SpringBootServletInitializer {
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(WebApplication.class);
}
public static void main(String[] args) throws Exception {
SpringApplication.run(WebApplication.class, args);
}
}
HelloController.java
package com.ama.ist.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
#Controller
public class HelloController {
#RequestMapping("/")
String index() {
return "index";
}
}
index.html
<!DOCTYPE html>
<html ng-app="MyApp">
<head>
<link rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css">
<link rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/angular-material/1.1.5/angular-material.min.css">
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-4 jumbotron">
<div class="col-md-12 dialog-demo-content">
<div ui-view="newPatch"></div>
</div>
</div>
<div class="col-md-8 ">
<div ui-view="svntab"></div>
</div>
</div>
</div>
<!-- <div class="container" class="md-padding" ng-cloak> -->
<!-- <div class="row"> -->
<!-- <div class="col-md-3 dialog-demo-content"> -->
<!-- <div ui-view="newPatch"></div> -->
<!-- </div> -->
<!-- <div class="col-md-2"></div> -->
<!-- <div class="col-md-3"> -->
<!-- <div ui-view="svntab"></div> -->
<!-- </div> -->
<!-- </div> -->
<!-- </div> -->
<!-- ************************* SCRIPTS ********************************** -->
<script src="https://code.angularjs.org/1.5.8/angular.js"></script>
<script
src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-router/0.2.8/angular-ui-router.min.js"></script>
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script
src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js">
</script>
<script
src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular-animate.min.js"></script>
<script
src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular-aria.min.js"></script>
<script
src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular-messages.min.js"></script>
<script
src="https://ajax.googleapis.com/ajax/libs/angular_material/1.1.0/angular-material.min.js"></script>
<script src="js/app.js"></script>
<script src="js/patch.controller.js"></script>
<script src="js/svn.controller.js"></script>
</body>
</html>
This is the output of springboot
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1.3.5.RELEASE)
2017-12-05 19:54:09.019 INFO 480 --- [ main] com.ama.ist.WebApplication : Starting WebApplication on ISTL55310 with PID 480 (C:\Erkan\eclipse-workspace-new\patchinit\target\classes started by erkan.erkisi in C:\Erkan\eclipse-workspace-new\patchinit)
2017-12-05 19:54:09.022 INFO 480 --- [ main] com.ama.ist.WebApplication : No active profile set, falling back to default profiles: default
2017-12-05 19:54:09.063 INFO 480 --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext#1ce2a846: startup date [Tue Dec 05 19:54:09 EET 2017]; root of context hierarchy
2017-12-05 19:54:10.335 INFO 480 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2017-12-05 19:54:10.345 INFO 480 --- [ main] o.apache.catalina.core.StandardService : Starting service Tomcat
2017-12-05 19:54:10.347 INFO 480 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.0.33
2017-12-05 19:54:10.987 INFO 480 --- [ost-startStop-1] 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.
2017-12-05 19:54:10.994 INFO 480 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2017-12-05 19:54:10.994 INFO 480 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1933 ms
2017-12-05 19:54:11.468 INFO 480 --- [ost-startStop-1] o.s.b.c.e.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/]
2017-12-05 19:54:11.471 INFO 480 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]
2017-12-05 19:54:11.471 INFO 480 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2017-12-05 19:54:11.471 INFO 480 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2017-12-05 19:54:11.471 INFO 480 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]
2017-12-05 19:54:11.639 INFO 480 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for #ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext#1ce2a846: startup date [Tue Dec 05 19:54:09 EET 2017]; root of context hierarchy
2017-12-05 19:54:11.697 INFO 480 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/]}" onto java.lang.String com.ama.ist.controller.HelloController.index()
2017-12-05 19:54:11.698 INFO 480 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/resource]}" onto public java.util.Map<java.lang.String, java.lang.Object> com.ama.ist.controller.PatchController.home()
2017-12-05 19:54:11.698 INFO 480 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/mk],methods=[POST]}" onto public org.springframework.http.ResponseEntity<?> com.ama.ist.controller.PatchController.createFolder(com.ama.ist.model.Patch)
2017-12-05 19:54:11.701 INFO 480 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/localfolders],methods=[GET]}" onto public org.springframework.http.ResponseEntity<java.util.List<com.ama.ist.model.Folder>> com.ama.ist.controller.SvnController.getlocalFolders(com.ama.ist.model.User)
2017-12-05 19:54:11.702 INFO 480 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/svnfolders],methods=[GET]}" onto public org.springframework.http.ResponseEntity<java.util.List<com.ama.ist.model.Folder>> com.ama.ist.controller.SvnController.getFolders(com.ama.ist.model.User)
2017-12-05 19:54:11.703 INFO 480 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" 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)
2017-12-05 19:54:11.704 INFO 480 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2017-12-05 19:54:11.716 INFO 480 --- [ main] o.s.w.s.c.a.WebMvcConfigurerAdapter : Adding welcome page: ServletContext resource [/index.html]
2017-12-05 19:54:11.736 INFO 480 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Root mapping to handler of type [class org.springframework.web.servlet.mvc.ParameterizableViewController]
2017-12-05 19:54:11.745 INFO 480 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-12-05 19:54:11.745 INFO 480 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-12-05 19:54:11.778 INFO 480 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-12-05 19:54:11.878 INFO 480 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2017-12-05 19:54:11.945 INFO 480 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2017-12-05 19:54:11.949 INFO 480 --- [ main] com.ama.ist.WebApplication : Started WebApplication in 3.254 seconds (JVM running for 3.827)
Project Structure as an image
Finally I got a solution. I was able to resolve this issue by adding thymeleaf dependency in pom
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
What is Thymeleaf?
Thymeleaf is a Java library. It is an XML/XHTML/HTML5 template engine that is able to apply a set of transformations to template files in order to display data and/or text produced by your applications.
There is one correction required in your html file:
<link rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css">
<link rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/angular-material/1.1.5/angular-material.min.css">
make sure to close this tag <link> with end tag </link>. Otherwise you will get below error
There was an unexpected error (type=Internal Server Error,
status=500). Exception parsing document: template="index", line 12 -
column 3
Project structure

View not getting detected and throwing whitelabel error (Spring Boot Application)

I just started learning spring boot, and created a small spring mvc application. But I'm getting white label error when I try to access view, but application is working fine if i just return the string using responseBody annotation. I have also added thymeleaf to dependency.
Project Structure:
demo2
|-- src/main/java
| |-- example
| | `-- Demo2Application
| |-- controller
| | `-- Controller
| `-- model
| `-- Person
`-- src/main/resources
`-- templates
`-- personView.html
Here is my 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>
<groupId>org.demo</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>demo2</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Application Config Main method:
#Configuration
#EnableAutoConfiguration
#ComponentScan({"com", "example", "controller"})
public class Demo2Application {
public static void main(String[] args) {
SpringApplication.run(Demo2Application.class, args);
}
}
Controller class:
#Controller
public class PersonController {
#RequestMapping("/person")
public String getPerson(Model model) {
Person person = new Person();
person.setFirstname("John");
person.setLastName("Carter");
person.setAge(25);
model.addAttribute(person);
return "personView";
}
#RequestMapping("/test")
public #ResponseBody String test() {
return "hello world";
}
#ResponseBody
#RequestMapping("/")
String entry() {
return "Welcome to Spring Boot Application";
}
}
View Page:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8"></meta>
<title>Insert title here</title>
</head>
<body>
FirstName : <span th:text="${person.firstName}"></span>
LastName : <span th:text="${person.lastName}"></span>
Age : <span th:text="${person.age}"></span>
</body>
</html>
Error:
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Sun Sep 04 22:45:31 EDT 2016
There was an unexpected error (type=Not Found, status=404).
No message available
Log:
2016-09-04 22:43:10.330 INFO 11356 --- [ main] com.example.Demo2Application : Starting Demo2Application on Sais-MacBook-Pro.local with PID 11356 (/Users/sainishankbojja/Documents/workspace-sts-3.8.1.RELEASE/demo2/target/classes started by sainishankbojja in /Users/sainishankbojja/Documents/workspace-sts-3.8.1.RELEASE/demo2)
2016-09-04 22:43:10.334 INFO 11356 --- [ main] com.example.Demo2Application : No active profile set, falling back to default profiles: default
2016-09-04 22:43:10.370 INFO 11356 --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext#a9cd3b1: startup date [Sun Sep 04 22:43:10 EDT 2016]; root of context hierarchy
2016-09-04 22:43:11.353 INFO 11356 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2016-09-04 22:43:11.365 INFO 11356 --- [ main] o.apache.catalina.core.StandardService : Starting service Tomcat
2016-09-04 22:43:11.366 INFO 11356 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.4
2016-09-04 22:43:11.430 INFO 11356 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2016-09-04 22:43:11.430 INFO 11356 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1063 ms
2016-09-04 22:43:11.558 INFO 11356 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/]
2016-09-04 22:43:11.561 INFO 11356 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]
2016-09-04 22:43:11.562 INFO 11356 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2016-09-04 22:43:11.562 INFO 11356 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2016-09-04 22:43:11.562 INFO 11356 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]
2016-09-04 22:43:11.786 INFO 11356 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for #ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext#a9cd3b1: startup date [Sun Sep 04 22:43:10 EDT 2016]; root of context hierarchy
2016-09-04 22:43:11.834 INFO 11356 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/test]}" onto public java.lang.String controller.PersonController.test()
2016-09-04 22:43:11.835 INFO 11356 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/]}" onto java.lang.String controller.PersonController.entry()
2016-09-04 22:43:11.835 INFO 11356 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/person]}" onto public java.lang.String controller.PersonController.getPerson(org.springframework.ui.Model)
2016-09-04 22:43:11.837 INFO 11356 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" 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)
2016-09-04 22:43:11.837 INFO 11356 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2016-09-04 22:43:11.860 INFO 11356 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2016-09-04 22:43:11.860 INFO 11356 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2016-09-04 22:43:11.885 INFO 11356 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2016-09-04 22:43:11.990 INFO 11356 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2016-09-04 22:43:12.035 INFO 11356 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2016-09-04 22:43:12.038 INFO 11356 --- [ main] com.example.Demo2Application : Started Demo2Application in 12.204 seconds (JVM running for 12.492)
2016-09-04 22:43:15.931 INFO 11356 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring FrameworkServlet 'dispatcherServlet'
2016-09-04 22:43:15.931 INFO 11356 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization started
2016-09-04 22:43:15.941 INFO 11356 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization completed in 10 ms

Web App not serving static resources using Spring Boot and Tomcat

Hello I am having troubles 'serving' resources with my web server, while some of my other resources are 'serving.' (graphs.js is loading)
In my browser console:
GET http://localhost:8080/js/app.js [HTTP/1.1 400 Bad Request 2ms]
GET http://localhost:8080/includes/header.html [HTTP/1.1 400 Bad Request 4ms]
GET http://localhost:8080/cs/bootstrap.min.css [HTTP/1.1 400 Bad Request 3ms]
But these files are located in my directory as such:
css/
bootstrap.min.css
includes/
header.html
js/
app.js
controllers/
graphs.js
When my server starts:
2014-07-28 08:25:06.748 INFO 5260 --- [ main] .t.TomcatEmbeddedServletContainerFactory : Server initialized with port: 8080
2014-07-28 08:25:06.939 INFO 5260 --- [ main] o.apache.catalina.core.StandardService : Starting service Tomcat
2014-07-28 08:25:06.940 INFO 5260 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/7.0.52
2014-07-28 08:25:07.017 INFO 5260 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2014-07-28 08:25:07.017 INFO 5260 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1146 ms
2014-07-28 08:25:07.346 INFO 5260 --- [ost-startStop-1] o.s.b.c.e.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/]
**mapping**
2014-07-28 08:25:07.903 INFO 5260 --- [ main] o.s.w.s.c.a.WebMvcConfigurerAdapter : Adding welcome page: jndi:/localhost/index.html
2014-07-28 08:25:07.905 INFO 5260 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Root mapping to handler of type [class org.springframework.web.servlet.mvc.ParameterizableViewController]
2014-07-28 08:25:07.912 INFO 5260 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2014-07-28 08:25:07.912 INFO 5260 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2014-07-28 08:25:08.023 INFO 5260 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2014-07-28 08:25:08.147 INFO 5260 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080/http
2014-07-28 08:25:08.149 INFO 5260 --- [ main] c.c.i.qualifier.datacentral.Application : Started Application in 2.556 seconds (JVM running for 2.783)
and my HTML is like:
<script type="text/javascript" src="js/app.js"></script>
<script type="text/javascript" src="js/controllers/graphs.js"></script>
<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">
You have not stated if you have followed instructions for packaging as a war instead of a JAR and bundling Tomcat in. This does change things a little. Either way, take a look at the documentation here: http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#boot-features-spring-mvc-static-content
Bascally, out of the box, you can place static content in one of the locations mentioned: /static, /public, /resources or /META-INF/resorces. As for the location of these folders, well either at the root of the servlet context or on the classpath.
You can find a very basic Tomcat application here: https://github.com/mmeany/spring-boot-web-mvc that does the job.

Categories