404调用SpringBoot中控制器中的Rest API时出错



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

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

最新更新