whitelabel 错误页面 404 spring boot 微服务 Rest Controller
Posted
技术标签:
【中文标题】whitelabel 错误页面 404 spring boot 微服务 Rest Controller【英文标题】:whitelabel error page 404 spring boot microservices Rest Controller 【发布时间】:2021-12-20 17:53:38 【问题描述】:我是 Spring Boot 和微服务的新手。我创建了 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主类
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
任何帮助将不胜感激。 提前感谢
【问题讨论】:
您使用哪个链接来 ping 端点? 使用的链接是localhost:8085/citizen/test 【参考方案1】:如果你得到一个 404 错误页面,这意味着 SpringBoot 没有从这个 URI 路径到特定控制器方法的请求映射。
这可能有两个主要原因 ...
-
您使用了错误的请求 URI
您没有创建映射到此 URI 的控制器 bean
鉴于我看到了控制器 bean 的代码,这意味着它的 URI 错误。
如果您没有更改application.properties
中的任何配置,则用于获取数据的路径应为http://localhost:8080/citizen/test
。试试这个......如果它不起作用......请添加输出,SpringBoot在启动应用程序时打印到原始问题。还要对此答案添加评论,以便我收到通知并回复。
【讨论】:
我正在使用 application.yml,其中提到的服务器端口是 8085 然后把链接改成localhost:8085/citizen/test 告诉我是否有效 我尝试使用上面的链接,但它给出的错误为 404 @shaz 将您的代码推送到 github 并在此处留下链接。我将下载到我的机器并在那里调试。【参考方案2】:问题是您的控制器没有被扫描,因此没有创建 Spring 管理的 Bean。将您的主 CitizenDetailServiceApplication
类移动到包 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
的包上的所有子包都被扫描。这就是为什么在项目的基础包中包含主类通常是一个好习惯的原因。
【讨论】:
不客气!始终保持在项目的基础包中包含主类的“规则”,这样您就可以避免这些问题。 ;) 现在当我尝试 RequestMapping 时,它给我的错误是 404,而 PostMapping 在上面的代码中绝对没问题。@RequestMapping
非常通用,默认情况下,如果我们不指定值,它将映射到任何 HTTP 请求。建议使用您想要的 HTTP 方法的特定注释。
尝试使用 @GetMapping 但没有成功,并且 PostMapping 正在为控制器的其他方法工作,该方法在后端保存数据。
好吧,现在我很困惑。您介意用您当前的所有设置创建一个新问题吗?您面临的问题到底是什么?然后在这里发布链接。谢谢!以上是关于whitelabel 错误页面 404 spring boot 微服务 Rest Controller的主要内容,如果未能解决你的问题,请参考以下文章
为啥我在运行简单的 Spring Boot 应用程序时总是得到状态为“404”的 Whitelabel 错误页面
状态为404的Springboot Whitelabel错误页面:无法解析JSP的路径
Spring Boot访问HTML页面报404错误Whitelabel Error Page
如何在 Spring 中编辑 Whitelabel 错误页面