Getting error "Whitelabel Error Page" while running localhost:8080 - java

I started learning Spring Framework and get this kind of error while running localhost:8080
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Controller.java
package com.springFramework.helloGame.controller;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
#RestController
public class Controller {
#GetMapping("/sum")
public long displaySum(){
return 100;
}
}
HelloGameApplication.java
package com.springFramework.helloGame;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;
#SpringBootApplication
public class HelloGameApplication {
public static void main(String[] args) {
ConfigurableApplicationContext context =
SpringApplication.run(HelloGameApplication.class, args);
}
}
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.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.springFramework</groupId>
<artifactId>helloGame</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>helloGame</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</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-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
my project structure looks fine, but don't know why I got error!
Please help me!

The Whitelabel page is a spring error handling method and it tries to hint to you that you do have a mapping for /error.
Now coming to the issue you need to write #RequestMapping("/") above your get mapping in the controller so that the spring knows the base URI.
It's an entry point for the spring application for the browser.
Now you have written /sum get URI and in the browser, you are trying for http://localhost:8080
So for the above Uri to work you need to provide #RequestMapping("/")
or you can use http://localhost:8080/sum with you current code.

It seems your controller only have a mapping for "/sum" path. Try querying localhost:8080/sum.

Related

Whitelabel page error for graphql mapping

I'm new to spring boot and trying to learn how to implement graphql. #GetMapping and #PostMapping work as normal but when I try to use #QueryMapping or #SchemaMapping, following the Spring documentation I get the whitelabel error page. Playing around I'm able to guess that it's not recognizing these two as mapping. I've checked my dependencies but I'll include them below in case, I've verified the project structure, and I've also tried changing from #Controller to #RestController to no avail.
Controller file
package com.example.GraphQLBooks.controller;
import com.example.GraphQLBooks.model.Book;
import com.example.GraphQLBooks.repository.BookRepository;
import org.springframework.graphql.data.method.annotation.QueryMapping;
import org.springframework.graphql.data.method.annotation.SchemaMapping;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
#RestController
public class BookController {
private final BookRepository bookRepository;
public BookController(BookRepository bookRepository) {
this.bookRepository = bookRepository;
}
//This works with #RestController but breaks with #Controller
#GetMapping("/test")
public String controllerTest(){
return "Hello";
}
//#SchemaMapping(typeName = "Query",value = "allBooks")
#QueryMapping
public List<Book> findAll() {
return bookRepository.findAll();
}
}
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.7.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>GraphQL-Books</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>GraphQL-Books</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-graphql</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webflux</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.graphql</groupId>
<artifactId>spring-graphql-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.graphql-java</groupId>
<artifactId>graphql-java</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.properties
spring.graphql.graphiql.enabled=true
I tested the repositories using #GetMapping and they output as expected and when I got to my "/test" mapping it outputs Hello as expected. Any insight and tips are appreciated, and let me know if more info is needed.
Figured it out. My schema had a typo had schema.graphql instead of schema.graphqls and it should be localhost:8080/graphiql not localhost:8080/graphql dumb mistakes I know. And you can't use #RestController with graphql has to be #Controller which makes sense because we're not using a Rest Api.
Had similar issue. The problem was graphqls file wasn't placed in the right folder.
Spring boot graphQL looks for the graphqls file in the following directory by default: ~/src/main/resources/graphql/schema.graphqls

SpringBootServletInitializer causes whitelabel error

I'm going through this online course about Spring MVC.
During the course the instructor is adding a simple controller, and a simple jsp page.
In addition the application class is extending SpringBootServletInitializer.
my index.html file is found in src/main/webapp and my jsp file is found in src/main/webapp/WEB-INF/jsp
I've also added spring.mvc.view.prefix/suffix accordingly.
for some reason when i get to this point at the course, extending the SpringBootServletInitializer is causing whitelabel error to my main view (index.html) but the path to jsp file is working just fine.
I've tried some suggestions as changing #Controller to #RestController or removing the SpringBootServletInitializer but it didn't work (removing the SpringBootServletInitializer caused 404 when looking for the jsp page).
here is my code:
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.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.pluralSight</groupId>
<artifactId>conference</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>conference</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-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>
controller:
package com.pluralSight.conference.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import java.util.Map;
#Controller
public class GreetingController {
#GetMapping("greeting")
public String greeting(Map<String, Object> model){
model.put("message","Hello Daniel");
return "greeting";
}
}
application class:
package com.pluralSight.conference;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
#SpringBootApplication
public class ConferenceApplication extends SpringBootServletInitializer{
public static void main(String[] args) {
SpringApplication.run(ConferenceApplication.class, args);
}
}
application.properties:
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp

Whitelabel Error Page, rest urls not executed in springboot

new to spring boot application development. I ran the application as java application and got the below error. THe application is deployed successfully , I don't know why this error is rendered ... beating my head.. thank you for your help in advance.
Whitelabel Error Page This application has no explicit mapping for
/error, so you are seeing this as a fallback. Sat Jul 13 21:37:31 IST
2019 There was an unexpected error (type=Not Found, status=404). No
message available
tried solutions given on stack overflow but no help. could you please help me if anywhere I do wrong here.
package com.cpi.poc.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.cpi.poc.service.IService;
#RestController
#RequestMapping(value ="/test")
#ComponentScan(basePackages = "com.cgi.poc")
public class TestController {
#Autowired
IService service;
#RequestMapping(value = "/greet" , method = RequestMethod.GET)
public String testService(){
String result = service.greet();
return result;
}
}
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>1.5.21.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.cgi.poc</groupId>
<artifactId>test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>test</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</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>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
Check whether you have class :
#SpringBootApplication
#ComponentScan("com.cgi.poc")
#ComponentScan
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
No need to put component scan on controller if you have the above class.
Verify by using lsof command that your java service is running on the port on not.

Spring Boot Primitive Rest Controller Returns Whitelabel Error Page [duplicate]

This question already has answers here:
Intellij Springboot problems on startup
(3 answers)
Closed 4 years ago.
I am trying to build & run my Spring boot project which returns "I am alive" on the page. I have basic main class and one rest controller class. I guess solution is simple but i couldnt find any question similar with me.
Here is the thing.
Main Class
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class Application{
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Rest Controller
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
#RestController
public class HealthCheck {
#GetMapping("/up")
public String checkHealth(){
return "I am alive";
}
}
When i go to localhost:8080/up, it returns as
Whitelabel Error Page This application has no explicit mapping for
/error, so you are seeing this as a fallback.
Tue Sep 25 17:47:53 EEST 2018 There was an unexpected error (type=Not
Found, status=404). No message available
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.demo</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.5.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-data-mongodb</artifactId>-->
<!--</dependency>-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
By default, Spring Boot will scan for classes annotated with #RestController starting from the package that the main class (the one with #SpringBootApplication) and below. The easiest fix is to move your controller to the same package or a sub package of where your main class lives.
Instead of using #GetMapping("/up") try using #RequestMapping("/up"). As you want to see the message on the page, RequestMapping returns plain text when requested using either a browser or curl on the command line.
It looks like Spring doesn't see your controller. You have to use #ComponentScan
#ComponentScan
#SpringBootApplication
public class FraudWsApplication {
public static void main(String[] args) {
SpringApplication.run(FraudWsApplication.class, args);
}
}
You can also find an example in my repo
In your main Application change annotation to #SpringBootApplication(scanBasePackages = {"com.ykb.frd"})

Initial springboot starter project setup - facing issue

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.

Categories