【问题标题】:I am getting a 404 Error on API Call in Spring Boot我在 Spring Boot 中的 API 调用上收到 404 错误
【发布时间】:2020-01-04 14:09:21
【问题描述】:

我正在开发一个 Spring Boot 应用程序。我在点击我配置的 URL 路径时收到 404 错误。我哪里错了?

HomeController.java

package com.example.homes;

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

@RestController("/add")
public class HomeController {

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

}

HomeApplication.java

package com.example.homes;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;

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

@SpringBootApplication
public class HomeApplication {

    public static void main(String[] args) {
        SpringApplication.run(HomeApplication.class, args);
        System.out.println("Inside main");
    }

}

【问题讨论】:

  • 在你得到 404 之前向我们解释你做了什么。
  • 展示您如何启动服务,以及如何调用它
  • 你不是缺少 servlet 上下文路径吗?
  • 您能在控制台日志中看到 Inside main 吗?

标签: java spring spring-boot http-status-code-404


【解决方案1】:

您缺少 /add 的 RequestMapping。您保留为@RestController 属性。应该是@RequestMapping("/add")。在您当前的代码中,hello 映射到根目录。

试试localhost:8080/hello,它会起作用的。

如果你愿意localhost:8080/add/hello

那么它应该像下面这样:


@RestController
@RequestMapping("/add")
public class HomeController {

    @GetMapping(value = "/hello") 
    public String add() {
        return "Hello";
    }
}

【讨论】:

    【解决方案2】:

    @RestController@Controller 的专用版本,它自动添加了@Controller@ResponseBody 注释。所以我们不必将@ResponseBody 添加到我们的映射方法中。

    @RequestMapping 将 HTTP 请求映射到 MVC 和 REST 控制器的处理程序方法。

    这里可能的嫌疑人是@RestController("/add"),应该是@RequestMapping("/add")

    类级别的@RequestMapping

    @RestController
    @RequestMapping("/add")
    public class HomeController {
    
        //localhost:8080/add/hello (No Context Path in application.properties)
        @GetMapping("/hello")
        public String add() {
            return "Hello";
        }
    
    }
    

    方法级别的@RequestMapping

    @RestController
    public class HomeController {
    
        //localhost:8080/hello (No Context Path in application.properties)
        @GetMapping("/hello")
        public String add() {
            return "Hello";
        }
    
    }
    

    如果您没有在 application.properties 中定义任何上下文路径,则调用
    localhost:8080/add/hello 将提供所需的输出

    如果你有一个上下文路径作为 app 然后调用localhost:8080/app/add/hello

    【讨论】:

      【解决方案3】:

      不要做@RestController("/add"),而是这样做,

      @RestController
      @RequestMapping("/add")
      

      那你应该可以拨打localhost:8080/<ApplicationContext>/add/hello

      【讨论】:

        猜你喜欢
        • 2018-08-31
        • 2021-05-04
        • 2020-08-04
        • 1970-01-01
        • 1970-01-01
        • 2019-07-18
        • 2020-04-12
        • 2023-03-17
        • 1970-01-01
        相关资源
        最近更新 更多