如何将复杂对象作为@requestBody?

时间:2016-03-15 10:04:00

标签: java spring spring-mvc

我有以下课程作为我的@RequestBody,但我无法发送请求,我尝试以不同的格式准备JSON,但没有一个工作。

处理程序

@RequestMapping("/grades")
@ResponseBody 
public Response addgrades(@RequestBody MyClass myClass){
    .....
}

返回-类

Class MyClass{
  private int code;
  private String name;
  private String fname;
  private List<Grade> grades;
  ...
}
Class Grade{
  private double grade;
  private double rate;
  private String teacher;
  ...
}

我使用的格式之一

  [
   {"code":1233,"name":"Jack","fname":"Moore"},
   {{"grade":83.33,"rate":11.11,"teacher":"Alex Jones"},     
    {"grade":77.67,"rate":12.11,"teacher":"Alex Jones"}}
  ]

1 个答案:

答案 0 :(得分:1)

您的JSON格式不正确。试试这个:

{
  "code": 123,
  "name": "myclass1",
  "fname": "the name",
  "grades": [{
    "grade": 1.0,
    "rate": 2.0,
    "teacher": "teach1"
  }, {
    "grade": 3.0,
    "rate": 4.0,
    "teacher": "teach2"
  }]
}