在Jackson 2 anotation中反序列化时如何在JSON中省略包装器对象

时间:2017-02-16 14:29:51

标签: java json jaxb jackson2

我有一个简单的休息与jax-rs

@POST
@Path("/user")
@Produces({ "application/json" })
@Consumes({ "application/json" })
public Response createuser(User user){

    user.name = "pepebotero";
    return  Response.ok(user).build();

我们想要绑定到POJO用户

public class User {
    public String name;
    public String surname;
}

将JSON与对象用户一起使用

{
    "User":{
    "name":"geroge",    
    "surname":"jordi"
    }   
}

我希望找到一种方法来解决尽可能少的干扰,以避免下面的错误。我正在寻找面向注释的解决方案

Caused by: com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "User" (class com.endpoint.User), not marked as ignorable (2 known properties: "name", "surname"])
 at [Source: org.apache.cxf.transport.http.AbstractHTTPDestination$1@576599b5; line: 2, column: 10] (through reference chain: com..endpoint.User["User"]

1 个答案:

答案 0 :(得分:0)

您可以使用@JsonTypeInfo指定包装器对象。基本上这样的事情会做。请注意,类名必须与包装器对象相同,我认为它也区分大小写。尝试一下。

@JsonTypeInfo(use=Id.NAME, include=As.WRAPPER_OBJECT)