我正在使用Spring Boot(最新版本1.3.6),我想创建一个接受一堆参数和JSON对象的REST端点。例如:
curl -X POST http://localhost:8080/endpoint \
-d arg1=hello \
-d arg2=world \
-d json='{"name":"john", "lastNane":"doe"}'
我目前在Spring控制器中执行以下操作:
public SomeResponseObject endpoint(
@RequestParam(value="arg1", required=true) String arg1,
@RequestParam(value="arg2", required=true) String arg2,
@RequestParam(value="json", required=true) Person person) {
...
}
json
参数不会被序列化成Person对象。我得到了一个。400 error: the parameter json is not present.
很显然,我可以将json
参数作为字符串传入并在控制器方法中解析有效负载,但这有点违背了使用Spring MVC的初衷。
如果我使用@RequestBody
,一切都可以正常工作,但我就失去了在JSON主体之外POST分离参数的可能性。
在Spring MVC中是否有一种方式可以“混合”正常的POST参数和JSON对象?