Spring MVC:使用数组发布请求和json对象:错误请求
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28838261/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring MVC : post request and json object with array : bad request
提问by Fab313
I'm trying to retrieve parameters from a http POST request with Spring MVC.
我正在尝试使用 Spring MVC 从 http POST 请求中检索参数。
The request contains the following json object (content-type : application/json), which itself contains an array of customObjects :
该请求包含以下 json 对象(内容类型:application/json),它本身包含一个 customObjects 数组:
{
"globalId":"338",
"lines":[
{
"id": "someId",
"lib":"blabla",
...
}
]
}
Here's the code I'm trying to use :
这是我尝试使用的代码:
@RequestMapping(method = RequestMethod.POST, value = "/valider")
@ResponseBody
public void valider(final HttpServletRequest request, @RequestParam("globalId") final String globalId, @RequestParam("lines") final MyCustomObject[] lines) {
All I'm getting is a "bad request" error (http 400).
我得到的只是一个“错误的请求”错误(http 400)。
Is it possible to separately retrieve the two parameters "globalId" and "lines" ? Or since they are in the same json object, it has to be treated has a single parameter ? How do you proceed when you have more than one parameter in a Post request ?
是否可以分别检索两个参数“globalId”和“lines”?或者因为它们在同一个 json 对象中,它必须被视为具有单个参数?当 Post 请求中有多个参数时,如何进行?
采纳答案by RKodakandla
I think you're looking for something like `@RequestBody. Create a class to represent your JSON data. In your case, this class will contain two member variables - globalId as a string and lines as an array of the object it represents. Then in your controller method, you will use the @RequestBody annotation on this class type so that Spring will be able to convert the JSON into object. Check the examples below.
我认为您正在寻找类似`@RequestBody. 创建一个类来表示您的 JSON 数据。在您的情况下,此类将包含两个成员变量 - globalId 作为字符串和行作为它表示的对象的数组。然后在您的控制器方法中,您将在此类类型上使用 @RequestBody 批注,以便 Spring 能够将 JSON 转换为对象。检查下面的例子。
http://www.leveluplunch.com/java/tutorials/014-post-json-to-spring-rest-webservice/
http://www.leveluplunch.com/java/tutorials/014-post-json-to-spring-rest-webservice/
JQuery, Spring MVC @RequestBody and JSON - making it work together
JQuery、Spring MVC @RequestBody 和 JSON - 使其协同工作
http://www.techzoo.org/spring-framework/spring-mvc-requestbody-json-example.html
http://www.techzoo.org/spring-framework/spring-mvc-requestbody-json-example.html
回答by Fab313
Indeed, I have to use @RequestBody to get the JSON object.
事实上,我必须使用@RequestBody 来获取 JSON 对象。
Quick summary, depending on how the parameters are passed in the http POST body request :
快速总结,取决于参数在 http POST 正文请求中的传递方式:
one JSON object (Content-Type: application/json), use @RequestBodyto map the json object to a java object
multiple parameters (Content-Type: application/x-www-form-urlencoded), use @RequestParamfor each parameter
一个 JSON 对象(Content-Type: application/json),使用@RequestBody将 json 对象映射到一个 java 对象
多个参数(Content-Type: application/x-www-form-urlencoded),每个参数使用@RequestParam
回答by Oomph Fortuity
create model object to map your Json data
创建模型对象以映射您的 Json 数据
class DLibrary{
int id;
String lib;
//getters/setters
}
class GLibrary{
int globalId;
List<DLibrary> lines;
//getters/setters
}
Replace your controller code with below
用下面的替换您的控制器代码
@RequestMapping(method = RequestMethod.POST, value = "/valider")
@ResponseBody
public void valider(@RequestBody GLibrary gLibrary) {
@RequestBody annotation will map Json to Java Object implicitly. To achieve this spring must require Hymanson-core and Hymanson-mapperlibrary included in your application and your Java class should have getter and setters i.e it must follow bean standards.
@RequestBody 注解会将 Json 隐式映射到 Java 对象。为了实现这个春天,你的应用程序中必须包含Hymanson-core 和 Hymanson-mapper库,并且你的 Java 类应该有 getter 和 setter,即它必须遵循 bean 标准。

