当前位置: 首页 > 知识库问答 >
问题:

白标错误页面 404 Spring启动微服务 Rest 控制器

上官英哲
2023-03-14

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

以下是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靴主类

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

任何帮助都将不胜感激。提前还款

共有2个答案

仇炜
2023-03-14

如果您得到一个404错误页面,这意味着SpringBoot没有从这个URI路径到特定控制器方法的请求映射。

这可能有两个主要原因…

    < li >您使用了错误的请求URI < li >您没有创建映射到此URI的控制器bean

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

如果您未更改< code > application . properties 中的任何配置,则获取数据的路径应为< code > http://localhost:8080/citizen/test 。用这个试试吧...如果它不起作用...请将SpringBoot在启动应用程序时打印的输出添加到原问题中。也给这个回答添加一个评论,这样我就能得到通知并可以回复。

万俟招
2023-03-14

问题是没有扫描Controller,因此没有创建Spring管理的Bean。将主CitizenDetailServiceApplication类移到包com.example中。microservices,它应该是您创建的任何其他子包的父包。

java prettyprint-override">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);
    }    
}

< code > @ spring boot application 封装了< code>@Configuration 、< code > @ enable auto Configuration 和< code>@ComponentScan批注及其默认属性。< code>@ComponentScan的默认值表示扫描使用< code>@ComponentScan的包中的所有子包。这就是为什么在项目的基础包中包含主类通常是一个好的做法。

 类似资料:
  • 我正在构建一个spring boot应用程序,在从eclipse运行项目的同时,它可以完美地工作,页面加载正常。但当我构建maven并生成JAR文件并尝试执行时,JSP页面没有加载,它显示了白标签错误。 应用属性 文件夹结构 波姆。xml 这个项目pom xml与所有的depeden Spring启动类文件

  • 我正在尝试将我的spring mvc项目转换为spring boot。我根据spring boot转换了所有必要的文件。控制台上没有错误。但当我在浏览器中运行我的web应用程序时,我收到了这个错误。错误:“Whitelabel Error Page”没有可用的消息请告诉我为什么会出现此错误以及如何解决? PageController.java OnlineshoppingApplication。J

  • 我坚持使用这个简单的MVC示例。当我启动应用程序并转到localhost:8080时,我得到了“白标签错误页面”,甚至我在“src/main/资源/模板”中创建了“index.html”。我还在我的索引方法上添加了@Request estMap(“/”)。我找不到问题。 : : -在"src/main/资源/模板"下:

  • 我正在尝试测试以下控制器: 下面是测试类: 当我运行测试失败,并给我以下消息: 当我测试的url正常工作。

  • 大家好,我有一个spring boot rest api,每个键和值都有重复的结果,如下代码所示 正如您所看到的,每一列都是重复的,一列首字母大写,另一列全是小写 这是我的课: 我使用我的数据库中的postgres,这是我的代码 如有任何帮助或指导将不胜感激

  • 我不知道如何解决这个问题 spring端:role.java 因此,如果我现在尝试inline=“javascript”: 当我运行它时,我得到这样的响应: 白标签错误页