2015-11-01 41 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