首页 > 解决方案 > 休眠延迟初始化对象的 Spring Data Rest HAL 响应格式问题

问题描述

技术栈: Spring Boot (2.3.1.RELEASE)、Spring Data JPA、Sprign Data Rest (3.3.1 RELEASE)、Hibernate (5.4.17 Final)、PostgresQL。我有父实体ProductEntity

@Data
@Entity
@Table(name = "product")
@EntityListeners(AuditingEntityListener.class)
public class ProductEntity {

    @NotNull
    @Id
    @EqualsAndHashCode.Exclude
    @Type(type = "pg-uuid")
    @GeneratedValue(generator = "uuid2")
    @GenericGenerator(name = "uuid2", strategy = "uuid2")
    private UUID id;

    @JsonProperty("productType")
    @ToString.Exclude
    @OneToOne(mappedBy = "productEntity", cascade = CascadeType.ALL, fetch = FetchType.LAZY, orphanRemoval = true, optional = false)
    private ProductTypeEntity productTypeEntity;
}

结束子实体ProductTypeEntity

@Data
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
@Entity
@Table(name = "product_type")
@JsonIgnoreProperties({"hibernateLazyInitializer"})
public class ProductTypeEntity extends BaseEntity implements RootAware<ProductEntity> {
    
    @NotBlank
    @Size(max = 255)
    private String type;

    @NotBlank
    @Size(max = 255)
    private String brand;

    @OneToOne(fetch = FetchType.LAZY, optional = false)
    @MapsId
    @JoinColumn(name = "id")
    @ToString.Exclude
    @EqualsAndHashCode.Exclude
    @JsonIgnore
    private ProductEntity productEntity;

    @Override
    public ProductEntity root() {
        return productEntity;
    }
}

最后有一个 Spring Data REST Repository ProductRepository

@RepositoryRestResource(collectionResourceRel = "products", path = "products")
public interface ProductRepository extends JpaRepository<ProductEntity, UUID> {

}

主要问题是当我请求GET http://localhost:8081/api/v1/products时,我在productType下收到了额外的字段内容,这些内容完全是多余的,并且无法充分解析响应:

{
    "_embedded": {
        "products": [
            {
                "productType": {
                    "content": {
                        "type": "type1",
                        "brand": "brand1"
                    }
                }
                "_links": {
                    "self": {
                        "href": "http://localhost:8081/api/v1/products/6b0e375e-4d2d-41af-b42f-989859652c81"
                    },
                    "productEntity": {
                        "href": "http://localhost:8081/api/v1/products/6b0e375e-4d2d-41af-b42f-989859652c81"
                    }
                }
            }
        ]
    },
    "_links": {
        "self": {
            "href": "http://localhost:8081/api/v1/products"
        },
        "profile": {
            "href": "http://localhost:8081/api/v1/profile/products"
        }
    },
    "page": {
        "size": 20,
        "totalElements": 1,
        "totalPages": 1,
        "number": 0
    }
}

但是当我将 productTypeEntity 从LAZY更改为EAGER

@JsonProperty("productType")
@ToString.Exclude
@OneToOne(mappedBy = "productEntity", cascade = CascadeType.ALL, fetch = FetchType.EAGER, orphanRemoval = true, optional = false)
private ProductTypeEntity productTypeEntity;

我收到没有内容字段的正确响应:

{
    "_embedded": {
        "products": [
            {
                "productType": {
                    "type": "type1",
                    "brand": "brand1",
                    "_links": {
                        "productEntity": {
                            "href": "http://localhost:8081/api/v1/products/6b0e375e-4d2d-41af-b42f-989859652c81"
                        }
                    }
                },
                "_links": {
                    "self": {
                        "href": "http://localhost:8081/api/v1/products/6b0e375e-4d2d-41af-b42f-989859652c81"
                    },
                    "productEntity": {
                        "href": "http://localhost:8081/api/v1/products/6b0e375e-4d2d-41af-b42f-989859652c81"
                    }
                }
            }
        ]
    },
    "_links": {
        "self": {
            "href": "http://localhost:8081/api/v1/products"
        },
        "profile": {
            "href": "http://localhost:8081/api/v1/profile/products"
        }
    },
    "page": {
        "size": 20,
        "totalElements": 1,
        "totalPages": 1,
        "number": 0
    }
}

主要问题是这种行为背后的原因是什么。我无法采用和使用它,因为我无法理解它的性质以及它是否可以解决。

标签: spring-boothibernatespring-data-jpaspring-data-resthateoas

解决方案


推荐阅读