如何解决:** Whitelabel 错误页面。** 我得到 **此应用程序没有明确的 /error 映射,因此您将此视为后备。**

Posted

技术标签:

【中文标题】如何解决:** Whitelabel 错误页面。** 我得到 **此应用程序没有明确的 /error 映射,因此您将此视为后备。**【英文标题】:How to solve: ** Whitelabel Error Page.** I am getting **This application has no explicit mapping for /error, so you are seeing this as a fallback.** 【发布时间】:2021-07-15 07:32:36 【问题描述】:

当我点击 URL:http://localhost:8080/login 时,我得到了 白标错误页面 此应用程序没有显式映射 /error,因此您将其视为后备。 2021 年 4 月 21 日星期三 11:24:00 IST 出现意外错误(类型=未找到,状态=404)。错误。我该如何解决这个问题?

主类

package com.springboot.controller;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;


@EntityScan(basePackages="com.springboot.controller.model")
@EnableJpaRepositories(basePackages="com.springboot.controller.Repository")
@SpringBootApplication(scanBasePackages="com.springboot.controller")
public class RestApi 
     
    public static void main(String[] args) 
        SpringApplication.run(RestApi.class, args);

    


登录控制器

package com.springboot.controller.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;



@RestController
public class LoginController 
    
    @RequestMapping("/login")
    @ResponseBody
    public ModelAndView checkMVC()
    
        ModelAndView mav= new ModelAndView("LoginForm");
                
        return mav;
    

UserRepo.java

package com.springboot.controller.Repository;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Service;

import com.springboot.controller.model.User;

@Service("UserRepo")
public interface UserRepo extends JpaRepository<User,Integer> 

    String findByEmail(String email);


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.3</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.springboot.controller</groupId>
    <artifactId>cunsultustoday-web-services</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>cunsultustoday-web-services</name>
    <description>Demo project for Spring Boot</description>
    <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-jpa</artifactId>
        </dependency>
        <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>commons-configuration</groupId>
            <artifactId>commons-configuration</artifactId>
            <version>1.9</version>
        </dependency>
        <dependency>
           <groupId>javax.servlet</groupId>
           <artifactId>jstl</artifactId>
           <scope>provided</scope>
        </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>
        <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-redis</artifactId>
                <exclusions>
                    <exclusion>
                        <groupId>io.lettuce</groupId>
                        <artifactId>lettuce-core</artifactId>
                    </exclusion>
                </exclusions>    
            </dependency>
            <dependency>
                <groupId>redis.clients</groupId>
                <artifactId>jedis</artifactId>
            </dependency>
            <dependency>
              <groupId>org.springframework</groupId>
              <artifactId>spring-webmvc</artifactId>
              </dependency>
             
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

日志

 :: Spring Boot ::                (v2.4.3)

2021-04-21 11:23:50.952  INFO 13080 --- [           main] com.springboot.controller.RestApi        : Starting RestApi using Java 15.0.2 on Dell with PID 13080 (C:\Users\Ins\Desktop\Project\CunsultUsToday\CunsultUsToday\target\classes started by Ins in C:\Users\Ins\Desktop\Project\CunsultUsToday\CunsultUsToday)
2021-04-21 11:23:50.955  INFO 13080 --- [           main] com.springboot.controller.RestApi        : No active profile set, falling back to default profiles: default
2021-04-21 11:23:51.546  INFO 13080 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Multiple Spring Data modules found, entering strict repository configuration mode!
2021-04-21 11:23:51.546  INFO 13080 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2021-04-21 11:23:51.604  INFO 13080 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 48 ms. Found 3 JPA repository interfaces.
2021-04-21 11:23:51.795  INFO 13080 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Multiple Spring Data modules found, entering strict repository configuration mode!
2021-04-21 11:23:51.796  INFO 13080 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data Redis repositories in DEFAULT mode.
2021-04-21 11:23:51.816  INFO 13080 --- [           main] .RepositoryConfigurationExtensionSupport : Spring Data Redis - Could not safely identify store assignment for repository candidate interface com.springboot.controller.Repository.ClgRegRepo. If you want this repository to be a Redis repository, consider annotating your entities with one of these annotations: org.springframework.data.redis.core.RedisHash (preferred), or consider extending one of the following types with your repository: org.springframework.data.keyvalue.repository.KeyValueRepository.
2021-04-21 11:23:51.817  INFO 13080 --- [           main] .RepositoryConfigurationExtensionSupport : Spring Data Redis - Could not safely identify store assignment for repository candidate interface com.springboot.controller.Repository.StudentRegRepo. If you want this repository to be a Redis repository, consider annotating your entities with one of these annotations: org.springframework.data.redis.core.RedisHash (preferred), or consider extending one of the following types with your repository: org.springframework.data.keyvalue.repository.KeyValueRepository.
2021-04-21 11:23:51.818  INFO 13080 --- [           main] .RepositoryConfigurationExtensionSupport : Spring Data Redis - Could not safely identify store assignment for repository candidate interface com.springboot.controller.Repository.UserRepo. If you want this repository to be a Redis repository, consider annotating your entities with one of these annotations: org.springframework.data.redis.core.RedisHash (preferred), or consider extending one of the following types with your repository: org.springframework.data.keyvalue.repository.KeyValueRepository.
2021-04-21 11:23:51.818  INFO 13080 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 11 ms. Found 0 Redis repository interfaces.
2021-04-21 11:23:52.325  INFO 13080 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2021-04-21 11:23:52.342  INFO 13080 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2021-04-21 11:23:52.343  INFO 13080 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.43]
2021-04-21 11:23:52.698  INFO 13080 --- [           main] 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.
2021-04-21 11:23:52.709  INFO 13080 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2021-04-21 11:23:52.709  INFO 13080 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1688 ms
2021-04-21 11:23:52.881  INFO 13080 --- [           main] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [name: default]
2021-04-21 11:23:52.940  INFO 13080 --- [           main] org.hibernate.Version                    : HHH000412: Hibernate ORM core version 5.4.28.Final
2021-04-21 11:23:53.087  INFO 13080 --- [           main] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations 5.1.2.Final
2021-04-21 11:23:53.206  INFO 13080 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2021-04-21 11:23:53.377  INFO 13080 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
2021-04-21 11:23:53.396  INFO 13080 --- [           main] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect
2021-04-21 11:23:54.035  INFO 13080 --- [           main] o.h.e.t.j.p.i.JtaPlatformInitiator       : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
2021-04-21 11:23:54.044  INFO 13080 --- [           main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2021-04-21 11:23:54.556  WARN 13080 --- [           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-04-21 11:23:54.651  INFO 13080 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2021-04-21 11:23:54.990  INFO 13080 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2021-04-21 11:23:54.998  INFO 13080 --- [           main] com.springboot.controller.RestApi        : Started RestApi in 4.441 seconds (JVM running for 5.327)
2021-04-21 11:24:00.446  INFO 13080 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
2021-04-21 11:24:00.446  INFO 13080 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2021-04-21 11:24:00.447  INFO 13080 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 0 ms

日志中没有显示错误,但我仍然收到此 whitelabel 错误页面

请帮忙。

【问题讨论】:

你为什么用@RestController而不是@Controller注释LoginController?错误页面告诉你/login没有找到。 好的,我会改的。 我将 @RestController 更改为 @Controller 现在日志显示 os.web.servlet.PageNotFound : No mapping for GET /login 问题仍未解决。 @dan1st 您是否在 application.properties 中定义了上下文路径? 没有。 #mvc view spring.mvc.view.prefix=/WEB-INF/views/ spring.mvc.view.suffix=.jsp spring.mvc.static-path-pattern=/resources/** server.port=8080这是我的application.properties @Alien 【参考方案1】:

您没有指定方法和值。请参阅下面的屏幕截图。 https://www.baeldung.com/spring-requestmapping 例如:

@RequestMapping(value = "/login", method = RequestMethod.POST)
@ResponseBody
    public ModelAndView checkMVC()
    
        ModelAndView mav= new ModelAndView("LoginForm");
                
        return mav;
    

【讨论】:

其实没关系...如果没有指定方法,它适用于 GET

以上是关于如何解决:** Whitelabel 错误页面。** 我得到 **此应用程序没有明确的 /error 映射,因此您将此视为后备。**的主要内容,如果未能解决你的问题,请参考以下文章

如何在 Spring 中编辑 Whitelabel 错误页面

whitelabel error page翻译

如何在Spring-boot中修复错误“Whitelabel错误页面”

Spring Boot自定义错误页面,Whitelabel Error Page处理方式

SpringBoot 应用程序在从 JAR 提供 JSP 时显示 Whitelabel 错误页面

带有 Angular + Springboot 的 Whitelabel 错误页面