我在 API 调用 Spring 引导时收到 404 错误
I am getting a 404 Error on API Call in Spring Boot
我正在开发 Spring 启动应用程序。我在点击我配置的 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");
}
}
您缺少 /add
的 RequestMapping。您保留为 @RestController
属性。应该是@RequestMapping("/add")
。在您当前的代码中,hello
被映射到 root。
试试 localhost:8080/hello
就可以了。
如果你想要localhost:8080/add/hello
那么它应该像下面这样:
@RestController
@RequestMapping("/add")
public class HomeController {
@GetMapping(value = "/hello")
public String add() {
return "Hello";
}
}
@RestController 是 @Controller
的特殊版本,其中添加了 @Controller
和 @ResponseBody
自动注释。所以我们不必将 @ResponseBody
添加到我们的映射方法中。
@RequestMapping 将 HTTP 请求映射到 MVC 和 REST 控制器的处理程序方法。
这里可能的嫌疑人是@RestController("/add")
,应该是@RequestMapping("/add")
@RequestMapping 在 Class 级别
@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
而不是@RestController("/add")
,而是这样做,
@RestController
@RequestMapping("/add")
那么你应该可以调用 localhost:8080/<ApplicationContext>/add/hello
我正在开发 Spring 启动应用程序。我在点击我配置的 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");
}
}
您缺少 /add
的 RequestMapping。您保留为 @RestController
属性。应该是@RequestMapping("/add")
。在您当前的代码中,hello
被映射到 root。
试试 localhost:8080/hello
就可以了。
如果你想要localhost:8080/add/hello
那么它应该像下面这样:
@RestController
@RequestMapping("/add")
public class HomeController {
@GetMapping(value = "/hello")
public String add() {
return "Hello";
}
}
@RestController 是 @Controller
的特殊版本,其中添加了 @Controller
和 @ResponseBody
自动注释。所以我们不必将 @ResponseBody
添加到我们的映射方法中。
@RequestMapping 将 HTTP 请求映射到 MVC 和 REST 控制器的处理程序方法。
这里可能的嫌疑人是@RestController("/add")
,应该是@RequestMapping("/add")
@RequestMapping 在 Class 级别
@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
而不是@RestController("/add")
,而是这样做,
@RestController
@RequestMapping("/add")
那么你应该可以调用 localhost:8080/<ApplicationContext>/add/hello