使用Ajax将@RequestBody中的多个变量传递给Spring MVC控制器 [英] Passing multiple variables in @RequestBody to a Spring MVC controller using Ajax
问题描述
是否需要包裹背衬物体?我想这样做:
Is it necessary to wrap in a backing object? I want to do this:
@RequestMapping(value = "/Test", method = RequestMethod.POST)
@ResponseBody
public boolean getTest(@RequestBody String str1, @RequestBody String str2) {}
并使用这样的JSON:
And use a JSON like this:
{
"str1": "test one",
"str2": "two test"
}
但我必须使用:
@RequestMapping(value = "/Test", method = RequestMethod.POST)
@ResponseBody
public boolean getTest(@RequestBody Holder holder) {}
然后使用这个JSON:
And then use this JSON:
{
"holder": {
"str1": "test one",
"str2": "two test"
}
}
这是正确的吗?我的另一个选择是将 RequestMethod
更改为 GET
并使用 @RequestParam
在查询字符串中或使用 @PathVariable
与 RequestMethod
。
Is that correct? My other option would be to change the RequestMethod
to GET
and use @RequestParam
in query string or use @PathVariable
with either RequestMethod
.
推荐答案
你是对的,@ RequestBody带注释的参数应该保存整个请求并绑定到一个对象,所以你基本上必须选择。
You are correct, @RequestBody annotated parameter is expected to hold the entire body of the request and bind to one object, so you essentially will have to go with your options.
如果你绝对想要你的方法,你可以做一个自定义的实现:
If you absolutely want your approach, there is a custom implementation that you can do though:
说这是你的json:
{
"str1": "test one",
"str2": "two test"
}
和你一起想把它绑定到这两个参数:
and you want to bind it to the two params here:
@RequestMapping(value = "/Test", method = RequestMethod.POST)
public boolean getTest(String str1, String str2)
首先定义一个自定义注释,比如说 @JsonArg
,其中包含JSON路径,例如您想要的信息路径:
First define a custom annotation, say @JsonArg
, with the JSON path like path to the information that you want:
public boolean getTest(@JsonArg("/str1") String str1, @JsonArg("/str2") String str2)
现在写一个Custom HandlerMethodArgumentResolver 使用 JsonPath 定义以上解决实际参数:
Now write a Custom HandlerMethodArgumentResolver which uses the JsonPath defined above to resolve the actual argument:
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.io.IOUtils;
import org.springframework.core.MethodParameter;
import org.springframework.http.server.ServletServerHttpRequest;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;
import com.jayway.jsonpath.JsonPath;
public class JsonPathArgumentResolver implements HandlerMethodArgumentResolver{
private static final String JSONBODYATTRIBUTE = "JSON_REQUEST_BODY";
@Override
public boolean supportsParameter(MethodParameter parameter) {
return parameter.hasParameterAnnotation(JsonArg.class);
}
@Override
public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
String body = getRequestBody(webRequest);
String val = JsonPath.read(body, parameter.getMethodAnnotation(JsonArg.class).value());
return val;
}
private String getRequestBody(NativeWebRequest webRequest){
HttpServletRequest servletRequest = webRequest.getNativeRequest(HttpServletRequest.class);
String jsonBody = (String) servletRequest.getAttribute(JSONBODYATTRIBUTE);
if (jsonBody==null){
try {
String body = IOUtils.toString(servletRequest.getInputStream());
servletRequest.setAttribute(JSONBODYATTRIBUTE, body);
return body;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return "";
}
}
现在只需在Spring MVC中注册。有点涉及,但这应该干净利落。
Now just register this with Spring MVC. A bit involved, but this should work cleanly.
这篇关于使用Ajax将@RequestBody中的多个变量传递给Spring MVC控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!