简体   繁体   English

为什么 Spring 引导中的 REST 控制器返回 HTTP 状态 404 – 未找到

[英]Why REST Controlle in Spring Boot is returning HTTP Status 404 – Not Found

I have created simple spring Boot Application.I have tried so many times but each time it is throwing error:404 Error when I run this on Pivotal tc server.It would be really great if any one can help me this.I have created Controller class with Spring Boot Starter Class.Below the image for the Error I am getting.Pivotal Server is up and running at port 8082 but as soon as I enter /hello to localhost:8082,I am getting Error 404(I have shared image of Pivotal server too in the last).I have almost tried everything that is available on google.I am starter in Spring Boot and would really appreciate if any one can give suggestion to resolve this.I have also shared pom.xml.我已经创建了简单的 spring 引导应用程序。我已经尝试了很多次,但是每次在 Pivotal tc 服务器上运行它时都会抛出错误:404 错误。如果有人可以帮助我,那就太好了。我已经创建了 Controller class 和 Spring 启动启动器 Class。在我遇到的错误的图像下方。Pivotal 服务器已在端口 800842 上启动并运行,但我一输入错误图像,我就分享了 /hello 4最后也是关键服务器)。我几乎尝试了谷歌上可用的所有内容。我是 Spring 启动的入门者,如果有人能提出解决此问题的建议,我将不胜感激。我还分享了 pom.xml。

404 Error Screenshot--> Error Image . 404 错误屏幕截图-->错误图像

I am running this on pivotal tc server with url: http://localhost:8082/hello我正在使用 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.I have tried everything that I can search for. 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.我已经尝试了所有可以搜索的内容。

Link to Pivotal server up and Running Screenshot--> Pivotal server up and running image链接到 Pivotal 服务器启动和运行屏幕截图--> Pivotal 服务器启动和运行图像

Code for Spring Boot Starter Class: 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);

 }

}

Code For controller Class: 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: 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>

use this:用这个:

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

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

and then send a request to this API /test/hello然后向这个 API /test/hello 发送请求

Could you try wrapping the String inside Response Entity.您能否尝试将字符串包装在响应实体中。

@RestController
public class HelloController {

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM