2011-04-02 33 views
5

我收到發送到服務器的PUT請求時遇到問題。Spring框架,啓用PUT方法

這是我的方法:

@RequestMapping(method= RequestMethod.GET) 
public String getCity(@PathVariable(value="cid") String cid, @RequestParam(value="State") Integer state, Model model) { 
    System.out.println("get request"); 
    return "index"; 
} 


@RequestMapping(method= RequestMethod.PUT) 
public String putCity(@PathVariable(value="cid") String cid, @RequestParam(value="State") Integer state, Model model) { 
    System.out.println("put request"); 
    return "index"; 
} 

當我追蹤的電話,我的PUT請求是由GET方法,而不是由PUT方法在我的課..處理了屏幕上,它總是讀作「得到請求」。我檢查了瀏覽器日誌並確認他們發送了正確的PUT請求,所以我認爲我在這裏錯過了一些Spring配置,但我不知道它是什麼..

有人可以幫忙嗎?

謝謝。

編輯:與其他類代碼:

@Controller 
@RequestMapping(value="/retail/{cid}/master/city") 
public class City { 

    @RequestMapping(value="/foo1", method= RequestMethod.GET) 
    public String getCity(@PathVariable(value="cid") String cid, @RequestParam(value="State") Integer state, Model model) { 
     System.out.println("get request"); 
     return "index"; 
    } 

    @RequestMapping(value="/foo2", method= RequestMethod.PUT) 
    public String putCity(@PathVariable(value="cid") String cid, @RequestParam(value="State") Integer state, Model model) { 
     System.out.println("put request"); 
     return "index"; 
    } 
} 

EDIT2: 對不起,好像檢查日誌的時候..我抓住了這個警告,兩次我都沒有很透徹。

WARNING: Error in annotation processing: java.lang.NoClassDefFoundError: org/aopalliance/intercept/MethodInterceptor

任何想法如何解決這個問題?

+0

如果調用getCity,則請求是GET方法。不需要額外的配置,您必須發送錯誤的請求。你確定你的客戶做得對嗎? – skaffman 2011-04-02 07:30:58

+0

是的..我確信客戶做的是正確的事情。它正在登錄到控制檯。試着用chrome和firefox,並且都發送了PUT請求。 – Magician 2011-04-02 07:38:43

+0

將'HttpServletRequest'參數添加到'getCity',然後記錄'request.getMethod()',看看有什麼說的。 – skaffman 2011-04-02 08:01:08

回答

3

也就迎刃而解了......這是修訂後的方法,工程

 
@Controller 
@RequestMapping(value="/retail/{cid}/master/city") 
public class City { 

    @RequestMapping(method= RequestMethod.GET) 
    public String getCity(@PathVariable(value="cid") String cid, @RequestParam(value="State") Integer state, Model model) { 
    System.out.println("get request"); 
    return "index"; 
    } 

    @RequestMapping(method= RequestMethod.PUT) 
    public String putCity(@PathVariable(value="cid") String cid, @RequestBody CityData state, Model model) { 
    System.out.println(state.getState()); 
    return "index"; 
    } 
} 
 
public class CityData { 
    private String state; 
    public String getState() { 
    return this.state; 
    } 
    public void setState(String state) { 
    this.state = state; 
    } 
} 

你可以使用@RequestBody String state,但我更喜歡創造CityData對象,因爲在上面的示例是我的代碼過於簡單,只檢查如何處理數據

1

這可能與您沒有指定映射值的事實有關。嘗試@RequestMapping(value="/foo", ...GET)@RequestMapping(value="/foo", ...PUT)分別

的文件寫道:

如果你有一個默認的方法(沒有明確的路徑映射),則沒有發現一個更具體的映射方法的所有請求都將被分派到它。如果你有多個這樣的默認方法,那麼方法名將被考慮在它們之間進行選擇。

關於錯誤 - 您需要將aopalliance jar添加到您的類路徑中。

+0

我只在類級別上定義了映射。該方法將處理請求。 – Magician 2011-04-03 04:40:07

+0

@Rudy Dajoh,試試看 - 這可能是選擇調用哪種方法的問題。 – Bozho 2011-04-03 07:01:18

+0

好吧,我用'@ RequestParam'測試了它,被調用的是getCity,而刪除所有參數將正確調用不帶參數的putCity。雖然我無法使用HttpServletRequest獲取參數。我剛剛看到'@ RequestParam'出現'invalid request'錯誤,這可能是putCity失敗並調用getCity的原因,而且這可能是主要問題,因爲Java無法從請求中檢索任何參數。我嘗試使用RoR來捕獲數據,並且RoR可以正確收到其所有表單數據。所以,我相信問題在於Java。 – Magician 2011-04-03 09:36:03