簡體   English   中英

Spring jackson 無法解析 JSON 對象

[英]Spring jackson can't parse JSON Object

我有實體用戶

    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>

更新索賠和訂單實體

   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
    }

顯然,他進入了深度遞歸。 用戶有很多索賠和訂單,訂單和索賠屬於用戶。 依此類推至無窮大。 但話說回來,在這種情況下,我必須設置為空聲明和訂單?

你對你的實體有循環引用。 請查看@JsonIdentityInfo 注釋。 也許下面的解決方案可以提供幫助

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM