Whitelabel Error Page - application has no explicit mapping for /error

Shivam Aggarwal picture Shivam Aggarwal · Dec 24, 2018 · Viewed 15k times · Source

I have configured a spring boot application but on booting the application,I am getting mapping error as below

Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.

Mon Dec 24 12:46:27 IST 2018
There was an unexpected error (type=Not Found, status=404).
No message available

.I have gone through the below link and have updated my controller to be part of root package containing the main class. This application has no explicit mapping for /error

I have used below mentioned configuration/code for my application:

build.gradle

buildscript {
    ext {
        springBootVersion = '2.1.1.RELEASE'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

group = 'com.ticket'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

repositories {
    mavenCentral()
}


dependencies {
    implementation('org.springframework.boot:spring-boot-starter-data-jpa')
    implementation('javax.servlet:jstl')
    implementation('org.springframework.boot:spring-boot-starter-web')
    runtimeOnly('com.h2database:h2')
    testImplementation('org.springframework.boot:spring-boot-starter-test')
}

TicketController.java

package com.ticket.controller;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

@RestController
public class TicketController {

    static Logger log = LogManager.getLogger();

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public ModelAndView homeScreen(ModelMap model) {
        return new ModelAndView("view");
    }   
}

application.properties

spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp

Jsp path

src/main/webapp/WEB-INF/jsp/view.jsp

Answer

osiris a. suarez picture osiris a. suarez · Mar 18, 2020

The answer is very simple, if you are using thymeleaf, add the following lines to your pom.xml:

<dependency>
  <groupId> org.springframework.boot </groupId>
  <artifactId> spring-boot-starter-thymeleaf </artifactId>
</dependency>

with that is all, you must put your views in the templates package of src / main / resources/templates

If you are using jsp files for the views, the above described does not work, you must add the following lines to your application.properties file:

spring.mvc.view.prefix = / WEB-INF / views /
spring.mvc.view.suffix = .jsp

the previous lines indicate the folders where the drivers will go to look for the views and they must also exist with that folder structure.

and to your pom.xml these lines:

**

<dependency>
      <groupId> org.apache.tomcat.embed </groupId>
      <artifactId> tomcat-embed-jasper </artifactId>
      <scope> provided </scope>
    </dependency>

**

That is all, what spring boot indicates is that it cannot find the requested resource but with what it already describes above it must be solved.