Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用SpringMVC将json内容解析为post请求中的多参数_Java_Json_Spring_Spring Mvc - Fatal编程技术网

Java 使用SpringMVC将json内容解析为post请求中的多参数

Java 使用SpringMVC将json内容解析为post请求中的多参数,java,json,spring,spring-mvc,Java,Json,Spring,Spring Mvc,如何在post请求中将json自动解析为多参数,且内容类型为“application/json”? 例如,我有这样一个控制器方法: @RequestMapping(value = "/test", method = RequestMethod.POST) public Object testPost(@RequestBody Student student) { return student; } “学生”班是: public class Student { private I

如何在post请求中将json自动解析为多参数,且内容类型为“application/json”? 例如,我有这样一个控制器方法:

@RequestMapping(value = "/test", method = RequestMethod.POST)
public Object testPost(@RequestBody Student student) {
    return student;
}
“学生”班是:

public class Student {
    private Integer id;
    private String name;
    getter&setter
}
json是:

{"student":{"id":1,"name":"测试"}}
{"id":1,"name":"测试"}
控制器返回空对象(当然不是null,“id”和“name”属性为null)。 我知道如果json是:

{"student":{"id":1,"name":"测试"}}
{"id":1,"name":"测试"}
或者我将学生扭曲到其他班级的“学生”字段,但我不能这样做。 我发现它不能使用@RequestParams或@ModelAttribute,因为在RequestParamMethodArgumentResolver和ModelAttributeMethodProcessor中,它们只能在ServletRequest#getParameter中找到值。 那么,SpringMVC能解决这个问题吗?还是我只能定制? (原谅我,我的英语很差…)


感谢您的帮助,我无法包装学生,因为我使用的是公司内部工具,可能这不是一个非常常见的功能,因此我决定编写一个自定义参数解析器(HandlerMethodArgumentResolver)。

尝试将“学生”对象包装到其他对象中。对于您正在执行的当前实现,json应该如下所示

{“id”:1,“名称”:测试"}

但是,如果您将JSON作为

{“学生”:{“id”:1,“姓名”:”测试“}}

然后必须将它包装到另一个Java类中,JSON解析器才能正确解析它。

尝试将“Student”对象包装到另一个对象中。对于当前的实现,JSON应该如下所示

{“id”:1,“名称”:测试“}

但是,如果您将JSON作为

{“学生”:{“id”:1,“姓名”:”测试“}}


然后,它必须被包装在另一个Java类中,JSON解析器才能正确解析它。

您可以在该方法处理请求之前处理JSON消息,在treat body中,您可以将JSON字符串转换为学生对象,然后返回它,该学生对象将保存在名为student r的requestAttribute中把它放在你的测试柱上

@RequestMapping(path="/test", method = RequestMethod.POST)
public String testPost(@ModelAttribute("student") Student student) {
    System.out.println("student->" + student);

    return "response";

}

@ModelAttribute("student")
public Student treatBody(@RequestBody String body) {
    //retrieve student from json 
    return student;
}

您可以在方法处理请求之前处理json消息,在treat body中,您可以将json字符串转换为student对象,然后返回它,该student对象将保存在requestAttribute中,名为student,并在您的testPost中检索它

@RequestMapping(path="/test", method = RequestMethod.POST)
public String testPost(@ModelAttribute("student") Student student) {
    System.out.println("student->" + student);

    return "response";

}

@ModelAttribute("student")
public Student treatBody(@RequestBody String body) {
    //retrieve student from json 
    return student;
}

在另一节课中,将学生包装到“学生”字段,但我不能这样做"为什么不呢?如果你不能这样做,那么你也不能做其他事情。@zeroflagL他可以用@ModelAttribute@achabahe我尝试了@modeldattribute,并且阅读了源代码,您可以发现@modeldattribute只是读取代码中解析参数的请求#getParameterMap:modeldattributeMethodProcessor#resolveArgument->DefaultDataBinderFactory#createBinderInstance->WebRequestDataBinder#Bind您可以检查我是否已经做出了答案并且它对我有效“在其他类中将学生包装到“学生”字段,但我不能这样做”为什么不呢?如果你不能这样做,那么你也不能做其他事情。@zeroflagL他可以用@ModelAttribute@achabahe我尝试了@modeldattribute,并且阅读了源代码,您可以发现@modeldattribute只是读取代码中解析参数的请求#getParameterMap:modeldattributeMethodProcessor#resolveArgument->DefaultDataBinderFactory#createBinderInstance->WebRequestDataBinder#Bind您可以检查我已经给出了答案,它对我有效是的,我知道它可以有效,但我受到公司内部工具的限制,无法更改json。因此我决定定制…您的定制实现对您有效吗?或者您还在解决问题吗?是的,我做了一个简单的修改自定义实现和它的工作,谢谢。是的,我知道它可以工作,但我受到公司内部工具的限制,无法更改json。所以我决定进行自定义…您的自定义实现为您工作了吗?还是您仍在解决问题?是的,我做了自定义实现和它的工作,谢谢。是的,这可以工作。我忘了说我想要一个通用功能来自动解析对象,比如@RequestParam work。是的,这可以是work。我忘了说我想要一个通用功能来自动解析对象,比如@RequestParam work。