我正尝试使用杰克逊的JsonView
,但已经通知了
log.trace("Converting to json for elasticsearch >>> {}",objectMapper.
writer().withView(Views.Elasticsearch.class).writeValueAsString(object));
将object
内的所有嵌套对象打印为json嵌套对象
log.trace("Converting to json for elasticsearch >>> {}",objectMapper.
writerWithView(Views.Elasticsearch.class).writeValueAsString(object));
仅打印具有object
字段值的json,但不打印嵌套的Java对象,该对象具有所有Spring Data @Entity
功能,包括implements Serializable
,getter和setter ,因此所有使我期望它们被打印为根json对象的嵌套部分的东西。这是否可以预期?如果可以预期,writerWithView(Views.Elasticsearch.class)
应该如何准备产生正确的json字符串?
该对象使用一些额外的Spring Data Elasticsearch和JPA批注:
@Entity
@Table(name = "good")
@org.springframework.data.elasticsearch.annotations.Document(indexName = "good", shards = 1, replicas = 0, refreshInterval = "-1")
public class Good implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequenceGenerator")
@SequenceGenerator(name = "sequenceGenerator")
@org.springframework.data.elasticsearch.annotations.Field(type = FieldType.Keyword)
@JsonView(Views.Elasticsearch.class)
private Long id;
@NotNull
@Column(name = "short_name", nullable = false)
@JsonView(Views.Elasticsearch.class)
@Field(store = true, index = true, type=FieldType.Text)
private String shortName;
@NotNull
@Column(name = "description", nullable = false)
@JsonView(Views.Elasticsearch.class)
@Field(store = true, index = true, type=FieldType.Text)
private String description;
@Lob
@Column(name = "image", nullable = false)
@JsonView(Views.Rest.class)
private byte[] image;
@Column(name = "image_content_type", nullable = false)
@JsonView(Views.Rest.class)
private String imageContentType;
@NotNull
@Column(name = "price", nullable = false)
@JsonView(Views.Elasticsearch.class)
@Field(store = true, index = true, type=FieldType.Integer)
private Integer price;
@NotNull
@Column(name = "available", nullable = false)
@JsonView(Views.Elasticsearch.class)
@Field(store = true, index = true, type=FieldType.Boolean)
private Boolean available;
@OneToMany(mappedBy = "good")
@JsonView(Views.Elasticsearch.class)
//@Field(store = true, index = true, type=FieldType.Object)
private Set<Shipment> shipments = new HashSet<>();
...
,引用的Shipment
对象是:
@Entity
@Table(name = "shipment")
@org.springframework.data.elasticsearch.annotations.Document(indexName = "shipment", shards = 1, replicas = 0, refreshInterval = "-1")
public class Shipment implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequenceGenerator")
@SequenceGenerator(name = "sequenceGenerator")
@org.springframework.data.elasticsearch.annotations.Field(type = FieldType.Keyword)
private Long id;
@Enumerated(EnumType.STRING)
@Column(name = "status")
@Field(store = true, index = true, type=FieldType.Text)
@JsonView(Views.Elasticsearch.class)
private ShipmentStatus status;
@Column(name = "quantity")
@Field(store = true, index = true, type=FieldType.Integer)
@JsonView(Views.Elasticsearch.class)
private Integer quantity;
...
enter code here