我有两节课
public class Parent {
private String name;
private int age;
private ArrayList<Child> children = new ArrayList<Child>();
//Setters and getter to follow..
}
public Class Child {
private String name;
private int age;
}
Run Code Online (Sandbox Code Playgroud)
Spring配置包括:
<bean id="jsonMessageConverter"
class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" />
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="jsonMessageConverter" />
</list>
</property>
</bean>
Run Code Online (Sandbox Code Playgroud)
控制器如下所示:
@RequestMapping(value = "/parents",
method = RequestMethod.POST,
headers="Content-Type=application/json")
public @ResponseBody Parent add(@RequestBody Parent parent, Model model) {
logger.debug("Received request to add a parent");
Parent tempParent = parentService.add(parent); // This will …Run Code Online (Sandbox Code Playgroud)