Birthright Birthright - 6 months ago 28
JSON Question

Spring jackson can't parse JSON Object

I Have entity Users

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
}


User has many claims and orders. And my controller method such like this

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


And then i try to get user, i get this error
at

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


All my entities implements Serializible interface.
But if i try something like this

@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;

}


and it works.
I get json string like this

{
"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
}


What am I doing wrong?
My 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
Claims and orders entities

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
}


Apparently , he goes into a deep recursion. User has many claims and orders, and orders & claims belongs to user.
And so on to infinity.
But then again , in this case , i must set to null claims and orders?

Answer

You have circular reference on your entities. Please take a look at @JsonIdentityInfo annotation. And maybe below solution can help

@JsonIdentityInfo(generator=ObjectIdGenerators.IntSequenceGenerator.class, property="@id")
public class Claim implements Serializable {}

@JsonIdentityInfo(generator=ObjectIdGenerators.IntSequenceGenerator.class, property="@id")
public class Orders implements Serializable {}