【问题标题】:Why REST Controlle in Spring Boot is returning HTTP Status 404 – Not Found为什么 Spring Boot 中的 REST 控制器返回 HTTP 状态 404 – 未找到
【发布时间】:2020-08-15 06:16:42
【问题描述】:

我创建了简单的 Spring Boot 应用程序。我尝试了很多次,但每次在 Pivotal tc 服务器上运行它时都会抛出错误:404 错误。如果有人能帮助我,那就太好了。我已经使用 Spring Boot Starter Class 创建了 Controller 类。在我收到的错误的图像下方。Pivotal Server 已启动并在端口 8082 上运行,但只要我输入 /hello 到 localhost:8082,我就会收到错误 404(我有最后也共享了 Pivotal 服务器的图像)。我几乎尝试了 google 上可用的所有内容。我是 Spring Boot 的初学者,如果有人能提出解决这个问题的建议,我将不胜感激。我还共享了 pom.xml。

404 错误截图-->Error Image.

我正在使用 url:http://localhost:8082/hello 的关键 tc 服务器上运行它

链接到 Pivotal 服务器 url 截图--->Pivotal server with controller return url Image Pivotal 服务器已启动并正在运行,但只要我输入 http://localhost:8082/hello, 我收到 404 错误。任何人都可以帮忙。我已经尝试了所有可以搜索的内容。

链接到 Pivotal 服务器启动并运行屏幕截图-->Pivotal server up and running image

Spring Boot Starter 类的代码:

package io.javabrains.springbootstarter;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication   

public class CourseApiApp {

public static void main(String[] args) {

    SpringApplication.run(CourseApiApp.class,args);

 }

}

控制器类代码:

package io.javabrains.springbootstarter.hello;

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

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

@RestController

public class HelloController {

@RequestMapping("/hello")

public String sayHi()
{
    return("Hello");
}
}

Pom.xml:

<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 http://maven.apache.org/xsd/maven- 
4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>io.javacheck.springbootquickstart</groupId>
<artifactId>course-api-test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>Java Brains Course Api</name>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.3.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<dependencies>

<!-- WEB -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    </dependencies>

      <properties>
    <java.version>1.8</java.version>
</properties>

</project>

【问题讨论】:

标签: java azure spring-boot azure-api-apps


【解决方案1】:

使用这个:

@RestController
@RequestMapping("/test")
public class HelloController {

@GetMapping("/hello")
public String sayHi()
{
    return("Hello");
}
}

然后向这个 API /test/hello 发送一个请求

【讨论】:

    【解决方案2】:

    您可以尝试将字符串包装在响应实体中吗?

    @RestController
    public class HelloController {
    
     @RequestMapping("/hello")
     public ResponseEntity<String> sayHi()
     {
       return ResponseEntity.ok("Hello");
     }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-06-04
      • 1970-01-01
      • 2021-03-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多