whitelabel 错误页面 404 spring 引导微服务 Rest 控制器

whitelabel error page 404 spring boot microservices Rest Controller

我是 Spring 引导和微服务的新手。我已经创建了 RestController 并尝试使用 RequestMapping 测试进行检查,但它给我们的错误是 404,whitelabel 错误页面。

下面是pom.xml:

<modelVersion>4.0.0</modelVersion>
<parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>2.5.6</version>
  <relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.demoweb</groupId>
<artifactId>CitizenDetailService</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>CitizenDetailService</name>
<description>Microservices Project for CitizenServiceDetailService</description>
<properties>
  <java.version>1.8</java.version>
  <spring-cloud.version>2020.0.4</spring-cloud.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.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  </dependency>

  <dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <scope>runtime</scope>
  </dependency>
  <dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <optional>true</optional>
  </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>
      <configuration>
        <excludes>
          <exclude>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
          </exclude>
        </excludes>
      </configuration>
    </plugin>
  </plugins>
</build>

</project> 

下面是控制器的代码

package com.exmaple.microservices.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/citizen")
public class CitizenController {
    @RequestMapping()
public String test()
{
    return "hello";
}

下面是Spring boot main class

package com.example.microservices.CitizenDetailsService;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class CitizenDetailServiceApplication {

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

服务器启动日志如下:

  .   ____          _            __ _ _
 /\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
[32m :: Spring Boot :: [39m              [2m (v2.5.6)[0;39m

[2m2021-11-07 21:51:08.753[0;39m [32m INFO[0;39m [35m27720[0;39m [2m---[0;39m [2m[           main][0;39m [36mc.e.m.C.CitizenDetailServiceApplication [0;39m [2m:[0;39m Starting CitizenDetailServiceApplication using Java 16.0.1 on DESKTOP-9G5CUP8 with PID 27720 (D:\Spring\CitizenDetailService\target\classes started by 91909 in D:\Spring\CitizenDetailService)
[2m2021-11-07 21:51:08.755[0;39m [32m INFO[0;39m [35m27720[0;39m [2m---[0;39m [2m[           main][0;39m [36mc.e.m.C.CitizenDetailServiceApplication [0;39m [2m:[0;39m No active profile set, falling back to default profiles: default
[2m2021-11-07 21:51:09.257[0;39m [32m INFO[0;39m [35m27720[0;39m [2m---[0;39m [2m[           main][0;39m [36m.s.d.r.c.RepositoryConfigurationDelegate[0;39m [2m:[0;39m Bootstrapping Spring Data JPA repositories in DEFAULT mode.
[2m2021-11-07 21:51:09.266[0;39m [32m INFO[0;39m [35m27720[0;39m [2m---[0;39m [2m[           main][0;39m [36m.s.d.r.c.RepositoryConfigurationDelegate[0;39m [2m:[0;39m Finished Spring Data repository scanning in 3 ms. Found 0 JPA repository interfaces.
[2m2021-11-07 21:51:09.451[0;39m [32m INFO[0;39m [35m27720[0;39m [2m---[0;39m [2m[           main][0;39m [36mo.s.cloud.context.scope.GenericScope    [0;39m [2m:[0;39m BeanFactory id=c60b4e5b-8c67-3295-9bcc-48b6ccda1d6a
[2m2021-11-07 21:51:09.821[0;39m [32m INFO[0;39m [35m27720[0;39m [2m---[0;39m [2m[           main][0;39m [36mo.s.b.w.embedded.tomcat.TomcatWebServer [0;39m [2m:[0;39m Tomcat initialized with port(s): 8085 (http)
[2m2021-11-07 21:51:09.831[0;39m [32m INFO[0;39m [35m27720[0;39m [2m---[0;39m [2m[           main][0;39m [36mo.apache.catalina.core.StandardService  [0;39m [2m:[0;39m Starting service [Tomcat]
[2m2021-11-07 21:51:09.831[0;39m [32m INFO[0;39m [35m27720[0;39m [2m---[0;39m [2m[           main][0;39m [36morg.apache.catalina.core.StandardEngine [0;39m [2m:[0;39m Starting Servlet engine: [Apache Tomcat/9.0.54]
[2m2021-11-07 21:51:09.968[0;39m [32m INFO[0;39m [35m27720[0;39m [2m---[0;39m [2m[           main][0;39m [36mo.a.c.c.C.[Tomcat].[localhost].[/]      [0;39m [2m:[0;39m Initializing Spring embedded WebApplicationContext
[2m2021-11-07 21:51:09.968[0;39m [32m INFO[0;39m [35m27720[0;39m [2m---[0;39m [2m[           main][0;39m [36mw.s.c.ServletWebServerApplicationContext[0;39m [2m:[0;39m Root WebApplicationContext: initialization completed in 1184 ms
[2m2021-11-07 21:51:10.121[0;39m [32m INFO[0;39m [35m27720[0;39m [2m---[0;39m [2m[           main][0;39m [36mo.hibernate.jpa.internal.util.LogHelper [0;39m [2m:[0;39m HHH000204: Processing PersistenceUnitInfo [name: default]
[2m2021-11-07 21:51:10.170[0;39m [32m INFO[0;39m [35m27720[0;39m [2m---[0;39m [2m[           main][0;39m [36morg.hibernate.Version                   [0;39m [2m:[0;39m HHH000412: Hibernate ORM core version 5.4.32.Final
[2m2021-11-07 21:51:10.327[0;39m [32m INFO[0;39m [35m27720[0;39m [2m---[0;39m [2m[           main][0;39m [36mo.hibernate.annotations.common.Version  [0;39m [2m:[0;39m HCANN000001: Hibernate Commons Annotations {5.1.2.Final}
[2m2021-11-07 21:51:10.414[0;39m [32m INFO[0;39m [35m27720[0;39m [2m---[0;39m [2m[           main][0;39m [36mcom.zaxxer.hikari.HikariDataSource      [0;39m [2m:[0;39m HikariPool-1 - Starting...
[2m2021-11-07 21:51:10.753[0;39m [32m INFO[0;39m [35m27720[0;39m [2m---[0;39m [2m[           main][0;39m [36mcom.zaxxer.hikari.HikariDataSource      [0;39m [2m:[0;39m HikariPool-1 - Start completed.
[2m2021-11-07 21:51:10.775[0;39m [32m INFO[0;39m [35m27720[0;39m [2m---[0;39m [2m[           main][0;39m [36morg.hibernate.dialect.Dialect           [0;39m [2m:[0;39m HHH000400: Using dialect: org.hibernate.dialect.MySQL5InnoDBDialect
[2m2021-11-07 21:51:11.079[0;39m [32m INFO[0;39m [35m27720[0;39m [2m---[0;39m [2m[           main][0;39m [36mo.h.e.t.j.p.i.JtaPlatformInitiator      [0;39m [2m:[0;39m HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
[2m2021-11-07 21:51:11.090[0;39m [32m INFO[0;39m [35m27720[0;39m [2m---[0;39m [2m[           main][0;39m [36mj.LocalContainerEntityManagerFactoryBean[0;39m [2m:[0;39m Initialized JPA EntityManagerFactory for persistence unit 'default'
[2m2021-11-07 21:51:11.128[0;39m [33m WARN[0;39m [35m27720[0;39m [2m---[0;39m [2m[           main][0;39m [36mJpaBaseConfiguration$JpaWebConfiguration[0;39m [2m:[0;39m 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
[2m2021-11-07 21:51:11.619[0;39m [32m INFO[0;39m [35m27720[0;39m [2m---[0;39m [2m[           main][0;39m [36mDiscoveryClientOptionalArgsConfiguration[0;39m [2m:[0;39m Eureka HTTP Client uses RestTemplate.
[2m2021-11-07 21:51:11.802[0;39m [33m WARN[0;39m [35m27720[0;39m [2m---[0;39m [2m[           main][0;39m [36miguration$LoadBalancerCaffeineWarnLogger[0;39m [2m:[0;39m Spring Cloud LoadBalancer is currently working with the default cache. You can switch to using Caffeine cache, by adding it and org.springframework.cache.caffeine.CaffeineCacheManager to the classpath.
[2m2021-11-07 21:51:11.879[0;39m [32m INFO[0;39m [35m27720[0;39m [2m---[0;39m [2m[           main][0;39m [36mo.s.c.n.eureka.InstanceInfoFactory      [0;39m [2m:[0;39m Setting initial instance status as: STARTING
[2m2021-11-07 21:51:11.941[0;39m [32m INFO[0;39m [35m27720[0;39m [2m---[0;39m [2m[           main][0;39m [36mcom.netflix.discovery.DiscoveryClient   [0;39m [2m:[0;39m Initializing Eureka in region us-east-1
[2m2021-11-07 21:51:11.946[0;39m [32m INFO[0;39m [35m27720[0;39m [2m---[0;39m [2m[           main][0;39m [36mc.n.d.s.r.aws.ConfigClusterResolver     [0;39m [2m:[0;39m Resolving eureka endpoints via configuration
[2m2021-11-07 21:51:11.980[0;39m [32m INFO[0;39m [35m27720[0;39m [2m---[0;39m [2m[           main][0;39m [36mcom.netflix.discovery.DiscoveryClient   [0;39m [2m:[0;39m Disable delta property : false
[2m2021-11-07 21:51:11.980[0;39m [32m INFO[0;39m [35m27720[0;39m [2m---[0;39m [2m[           main][0;39m [36mcom.netflix.discovery.DiscoveryClient   [0;39m [2m:[0;39m Single vip registry refresh property : null
[2m2021-11-07 21:51:11.980[0;39m [32m INFO[0;39m [35m27720[0;39m [2m---[0;39m [2m[           main][0;39m [36mcom.netflix.discovery.DiscoveryClient   [0;39m [2m:[0;39m Force full registry fetch : false
[2m2021-11-07 21:51:11.980[0;39m [32m INFO[0;39m [35m27720[0;39m [2m---[0;39m [2m[           main][0;39m [36mcom.netflix.discovery.DiscoveryClient   [0;39m [2m:[0;39m Application is null : false
[2m2021-11-07 21:51:11.980[0;39m [32m INFO[0;39m [35m27720[0;39m [2m---[0;39m [2m[           main][0;39m [36mcom.netflix.discovery.DiscoveryClient   [0;39m [2m:[0;39m Registered Applications size is zero : true
[2m2021-11-07 21:51:11.980[0;39m [32m INFO[0;39m [35m27720[0;39m [2m---[0;39m [2m[           main][0;39m [36mcom.netflix.discovery.DiscoveryClient   [0;39m [2m:[0;39m Application version is -1: true
[2m2021-11-07 21:51:11.980[0;39m [32m INFO[0;39m [35m27720[0;39m [2m---[0;39m [2m[           main][0;39m [36mcom.netflix.discovery.DiscoveryClient   [0;39m [2m:[0;39m Getting all instance registry info from the eureka server
[2m2021-11-07 21:51:12.272[0;39m [32m INFO[0;39m [35m27720[0;39m [2m---[0;39m [2m[           main][0;39m [36mcom.netflix.discovery.DiscoveryClient   [0;39m [2m:[0;39m The response status is 200
[2m2021-11-07 21:51:12.279[0;39m [32m INFO[0;39m [35m27720[0;39m [2m---[0;39m [2m[           main][0;39m [36mcom.netflix.discovery.DiscoveryClient   [0;39m [2m:[0;39m Starting heartbeat executor: renew interval is: 30
[2m2021-11-07 21:51:12.287[0;39m [32m INFO[0;39m [35m27720[0;39m [2m---[0;39m [2m[           main][0;39m [36mc.n.discovery.InstanceInfoReplicator    [0;39m [2m:[0;39m InstanceInfoReplicator onDemand update allowed rate per min is 4
[2m2021-11-07 21:51:12.303[0;39m [32m INFO[0;39m [35m27720[0;39m [2m---[0;39m [2m[           main][0;39m [36mcom.netflix.discovery.DiscoveryClient   [0;39m [2m:[0;39m Discovery Client initialized at timestamp 1636302072301 with initial instances count: 0
[2m2021-11-07 21:51:12.305[0;39m [32m INFO[0;39m [35m27720[0;39m [2m---[0;39m [2m[           main][0;39m [36mo.s.c.n.e.s.EurekaServiceRegistry       [0;39m [2m:[0;39m Registering application UNKNOWN with eureka with status UP
[2m2021-11-07 21:51:12.306[0;39m [32m INFO[0;39m [35m27720[0;39m [2m---[0;39m [2m[           main][0;39m [36mcom.netflix.discovery.DiscoveryClient   [0;39m [2m:[0;39m Saw local status change event StatusChangeEvent [timestamp=1636302072306, current=UP, previous=STARTING]
[2m2021-11-07 21:51:12.308[0;39m [32m INFO[0;39m [35m27720[0;39m [2m---[0;39m [2m[nfoReplicator-0][0;39m [36mcom.netflix.discovery.DiscoveryClient   [0;39m [2m:[0;39m DiscoveryClient_UNKNOWN/DESKTOP-9G5CUP8:8085: registering service...
[2m2021-11-07 21:51:12.330[0;39m [32m INFO[0;39m [35m27720[0;39m [2m---[0;39m [2m[           main][0;39m [36mo.s.b.w.embedded.tomcat.TomcatWebServer [0;39m [2m:[0;39m Tomcat started on port(s): 8085 (http) with context path ''
[2m2021-11-07 21:51:12.330[0;39m [32m INFO[0;39m [35m27720[0;39m [2m---[0;39m [2m[           main][0;39m [36m.s.c.n.e.s.EurekaAutoServiceRegistration[0;39m [2m:[0;39m Updating port to 8085
[2m2021-11-07 21:51:12.346[0;39m [32m INFO[0;39m [35m27720[0;39m [2m---[0;39m [2m[nfoReplicator-0][0;39m [36mcom.netflix.discovery.DiscoveryClient   [0;39m [2m:[0;39m DiscoveryClient_UNKNOWN/DESKTOP-9G5CUP8:8085 - registration status: 204
[2m2021-11-07 21:51:12.388[0;39m [32m INFO[0;39m [35m27720[0;39m [2m---[0;39m [2m[           main][0;39m [36mc.e.m.C.CitizenDetailServiceApplication [0;39m [2m:[0;39m Started CitizenDetailServiceApplication in 4.053 seconds (JVM running for 4.652)
[2m2021-11-07 21:51:42.285[0;39m [32m INFO[0;39m [35m27720[0;39m [2m---[0;39m [2m[freshExecutor-0][0;39m [36mcom.netflix.discovery.DiscoveryClient   [0;39m [2m:[0;39m Disable delta property : false
[2m2021-11-07 21:51:42.286[0;39m [32m INFO[0;39m [35m27720[0;39m [2m---[0;39m [2m[freshExecutor-0][0;39m [36mcom.netflix.discovery.DiscoveryClient   [0;39m [2m:[0;39m Single vip registry refresh property : null
[2m2021-11-07 21:51:42.286[0;39m [32m INFO[0;39m [35m27720[0;39m [2m---[0;39m [2m[freshExecutor-0][0;39m [36mcom.netflix.discovery.DiscoveryClient   [0;39m [2m:[0;39m Force full registry fetch : false
[2m2021-11-07 21:51:42.286[0;39m [32m INFO[0;39m [35m27720[0;39m [2m---[0;39m [2m[freshExecutor-0][0;39m [36mcom.netflix.discovery.DiscoveryClient   [0;39m [2m:[0;39m Application is null : false
[2m2021-11-07 21:51:42.286[0;39m [32m INFO[0;39m [35m27720[0;39m [2m---[0;39m [2m[freshExecutor-0][0;39m [36mcom.netflix.discovery.DiscoveryClient   [0;39m [2m:[0;39m Registered Applications size is zero : true
[2m2021-11-07 21:51:42.286[0;39m [32m INFO[0;39m [35m27720[0;39m [2m---[0;39m [2m[freshExecutor-0][0;39m [36mcom.netflix.discovery.DiscoveryClient   [0;39m [2m:[0;39m Application version is -1: false
[2m2021-11-07 21:51:42.286[0;39m [32m INFO[0;39m [35m27720[0;39m [2m---[0;39m [2m[freshExecutor-0][0;39m [36mcom.netflix.discovery.DiscoveryClient   [0;39m [2m:[0;39m Getting all instance registry info from the eureka server
[2m2021-11-07 21:51:42.295[0;39m [32m INFO[0;39m [35m27720[0;39m [2m---[0;39m [2m[freshExecutor-0][0;39m [36mcom.netflix.discovery.DiscoveryClient   [0;39m [2m:[0;39m The response status is 200
[2m2021-11-07 21:51:57.793[0;39m [32m INFO[0;39m [35m27720[0;39m [2m---[0;39m [2m[nio-8085-exec-1][0;39m [36mo.a.c.c.C.[Tomcat].[localhost].[/]      [0;39m [2m:[0;39m Initializing Spring DispatcherServlet 'dispatcherServlet'
[2m2021-11-07 21:51:57.793[0;39m [32m INFO[0;39m [35m27720[0;39m [2m---[0;39m [2m[nio-8085-exec-1][0;39m [36mo.s.web.servlet.DispatcherServlet       [0;39m [2m:[0;39m Initializing Servlet 'dispatcherServlet'
[2m2021-11-07 21:51:57.796[0;39m [32m INFO[0;39m [35m27720[0;39m [2m---[0;39m [2m[nio-8085-exec-1][0;39m [36mo.s.web.servlet.DispatcherServlet       [0;39m [2m:[0;39m Completed initialization in 3 ms

如有任何帮助,我们将不胜感激。 提前致谢

如果您收到 404 错误页面,则表示 SpringBoot 没有从该 URI 路径到特定控制器方法的请求映射。

这可能有两个主要原因...

  1. 您使用了错误的请求 URI
  2. 您没有创建映射到此 URI 的控制器 bean

鉴于我看到了控制器 bean 的代码,这意味着它是一个错误的 URI。

如果您没有更改 application.properties 中的任何配置,则您用来获取数据的路径应该是 http://localhost:8080/citizen/test。用这个试试……如果它不起作用……请添加输出,SpringBoot 在启动应用程序时打印到原始问题。还要为此答案添加评论,以便我收到通知并可以回复。

问题是您的 Controller 没有被扫描,因此没有创建 Spring-managed Bean。将您的主 CitizenDetailServiceApplication class 移动到包 com.example.microservices,对于您创建的任何其他子包,它应该是您的父包。

package com.example.microservices;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class CitizenDetailServiceApplication {

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

@SpringBootApplication 封装了 @Configuration@EnableAutoConfiguration@ComponentScan 注释及其默认属性。 @ComponentScan 的默认值意味着扫描 @ComponentScan 使用的包上的所有子包。这就是为什么在项目的基础包中包含主要 class 通常是一个好习惯。