2017-05-07 168 views
0

工作休息教程扔了春季啓動網站,並收到404錯誤。該網站說,如果我使用RestController註釋,一個視圖(百里香葉)是不需要的,似乎我有這個404錯誤。讓我知道我做錯了什麼。春天和休息服務

主營:

@SpringBootApplication 
@ComponentScan("com.ronone.entity") 
public class App { 
    public static void main(String[] args) { 
     SpringApplication.run(App.class, args); 
    } 
} 

控制器:

@RestController 
public class Controller { 

    private static final String template = "Hello %s!"; 
    private final AtomicLong counter = new AtomicLong(); 

    @GetMapping("/greeting") 
    public Greeting greeting(@RequestParam(value = "name", defaultValue = "World")String name){ 
     return new Greeting(counter.incrementAndGet(), 
       String.format(template, name)); 
    } 
} 

平原OLE的java:

public class Greeting { 

    private final long id; 
    private final String content; 

    public Greeting(long id, String content) { 
     this.id = id; 
     this.content = content; 
    } 

    public long getId() { 
     return id; 
    } 

    public String getContent() { 
     return content; 
    } 
} 
+0

更多關於@ComponentScan什麼網址給你'404'? –

+0

http:// localhost:8080/greeting –

+0

啓動彈出式應用程序時,控制檯中是否有任何錯誤日誌?只是爲了確認應用程序已正確啓動。 –

回答

0

請注意@ComponentScan註解。

基本包以外的類將無法訪問。

看來com.ronone.entity不是頂級包,而com.ronoe是。

因此,你的控制器不加載和頁面提供了404

0

@ComponentScan如果使用gradle這個那麼整個項目名稱應該爲settings.gradle

否則你只是刪除搜索您的項目名稱,
@ComponentScan註釋和項目將正常工作,

或只是把項目的正確名稱。

閱讀在spring docs