[英]Why REST Controlle in Spring Boot is returning HTTP Status 404 – Not Found
我已經創建了簡單的 spring 引導應用程序。我已經嘗試了很多次,但是每次在 Pivotal tc 服務器上運行它時都會拋出錯誤:404 錯誤。如果有人可以幫助我,那就太好了。我已經創建了 Controller class 和 Spring 啟動啟動器 Class。在我遇到的錯誤的圖像下方。Pivotal 服務器已在端口 800842 上啟動並運行,但我一輸入錯誤圖像,我就分享了 /hello 4最后也是關鍵服務器)。我幾乎嘗試了谷歌上可用的所有內容。我是 Spring 啟動的入門者,如果有人能提出解決此問題的建議,我將不勝感激。我還分享了 pom.xml。
404 錯誤屏幕截圖-->錯誤圖像。
我正在使用 url: http://localhost:8082/hello在關鍵 tc 服務器上運行它
Link to Pivotal Server url Screenshot---> Pivotal server with controller return url Image Pivotal server is up and running but as soon as I enter http://localhost:8082/hello , I am getting 404 error.Can anyone please help.我已經嘗試了所有可以搜索的內容。
鏈接到 Pivotal 服務器啟動和運行屏幕截圖--> Pivotal 服務器啟動和運行圖像
Spring 引導啟動器 Class 的代碼:
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);
}
}
controller Class 的代碼:
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>
用這個:
@RestController
@RequestMapping("/test")
public class HelloController {
@GetMapping("/hello")
public String sayHi()
{
return("Hello");
}
}
然后向這個 API /test/hello 發送請求
您能否嘗試將字符串包裝在響應實體中。
@RestController
public class HelloController {
@RequestMapping("/hello")
public ResponseEntity<String> sayHi()
{
return ResponseEntity.ok("Hello");
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.