小编use*_*730的帖子

使用 Jackson 转换器将嵌套的 json 绑定到 @RequestBody 对象

我有两节课

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)

java binding spring json jackson

5
推荐指数
1
解决办法
2万
查看次数

标签 统计

binding ×1

jackson ×1

java ×1

json ×1

spring ×1