Spring jackson无法解析JSON Object

时间:2016-05-27 11:31:05

标签: java json spring maven jackson

我有实体用户

    public class Users implements Serializable {
        private Long id;
        private String password;
        private String email;
        private Boolean enabled;
        private String name;
        private String lastname;
        private String userRole;
        private String secondName;
        private String telephone;
        private String automobile;
        private List<Claim> claims;
        private List<Orders> ordersList;
    // getters and setters
}

用户有很多索赔和订单。我的控制器方法就像这样

  @RequestMapping(value = "get", method = GET)
    public
    @ResponseBody
    Users get() {
        return ((CustomUserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUserInfo();
    }

然后我尝试获取用户,我收到此错误     在

    com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:694)
        at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:157)
        at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:672)
//etc

我的所有实体都实现了Serializible接口。 但如果我尝试这样的事情

 @RequestMapping(value = "get", method = GET)
    public
    @ResponseBody
    Users get() {

        Users users = ((CustomUserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUserInfo();
        users.setClaims(null);
        users.setOrdersList(null);
        return users;

    }

它有效。 我得到像这样的json字符串

{
  "id" : 7,
  "password" : "$2a$11$IDMDPHQNiNEs9NP282zrGe3rn8la5WL8aR.RY3IFFa8y0NJc9ubdS",
  "email" : "serendipity@mail.ru",
  "enabled" : true,
  "name" : "Alex",
  "lastname" : "Zhukov",
  "userRole" : "ROLE_USER",
  "secondName" : null,
  "telephone" : "89274455166",
  "automobile" : "MercedesW222",
  "claims" : null,
  "ordersList" : null
}

我做错了什么? 我的pom.xml

 <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-mapper-asl</artifactId>
            <version>RELEASE</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>RELEASE</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>RELEASE</version>
        </dependency>

UPD 索赔和订单实体

   public class Orders implements Serializable {
        private Long id;
        private String status;
        private String address;
        private Timestamp fDate;
        private Integer sum;
        private Users users;
        private List<OrdersProducts> ordersProductsList;
//getters and setters
}
public class Claim implements Serializable {
    private Long id;
    private Timestamp fDate;
    private String reason;
    private String status;
    private String response;
    private Users user;
    private Integer sum;
    //getters and setters
    }

显然,他进行了深度递归。用户有许多索赔和订单,以及订单和声明属于用户。 等等到无限。 但话说回来,在这种情况下,我必须将声明和命令设为空?

1 个答案:

答案 0 :(得分:1)

您对实体有循环参考。请查看@JsonIdentityInfo注释。也许以下解决方案可以帮助

{{1}}