简体   繁体   English

如何在 Spring 引导中忽略 Json 根名称?

[英]How to ignore Json root name in Spring Boot?

I have a question regarding Json serialization / deserialization in Spring Boot.我对 Spring Boot 中的 Json 序列化/反序列化有疑问。

I have an entity class, what has embedded id:我有一个实体 class,嵌入了 id:

@Entity
@Table(name="USER_TABLE")
@Getter
@Setter
public class User {

   private UserPK id;
   private String name;

}

/* Getters and Setters excluded */
public class UserPK implements Serializable {
    private Long userId;
    private Long personalId;
}

Now if I serialize an object, Spring Boot put a root "id" tag inside JSON.现在,如果我序列化 object,Spring 引导在 JSON 中放置一个根“id”标签。 Is it possible to exclude this, and put it into the same level of the name property?是否可以排除这个,并将其放入 name 属性的同一级别?

Actual result:实际结果:

{
    "id":{"userId":321, "personalId":222},
    "name":"John Doe"
}

Expected result:预期结果:

{
    "userId":321,
    "personalId":222,
    "name":"John Doe"
}

Thank you in advance!先感谢您!

You may annotate User.id field with @JsonUnwrapped annotation to effectively flatten the underlying structure like this:您可以使用@JsonUnwrapped注释对User.id字段进行注释,以有效地展平底层结构,如下所示:

public class User {
    @JsonUnwrapped
    private UserPK id;
    private String name;
}

See the docs .请参阅文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM