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

Nim*_*sky 91 java spring http 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.

Bij*_*men 85

你是对的,@ RequestBody带注释的参数应该保存请求的整个主体并绑定到一个对象,所以你基本上必须使用你的选项.

如果您绝对需要自己的方法,可以使用自定义实现:

说这是你的json:

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

你想把它绑定到这两个参数:

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

首先@JsonArg使用JSON路径定义自定义注释,例如所需信息的路径:

public boolean getTest(@JsonArg("/str1") String str1, @JsonArg("/str2") String str2)
Run Code Online (Sandbox Code Playgroud)

现在编写一个Custom HandlerMethodArgumentResolver,它使用上面定义的JsonPath来解析实际参数:

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 "";

    }
}
Run Code Online (Sandbox Code Playgroud)

现在只需在Spring MVC中注册即可.有点涉及,但这应该干净利落.

  • 仍然很有趣为什么这个选项没有添加到 spring。当您有 2 个 long 并且不想为其创建包装对象时,这似乎是一个合乎逻辑的选择 (4认同)
  • 如何创建自定义注释,请说@JsonArg? (2认同)

Kon*_*ong 71

虽然确实@RequestBody必须映射到单个对象,但该对象可以是a Map,因此这为您提供了一个很好的方法来实现您尝试实现的目标(无需编写一个关闭后备对象):

@RequestMapping(value = "/Test", method = RequestMethod.POST)
@ResponseBody
public boolean getTest(@RequestBody Map<String, String> json) {
   //json.get("str1") == "test one"
}
Run Code Online (Sandbox Code Playgroud)

如果你想要一个完整的JSON树,你也可以绑定到Jackson的ObjectNode:

public boolean getTest(@RequestBody ObjectNode json) {
   //json.get("str1").asText() == "test one"
Run Code Online (Sandbox Code Playgroud)

  • 但这通常不能用于两个JSON对象Map <String,Object> ... (2认同)
  • 我认为像“Map&lt;String, String&gt;”这样的动态方法的缺点是:API 文档库(swagger/springfox 等)可能无法从源代码中解析您的请求/响应模式。 (2认同)

azw*_*bar 15

用于传递多个对象、参数、变量等。您可以使用 jackson 库中的 ObjectNode 作为参数动态执行此操作。你可以这样做:

@RequestMapping(value = "/Test", method = RequestMethod.POST)
@ResponseBody
public boolean getTest(@RequestBody ObjectNode objectNode) {
   // And then you can call parameters from objectNode
   String strOne = objectNode.get("str1").asText();
   String strTwo = objectNode.get("str2").asText();

   // When you using ObjectNode, you can pas other data such as:
   // instance object, array list, nested object, etc.
}
Run Code Online (Sandbox Code Playgroud)

我希望这会有所帮助。


小智 8

您可以通过使用body和path变量来混合post参数,以获得更简单的数据类型:

@RequestMapping(value = "new-trade/portfolio/{portfolioId}", method = RequestMethod.POST)
    public ResponseEntity<List<String>> newTrade(@RequestBody Trade trade, @PathVariable long portfolioId) {
...
}
Run Code Online (Sandbox Code Playgroud)


Nbe*_*enz 8

简单的解决方案是创建一个包含 str1 和 str2 作为属性的有效负载类:

@Getter
@Setter
public class ObjHolder{

String str1;
String str2;

}
Run Code Online (Sandbox Code Playgroud)

当你可以通过之后

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

您的请求正文是:

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

  • 这个注解的包是什么?自动导入仅提供 import jdk.nashorn.internal.objects.annotations.Setter;编辑。我认为它是 Lombok https://projectlombok.org/features/GetterSetter。如果我错了请纠正我 (2认同)