2014-04-14 39 views
1

我在瀏覽器中加載爲 localhost:8080/picking/addPick獲取錯誤HTTP狀態405 - 不支持請求方法'GET'。 ?獲取錯誤HTTP狀態405用於其他Web服務POST方法

什麼不對的希望建議感謝

@Controller 
@RequestMapping("/picking") 

public class PickerController { 

@RequestMapping(method = RequestMethod.GET) 
public @ResponseBody ArrayList getAllPickingItems()throws SQLException,  ClassNotFoundException{ 
//.... 
} 

@RequestMapping(value="/addPick",method=RequestMethod.POST) 
@ResponseStatus(HttpStatus.CREATED) 
public Boolean add(@RequestBody PickingInfo pickingInfo,HttpServletResponse response){ 
    try{ 
     Boolean success = pickerMethod.addPickingInfo(pickingInfo); 
     response.setHeader("addPickingInfo", success+""); 
     return true; 
    }catch(Exception ex){ 
     System.out.println(ex.getMessage()); 
    } 

    return false; 
} 

} 

回答

2

您有限的URI /picking/addPick POST請求:當你試圖打開URI從你的瀏覽器你發送一個

@RequestMapping(value="/addPick",method=RequestMethod.POST) 

GET請求,而不是POST。如果你希望能夠訪問/採摘/ addPick從您的瀏覽器,你必須:

  • 解除限制method=RequestMethod.POST
  • 允許明確地GET請求:method = { RequestMethod.POST, RequestMethod.GET }

如果你只是想測試POST方法,使用SoapUI。只需創建一個「新REST項目」,粘貼您的服務URI,併發送任何類型的HTTP請求。

+0

我上面的代碼已經有 @RequestMapping(value =「/ addPick」,method = RequestMethod.POST) – user3354708

+0

是的,這就是問題:'RequestMethod.POST' ...當你嘗試從瀏覽器打開該URL時,你發送了一個GET請求,而不是'POST'。如果您希望能夠從瀏覽器訪問/ picking/addPick,您必須刪除限制'method = RequestMethod.POST'或者明確允許GET請求:'method = {RequestMethod.POST,RequestMethod.GET}' –

+0

哦,我懂了。其實我只是想測試後期方法的作品與否。 有什麼辦法對我的POST方法做一個簡單的測試嗎? – user3354708

0

您已將映射/ addPick添加到僅適用於POST請求的add方法。因此GET沒有映射到任何東西(在這種情況下,沒有點映射到該方法,因爲您也使用@RequestBody

相關問題