繁体   English   中英

在SpringBoot中的Controller中调用Rest API时出现404错误

[英]404 Error when invoke a Rest API in Controller in SpringBoot

我已经开发了一个简单的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);
    }
}

以上工作正常并在我以http:// localhost:8080 / api / home调用其余端点时返回200

但是现在我将其余的端点移到了一个不同的类,如下所示,

@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);
        }   
}

而且Example类看起来像

@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放在同一包中,然后重试

或者,您也可以将控制器的程序包放在主应用程序中,如下所示:

@SpringBootApplication
@EnableAutoConfiguration
@ComponentScan(basePackages={"com.controller"})
public class Application

暂无
暂无

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

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