带有休眠惰性初始化对象的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存储库 ProductRepository

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

}

主要问题是当我请求 GET http:// localhost:8081 / api / v1 / products 时,我收到了其他字段 content 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",fetch = FetchType.EAGER,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"
                    },"number": 0
    }
}

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

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...