2015-11-01 65 views
0

我必须开发具有如下休息终点简单SpringBoot应用程序调用REST API,404错误当控制器在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); 
     } 
} 

和示例CL屁股的样子,

@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" 
} 

缺少什么我在这里吗?

+0

是' MyController'与'Example'在同一个包或子包中? –

回答

1

请放置于同一个包中的类,myController的和实例,然后再试一次

0

或者你也可以把你的控制器封装在主应用程序,这样的事情:

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