2017-02-16 82 views
0

我正在嘗試學習使用SpringBoot部署簡單的REST服務。以下是我正在使用的類文件。使用Springboot部署REST服務:whitelabel error

@SpringBootApplication 
public class App { 

    public static void main(String[] args) { 

     SpringApplication.run(App.class, args); 
     int a= 5; 
     int b=10; 
     Addition.addR(a, b); 
     } 
} 

@RestController 
@RequestMapping(value="/spring/examples") 
public class Addition { 

    @RequestMapping(value="/", method= RequestMethod.GET) 
    public static String addR(int a, int b){ 
     String c ; 
     c= a + b + " = Addition of two Numbers"; 
     return c; 
     } 
} 

當我運行的主類,我得到一個錯誤:

Whitelabel Error Page 

This application has no explicit mapping for /error, so you are seeing this as a fallback. 

任何指針,以什麼造成的呢?

回答

1

我不認爲你只是通過啓動主類來獲得錯誤頁面。因爲這個Whitelabel Error Page是來自spring的錯誤頁面的默認響應。所以你會在請求後得到這個頁面。

更改addR這樣的代碼:

@RequestMapping(method = RequestMethod.GET) 
public static String addR(int a, int b) { 
    String c = a + b + " = Addition of two Numbers"; 
    return c; 
} 

和發送這樣的請求:http://localhost:8080/spring/examples?a=1&b=2。預期的響應應該如下所示:3 = Addition of two Numbers

如果你想檢查你的主類是否也可以在控制檯上打印它System.out.println(Addition.addR(1, 3));