gpt4 book ai didi

java - Spring Boot在Controller中调用Rest API时出现404错误

转载 作者:行者123 更新时间:2023-12-01 10:59:00 25 4
gpt4 key购买 nike

我开发了一个简单的 SpringBoot 应用程序,其休息端点如下,

@SpringBootApplication
@RestController
@RequestMapping(value = "/api")
public class Example {

@RequestMapping(value="/home", method = RequestMethod.GET)
HttpEntity<String> home() {
System.out.println("-----------myService invoke-----------");
return new ResponseEntity<String>(HttpStatus.OK);
}

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

上面工作正常,当我调用其余端点时返回 200, http://localhost:8080/api/home

但现在我已将其余端点移至不同的类别,如下所示,

@RestController
@RequestMapping(value = "/api")
public class MyController {

@RequestMapping(value="/home", method = RequestMethod.GET)
HttpEntity<String> home() {
System.out.println("-----------myService invoke-----------");
return new ResponseEntity<String>(HttpStatus.OK);
}
}

示例类看起来像,

@SpringBootApplication
public class Example {
public static void main(String[] args) throws Exception {
SpringApplication.run(Example.class, args);
}
}

现在我调用了低于错误的终点,

{
"timestamp": 1446375811463,
"status": 404,
"error": "Not Found",
"message": "No message available",
"path": "/api/home"
}

请问我在这里缺少什么?

最佳答案

请将类、MyController和Example放在同一个包中,然后重试

关于java - Spring Boot在Controller中调用Rest API时出现404错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33461509/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com