获取 404 Not Found for basic spring rest api 教程 - 'hello world'

时间:2021-02-22 17:18:44

标签: spring spring-boot rest

我正在尝试创建一个可以最终部署到容器环境中的 rest api web 应用程序。我也从 spring.io 到其他网站下载了很多教程,但每次使用确切的存储库时,我都会收到 404 错误,用于简单请求。

为了进一步简化,我将其缩减为一个包中的 2 个类:

project hierarchy

主类:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;


 @SpringBootApplication
 public class Main {

 public static void main(String[] args) {
    SpringApplication.run(Main.class, args);
 }

 }

控制器类:

@RestController
@RequestMapping("/")
public class RESTController {

private final AtomicLong counter = new AtomicLong();
private static final String template = "Hello, %s!";
/*
 * REST API Test Methods
 */


@RequestMapping( "/greeting" )
   public String greeting() {
      return "It's working...!";
}

当然还有实际的请求:

http://localhost:8080/test_rest_api/greeting

HTTP Status 404 – Not Found


Type Status Report

Message /test_rest_api/greeting

Description The origin server did not find a current representation for the target resource or is not 
willing to disclose that one exists.


Apache Tomcat/8.5.43

通过 Run As - 在服务器上运行;如果我选择 Run as java application 并选择 springboot 会发生以下错误:

    java.lang.IllegalArgumentException: Sources must not be empty at 
    org.springframework.util.Assert.notEmpty(Assert.java:467)

1 个答案:

答案 0 :(得分:1)

我终于想通了,这是一个愚蠢的错误。我添加了项目名称作为请求 url 域的一部分。

http://localhost:8080/test_rest_api/greeting

对比

http://localhost:8080/greeting