繁体   English   中英

如何解决此 Spring Boot Whitelabel 错误

[英]How to solve this Spring Boot Whitelabel error

我正在构建 CRUD 应用程序,但遇到了问题。 当我在 http://localhost:8080/api/v1/employees 启动应用程序时,我收到一个白标签错误 404,而不是 JSON 响应与 db 数据。

信息

此应用程序没有显式映射 /error,因此您将其视为后备。

Thu Sep 08 17:49:26 GST 2022 出现意外错误(类型=未找到,状态=404)。 没有可用的消息

Controller

    package com.crudpet.controller;
    import java.util.List;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    import com.crudpet.model.Employee;
    import com.crudpet.repository.EmployeeRepository;
    
    @RestController
    @RequestMapping("/api/v1/")
    public class EmployeeController {
        
        @Autowired
        private EmployeeRepository employeeRepository;
        
        @GetMapping("/employees")
        public List<Employee> getAllEmployees(){
            return employeeRepository.findAll();
        }
    }

主class

package com.crudpet.springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
public class SpringbootBackendCrudApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootBackendCrudApplication.class, args);
    }

}

安慰

2022-09-08 17:49:19.486  INFO 836 --- [  restartedMain] c.c.s.SpringbootBackendCrudApplication   : Starting SpringbootBackendCrudApplication using Java 17.0.1 on DESKTOP-08M3S29 with PID 836 (C:\Users\Bogich\Documents\workspace-spring-tool-suite-4-4.13.1.RELEASE\springboot-backend-crud\target\classes started by Bogich in C:\Users\Bogich\Documents\workspace-spring-tool-suite-4-4.13.1.RELEASE\springboot-backend-crud)
2022-09-08 17:49:19.487  INFO 836 --- [  restartedMain] c.c.s.SpringbootBackendCrudApplication   : No active profile set, falling back to 1 default profile: "default"
2022-09-08 17:49:19.556  INFO 836 --- [  restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable
2022-09-08 17:49:19.556  INFO 836 --- [  restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : For additional web related logging consider setting the 'logging.level.web' property to 'DEBUG'
2022-09-08 17:49:20.206  INFO 836 --- [  restartedMain] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2022-09-08 17:49:20.223  INFO 836 --- [  restartedMain] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 7 ms. Found 0 JPA repository interfaces.
2022-09-08 17:49:20.833  INFO 836 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2022-09-08 17:49:20.844  INFO 836 --- [  restartedMain] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2022-09-08 17:49:20.844  INFO 836 --- [  restartedMain] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.65]
2022-09-08 17:49:20.917  INFO 836 --- [  restartedMain] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2022-09-08 17:49:20.917  INFO 836 --- [  restartedMain] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1360 ms
2022-09-08 17:49:21.132  INFO 836 --- [  restartedMain] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [name: default]
2022-09-08 17:49:21.193  INFO 836 --- [  restartedMain] org.hibernate.Version                    : HHH000412: Hibernate ORM core version 5.6.10.Final
2022-09-08 17:49:21.388  INFO 836 --- [  restartedMain] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations {5.1.2.Final}
2022-09-08 17:49:21.507  INFO 836 --- [  restartedMain] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2022-09-08 17:49:21.684  INFO 836 --- [  restartedMain] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
2022-09-08 17:49:21.701  INFO 836 --- [  restartedMain] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.MySQL5InnoDBDialect
2022-09-08 17:49:21.988  INFO 836 --- [  restartedMain] o.h.e.t.j.p.i.JtaPlatformInitiator       : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
2022-09-08 17:49:21.999  INFO 836 --- [  restartedMain] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2022-09-08 17:49:22.063  WARN 836 --- [  restartedMain] 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
2022-09-08 17:49:22.450  INFO 836 --- [  restartedMain] o.s.b.d.a.OptionalLiveReloadServer       : LiveReload server is running on port 35729
2022-09-08 17:49:22.492  INFO 836 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2022-09-08 17:49:22.502  INFO 836 --- [  restartedMain] c.c.s.SpringbootBackendCrudApplication   : Started SpringbootBackendCrudApplication in 3.407 seconds (JVM running for 4.229)
2022-09-08 17:49:26.819  INFO 836 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
2022-09-08 17:49:26.819  INFO 836 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2022-09-08 17:49:26.820  INFO 836 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 1 ms

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.3</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.crudpet</groupId>
    <artifactId>springboot-backend-crud</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springboot-backend-crud</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-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </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>
    </dependencies>

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

</project>

问题是当我启动应用程序时,我必须收到 JSON 响应,但我看到 Whitelabel 错误。 我试过 ComponentScan,但它不适合我。 我应该做什么?

您尚未在 controller 和主类中指定 package 名称。

因此,默认的 Spring 自动发现将不起作用,并且您的 controller 将不会被实例化。 Spring 在包指定类下查找组件。 如果没有指定的 package,Spring 将无法“看到”您的 bean 并将它们添加到 ApplicationContext。

只需在 class 顶部添加 package 语句,一切都应该开始正常工作。

例子:

package com.example;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.crudpet.model.Employee;
import com.crudpet.repository.EmployeeRepository;

@RestController
@RequestMapping("/api/v1/")
public class EmployeeController {
    
    @Autowired
    private EmployeeRepository employeeRepository;
    
    @GetMapping("/employees")
    public List<Employee> getAllEmployees(){
        return employeeRepository.findAll();
    }
}

更新:

您的主要 class SpringbootBackendCrudApplication标记为@SpringBootApplication位于 package com.crudpet.springboot 而您的 controller 位于com.crudpet.controller ZEFE90A8E603A67C840D78F8D 中。

By default, Spring Boot will scan for beans in the base package of the class with main method ( SpringbootBackendCrudApplication in your case) and all nested packages (eg, com.crudpet.springboot.example ). The com.crudpet.controller package with your controller class is out of scope and simply wasn't initiated.

要解决此问题,您需要将SpringbootBackendCrudApplication class 放在com.crudpet package 中,这是最佳实践。 另一种可能的解决方案是在组件扫描中明确指定com.crudpet.controller package。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM