相关疑难解决方法(0)

使用Ajax将@RequestBody中的多个变量传递给Spring MVC控制器

是否有必要包裹背衬物体?我想做这个:

@RequestMapping(value = "/Test", method = RequestMethod.POST)
@ResponseBody
public boolean getTest(@RequestBody String str1, @RequestBody String str2) {}
Run Code Online (Sandbox Code Playgroud)

并使用这样的JSON:

{
    "str1": "test one",
    "str2": "two test"
}
Run Code Online (Sandbox Code Playgroud)

但相反,我必须使用:

@RequestMapping(value = "/Test", method = RequestMethod.POST)
@ResponseBody
public boolean getTest(@RequestBody Holder holder) {}
Run Code Online (Sandbox Code Playgroud)

然后使用这个JSON:

{
    "holder": {
        "str1": "test one",
        "str2": "two test"
    }
}
Run Code Online (Sandbox Code Playgroud)

那是对的吗?我的另一个选项是改变RequestMethodGET和使用@RequestParam的查询字符串,或者使用@PathVariable与两种RequestMethod.

java spring http spring-mvc

91
推荐指数
5
解决办法
16万
查看次数

标签 统计

http ×1

java ×1

spring ×1

spring-mvc ×1