[英]Spring MVC - Why not able to use @RequestBody and @RequestParam together
[英]Using both @RequestBody and @RequestParam together in spring mvc3
我使用spring-mvc 3.1.0.RELEASE ,由於某種原因,使用查詢參數和請求體映射POST不起作用。
以下是我的控制器方法的外觀:
@RequestMapping(method = POST, value = "/post-to-me/")
public void handlePost(
@RequestBody Content content,
@RequestParam("param1") String param1,
@RequestParam("param2") String param2
){
//do stuff
}
但是,如果我將所有請求參數轉換為路徑參數,則映射有效。 有沒有人碰到類似的東西?
謝謝!
編輯:“不起作用”== 404當我嘗試做, POST /post-to-me?param1=x¶m2=y
首先,你的POST網址與控制器方法網址不匹配,你的網址必須是“/ post-to-me /?param1 = x&param2 = y”not“/ post-to-me?param1 = x&param2 = y”
第二,Content類來自哪里? 我使用了一個字符串,對我來說很好
@RequestMapping(method = RequestMethod.POST, value = "/post-to-me/")
public void handlePost(@RequestBody String content,
@RequestParam("param1") String param1,
@RequestParam("param2") String param2, HttpServletResponse response) {
System.out.println(content);
System.out.println(param1);
System.out.println(param2);
response.setStatus(HttpServletResponse.SC_OK);
}
請注意,我使用HttpServletResponse返回HTTP 200代碼,但我認為返回Http代碼有更好的解決方案,請檢查: Spring MVC中的多響應http狀態
請求映射值末尾的斜杠可能是問題所在。 嘗試:
@RequestMapping(method = RequestMethod.POST, value = "/post-to-me")
或發送你的POST請求POST /post-to-me/?param1=x¶m2=y
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.