使用 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:
Say this is your 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)
现在编写一个自定义 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屋!