I trying to build a simple welcome page (in jsp) using Spring Boot.
Below is the project structure
Application
#SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
public SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
}
APIController
#Controller
public class APIController {
#RequestMapping("/")
public String home() {
System.out.println("testing");
return "welcome";
}
}
When I access http://localhost:8080/, I get below error
This application has no explicit mapping for /error, so you are seeing
this as a fallback.
Wed May 23 15:31:51 MYT 2018 There was an unexpected error (type=Not
Found, status=404). No message available
By curl
{"timestamp":1527061233703,"status":404,"error":"Not Found","message":"No message available","path":"/"}
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>
<artifactId>spring-boot-web-jsp</artifactId>
<packaging>war</packaging>
<name>Spring Boot Web JSP Example</name>
<description>Spring Boot Web JSP Example</description>
<version>1.0</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.2.RELEASE</version>
</parent>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- This is a web application -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<!-- Optional, test for static content, bootstrap CSS-->
<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId>
<version>3.3.7</version>
</dependency>
</dependencies>
<build>
<plugins>
<!-- Package as an executable jar/war -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
You have to define the prefix and suffix for you jsp file in application.properties file like following:
spring.mvc.view.prefix: /WEB-INF/jsp/
spring.mvc.view.suffix: .jsp
In directory \src\main\resources\static, Create a simple index.html file
<html>
<head>Hello, Bich Van</head>
<body>
<h3>Today is a rainny day</h3>
</body>
</html>
Then try again at http://localhost:8080
If you don't like static HTML files, you can use JSP, but need a little complicate. You need declaring JSP View resolver in Spring MVC configuration.
Reference document: https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-developing-web-applications.html#boot-features-spring-mvc-welcome-page
Ensure that you have jasper and jstl in the list of dependencies:
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
Here is a working starter project - https://github.com/spring-projects/spring-boot/tree/master/spring-boot-samples/spring-boot-sample-web-jsp
It's better to use thymeleaf in your spring boot project and add the following dependency
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
In directory \src\main\resources\template, Create a simple index.html file
Related
I'm trying to run Vaadin app on azure and I constantly getting error 404. It's basic app with only 2 classes - from spring initializr and one my class.
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class WsbProjektZalApplication {
public static void main(String[] args) {
SpringApplication.run(WsbProjektZalApplication.class, args);
}
}
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
public class ServletInitializer extends SpringBootServletInitializer {
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(WsbProjektZalApplication.class);
}
}
And one my class
import com.vaadin.flow.component.html.H1;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.router.Route;
#Route("/co")
public class ListView extends VerticalLayout {
public ListView() {
add(new H1("dziaĆa chyba totto"));
}
}
My pom file is default and I wasn't editing it.
?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.7.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>zaliczenie</groupId>
<artifactId>wsb-projekt-zal</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>wsb-projekt-zal</name>
<description>wsb-projekt-zal</description>
<properties>
<java.version>17</java.version>
<vaadin.version>23.0.10</vaadin.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.vaadin</groupId>
<artifactId>vaadin-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</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>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.vaadin</groupId>
<artifactId>vaadin-bom</artifactId>
<version>${vaadin.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
I'm trying to run this app with war artifacts on tomcat 9. Problems occur only with Vaadin, I can run this app locally without any problems. Can somebody help me and tell me where I'm making mistake?
Screen of error below.
enter image description here
Try to resolve the error by adding the following in $TOMCAT_HOME/conf/server.xml
To avoid memory leaks, Tomcat disables the loading of Java driver files by default.
<Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" driverManagerProtection="false" />
Also, if you're creating a spring boot application, add "SpringBootServletInitializer" to your main file, as seen in the code below. As without SpringBootServletInitializer, Tomcat will see it as a regular application rather than a Spring boot application.
#SpringBootApplication
public class DemoApplication extends *SpringBootServletInitializer*{
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(DemoApplication .class);
}
public static void main(String[] args) {
SpringApplication.run(DemoApplication .class, args);
}
}
References:
The origin server did not find a current representation for the target resource or is not willing to disclose that one exists ,
Tomcat 404 error: The origin server did not find a current representation for the target resource or is not willing to disclose that one exists [duplicate]
I am trying to make a simple spring web application using Spring boot starter package provided. I am able to display 'Hello World' into console, but when I am trying to open localhost:(port), it is showing me . I am using JRE 8. Tried using JDK8 but also gives me same error. Do we have to use JDK or JRE ?
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Mon Jul 02 21:58:01 MDT 2018
There was an unexpected error (type=Not Found, status=404).
No message available
I am trying to resolve this issue from from last 2 days but I am stuck at this!.
Please find below images and code which I have in my system.
Project Structure
Spring Starter Project Application .java
package com.gami.mvc;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
#SpringBootApplication
public class SpringStarterProjectApplication extends SpringBootServletInitializer{
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(SpringStarterProjectApplication.class);
}
public static void main(String[] args) {
SpringApplication.run(SpringStarterProjectApplication.class, args);
System.out.println("Hello World");
}
}
Pom.Xml file
<?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.gami.mvc</groupId>
<artifactId>springStarterProject</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springStarterProject</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.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>
<!-- This is a web application -->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Tomcat embedded container -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- JSTL for JSP -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<!-- Need this to compile JSP -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<!-- Optional, test for static content, bootstrap CSS -->
<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId>
<version>3.3.7</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Contoller. java
package com.gami.mvc;
import java.util.Map;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
public class WelcomeController {
// inject via application.properties
#Value("${welcome.message:test}")
private String message = "Hello World";
#RequestMapping("/")
public String welcome(Map<String, Object> model) {
model.put("message", this.message);
return "welcome";
}
}
Application.properties
server.port=8456
spring.mvc.view.prefix: /WEB-INF/jsp/
spring.mvc.view.suffix: .jsp
welcome.message: Hello Gami
Error receiving when running the application
Error Message
Please any help is really appreciate!
Thank you
Hardik Gami
Console Image
Console msg after hitting to local host
Java Build Path Error
Java Build Path - Eclipse Java Build Path of Project
Spring will search for classes annotated with #Controller on the configured search path, without this annotation spring won't pick up your controller class.
#Controller
public class WelcomeController { ... }
Need to annotate your class with #RestController, or this will search for the welcome.jsp
JRE will not have the Compiler. Use JDK, which will have development tools like javac, javap etc.
when trying to run my first HelloWorld app with springBoot and Tomcat Embeded I get the following exception :
org.springframework.beans.factory.BeanDefinitionStoreException: Failed
to read candidate component class nested exception is
java.lang.IllegalStateException: Could not evaluate condition on org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration$EmbeddedDatabaseConfiguration
due to org/springframework/dao/DataAccessException not found. Make
sure your own configuration does not rely on that class. This can also
happen if you are #ComponentScanning a springframework package (e.g.
if you put a #ComponentScan in the default package by mistake)
Here is my entry point Example class config:
#SpringBootApplication
public class Example extends SpringBootServletInitializer
{
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application)
{
return application.sources(Example.class);
}
public static void main(String[] args) throws Exception
{
SpringApplication.run(Example.class, args);
}
}
Here is my WelcomeController class:
package controler;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.Map;
#Controller
public class WelcomeController
{
// inject via application.properties
#Value("${welcome.message:test}")
private String message = "Hello World";
#RequestMapping("/")
public String welcome(Map<String, Object> model)
{
model.put("message", this.message);
return "welcome";
}
}
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>com.springBoot</groupId>
<artifactId>SpringbootHelloword</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Tomcat embedded container-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<!-- JSTL for JSP -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<!-- Need this to compile JSP -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<!-- Need this to compile JSP,
tomcat-embed-jasper version is not working, no idea why -->
<dependency>
<groupId>org.eclipse.jdt.core.compiler</groupId>
<artifactId>ecj</artifactId>
<version>4.6.1</version>
<scope>provided</scope>
</dependency>
<!-- Optional, test for static content, bootstrap CSS-->
<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId>
<version>3.3.7</version>
</dependency>
</dependencies>
</project>
i changed the server port because it was used by another app and it works correctly.
I am trying to run a simple spring boot app in a stand-alone tomcat. I've tried following the reference docs and a few question/answers here but nothing seems to work. When tomcat starts up it deploys the app but completely ignores my Application class so no spring code gets loaded. I am sure it must be something simple I'm missing but...?
Here's the relevant output in the log file:
INFO: Deploying web application archive C:\dev\servers\apache-tomcat-7.0.75\webapps\bankweb-0.1.war
Feb 15, 2017 4:41:45 PM org.apache.catalina.startup.TldConfig execute
INFO: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list o
f JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and
JSP compilation time.
Feb 15, 2017 4:41:45 PM org.apache.catalina.startup.HostConfig deployWAR
INFO: Deployment of web application archive C:\dev\servers\apache-tomcat-7.0.75\webapps\bankweb-0.1.war has finished in 3,595
ms
I've updated the Application class to extend SpringBootServletInitializer as per the docs:
#Configuration
#EnableAutoConfiguration
#ComponentScan
public class Application extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(applicationClass, args);
}
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(applicationClass);
}
private static Class<Application> applicationClass = Application.class;
}
#RestController
class GreetingController {
#RequestMapping("/hello/{name}")
String hello(#PathVariable String name) {
return "Hello, " + name + "!";
}
}
And in the pom I've changed packaging to war and marked the embedded servlet container as provided:
<?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>it.ankle</groupId>
<artifactId>bankweb</artifactId>
<version>0.1</version>
<packaging>war</packaging>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.1.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.7</java.version>
<tomcat.version>7.0.69</tomcat.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-jersey</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</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-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</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>
This is resolved, user error pretty much sums it up. I stripped everything back to remove all the optional spring modules, got it working, then added everything in bit by bit and it worked. I am not really accustomed to this spring boot stuff yet where functionality gets included just because you've configured a library in the pom. Thanks to anyone who took the time to read!
I'm just started working on Spring Boot and having issues while running the spring boot application. SO scenario is - i have two project
One is called Parent Project SpringBoot and
Other project called web-app is maven module which added in Parent project as module.
In Both Projects(Parent and child) i have Application classes which load the context but somehow when i start the application it gives me some exceptions like on project SpringBoot: Unable to find a suitable main class, please add a mainClass property
On top of that when i run the web-app separately then it works but when i run it through Parent Project SpringBoot it throws me exceptions
Here is my code structure of Spring Boot Project (Parent Project)
Application.java
#Configuration
#ComponentScan
#EnableAutoConfiguration
#SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Example.java
#RequestMapping("/api/**")
#RestController
public class Example {#RequestMapping(method = RequestMethod.GET, produces = {MediaType.APPLICATION_JSON_VALUE})
public String index() {
return "Hello World";
}
}
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>SpringBoot</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.1.RELEASE</version>
</parent>
<!-- Additional lines to be added here... -->
<modules>
<module>web-app</module>
</modules
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
Second project which is web-app:
ApplicationWebApp.Java
#Configuration
#ComponentScan
#EnableAutoConfiguration
#SpringBootApplication
ApplicationWebApp
public class ApplicationWebApp {
public static void main(String[] args) {
SpringApplication.run(ApplicationWebApp.class, args);
}
}
Example.java
#RequestMapping("/apis/**")
#RestController
public class Example {#RequestMapping(method = RequestMethod.GET, produces = {MediaType.APPLICATION_JSON_VALUE})
public String index() {
return "Hello World";
}
}
Pom.xml
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.example</groupId>
<artifactId>SpringBoot</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>web-app</artifactId>
</project>
I found couple of solutions on the stackoverflow and different forums by adding
<properties>
The main class to start by executing java -jar
<startclass>com.dashboard.backend.controllers.Application</startclass>
</properties>
By this way, i need to add this properly file in each n every pom.xml to load it. Since this is a web based application so I'm not sure whether its a good solution or not..
If anyone have tried any solution for this problem please share you findings