Everytime i am getting error- Whitelabel Error Page This application has no explicit mapping for /error - spring

I am new to spring Boot and followed some basic steps while creating a simple web application.
What i did is i created a folder like- src/main/webapp/WEB-INF/jsp and inside jsp i created home.jsp file
Then a controller class where i provided a path via #RequestMapping("/") function. But still i am not able to view my .jsp file. Please help
Structure
Controller class
Spring Boot Application
Application Properties
pom.xml
pom.xml-
http://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.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.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

Try to separate your main and your controller packages. I suggest that you add another package : com.example.demo.controller and change your controller class to that package
Also try to change the #Controller annotation to #RestController

Probably isn't relevant anymore but might help other people:
Try extending SpringBootServletInitializer.
public class demoApplication extends SpringBootServletInitializer {

Related

EnableFeignClients and FeignClient annotation is not recognized in application class file

I am using spring boot starter parent version 2.6.3(latest version). I am using spring cloud version 2021.0.0 . I am using openfeign dependency in maven pom file. But, my import statements in the class are not recognizable. Also, I am getting red tick in pom.xml. If I comment out openfeign dependency , red tick disappears from pom.xml . I tried to check openfeign jar files content, it does not have FeignClient and EnableFeignClients class files. So, it does make sense to get error while importing in my application class file.
What should I do to fix the import statements? I would like to have working version of dependency. I saw earlier versions tutorial but however I am looking forward to work with current version. I tried to put older version like spring boot starter parent 2.4.8 with spring cloud dependencies 2020.0.3 but I am getting same error for openfeign class files.
Here is my code.
My application class file.
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.cloud.openfeign.FeignClient;
#Configuration
#EnableFeignClients
#EnableDiscoveryClient
public class HelloWorldClientConfig {
#Autowired
private TheClient theClient;
#FeignClient(name = "HelloWorld")
interface TheClient {
#RequestMapping(path = "/helloworld", method = RequestMethod.GET)
#ResponseBody
String helloWorld();
}
public String HelloWorld() {
return theClient.helloWorld();
}
}
Here is my 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 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.6.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.travelport.ts</groupId>
<artifactId>HelloWorldConsumer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>HelloWorldConsumer</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<!-- spring-cloud.version>Hoxton.SR4</spring-cloud.version-->
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<!-- version>2.2.6.RELEASE</version-->
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>2021.0.0</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>
</project>
Try adding another dependency in your pom.
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-openfeign-core</artifactId>
</dependency>

JSP file is not rendering in Spring-Boot after included dependencies

I am creating simple demo Spring boot application. I have included all required dependencies in pom.xml file for jsp and set in application.properties as well but still not working.
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.4.1</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>11</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>
<!-- JSTL -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<!-- To compile JSP files -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project
controller.java
package com.example.demoController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
#RestController
public class controller {
#RequestMapping("/")
String first() {
return "index";
}
}
here is the project directory tree
please suggest something to resolve this problem.
Your JSP file should be in by default "src/main/webapp/WEB-INF/jsp" directory and right now it seems to be in wrong location.
UPDATE:- Looking at your shared code - the reason you are facing this problem is that you have annotated your Controller with #RestController instead of #Controller because of which the String you are returning from your controller is being written directly on the output stream instead of framework first checking the view name against a JSP template and then rendering it. If you change the annotation to #Controller it works fine

Springboot: show registered mappings

I have created simple spring boot application using spring initilzr. POM is:
<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>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>
then I add simple controller which returns string 'hello'. Endpoint works and I am happy but I would like to have all registered mappings in log during starting application. How to achieve that?
One way could be to use Spring Boot Actuator to monitor the application mappings.
You will then be able to monitor the data either with JMX or HTTP.
How to :
Add Spring Boot Actuator (see https://docs.spring.io/spring-boot/docs/current/reference/html/production-ready-features.html#production-ready-enabling)
Add this dependency :
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
Expose the mappings (see https://docs.spring.io/spring-boot/docs/current/reference/html/production-ready-features.html#production-ready-endpoints-exposing-endpoints)
2a. For HTTP :
Add this line to application.properties :
management.endpoints.web.exposure.include=info, health, mappings
Then check this endpoint : http://localhost:8080/actuator/mappings (JSON data)
2b. For JMX :
Add this line to application.properties :
spring.jmx.enabled=true
Then check the MBeans prefixed by org.springframework.boot, with VisualVM for example.

Turbine not working with spring boot 2.0.6 and spring cloud Finchley.SR1

I am trying to implement a simple service which will use turbine, but it doesn't work - turbine not getting any data from other Hystrix-used services.
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>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.6.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>
<spring-cloud.version>Finchley.SR1</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-turbine</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.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>
</project>
application.properties
server.port=5000
spring.application.name=turbine-aggregator
eureka.client.service-url.defaultZone=http://localhost:8761/eureka
turbine.app-config=weather-app
turbine.cluster-name-expression='default'
DemoApplication.java
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.turbine.EnableTurbine;
#SpringBootApplication
#EnableTurbine
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
I can see in the output that it recognize my Hystrix-used app:
click here for snapshot
but in turbine stream it looks empty:
click here for snapshot
If someone know what I did in the wrong way please let me know. Thanks.
At the end - it was my fault.
Since the Hystrix client had an older version of spring boot/cloud it "broadcast" the hystrix stream on ../hystrix.stream, but in the version which I used for turbine it aggregated streams from ../actuator/hystrix.stream.
So I just updated the versions in my Hystrix client.

Spring Boot - Run a WAR which is injected as a dependency as a standalone JAR

I am trying to move a Spring WebMVC project to containerless architecture. I thought to start by having a simple Spring Boot project which only needs to start the embedded WAR file, and inject all the beans as normal. In my Application.java class, I have the following:
package demo.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;
#SpringBootApplication
#ImportResource("classpath:beans/beans.xml")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
In my pom.xml, I have included the Spring WebMVC project as a dependency:
<dependency>
<groupId>com.other.app</groupId>
<artifactId>CoolWARApplication</artifactId>
<version>${app.version}</version>
</dependency>
My full pom:
<?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.springbootapp</groupId>
<artifactId>springbootapp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</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.9.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>
<app.version>3.0.27-SNAPSHOT</app.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</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>
<dependency>
<groupId>com.other.app</groupId>
<artifactId>CoolWARApplication</artifactId>
<version>${app.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
In my project structure in Eclipse, I can see this project and all of its dependencies being pulled in. However, when I start the Spring Boot app, I get the following error:
org.springframework.context.ApplicationContextException: Unable to start embedded container; nested exception is org.springframework.context.ApplicationContextException: Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean.
I'm not sure what I'm missing here. I thought that importing the WebMVC project as a dependency and injecting its beans into the Spring Boot application would run the project as normal, only inside a standalone JAR file. Is it advisable, or even possible, to do the above as described?
Thanks in advance.

Resources