查询单个资源时,Spring Data Rest输出将列对象作为JSON字段连接

问题描述 投票:1回答:1

我有公寓实体:

@Entity
public class Apartment extends AbstractEntity {

private String name;

@OneToOne(fetch = FetchType.EAGER)
@JoinColumn(nullable = false)
@RestResource(exported = false)
private Address address;

private String website;

@OneToMany(mappedBy = "apartment")
@RestResource(exported = false)
private Set<FloorPlan> floorPlans;
...

FloorPlan实体:

@Entity
public class FloorPlan extends AbstractEntity {

@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "apt_id", nullable = false)
private Apartment apartment;
private float bed;
private float bath;
private int priceFrom;
...

我将excerptProjection应用于Floorplan只显示床,浴和价格。当我查询公寓集合时,json输出看起来没问题:

{
"_embedded": {
    "apartments": [
        {
            "name": "Avalon Silicon Valley",
            "website": "https://www.avaloncommunities.com/california/sunnyvale-apartments/avalon-silicon-valley",
            "address": {
                "streetNumber": "1257",
                "street": "Lakeside Drive",
                "city": "Sunnyvale",
                "state": "CA",
                "zipCode": "94085",
                "fullAddress": "1257 Lakeside Drive, Sunnyvale, CA 94085"
            },
            "floorPlans": [
                {
                    "bed": 3,
                    "bath": 3,
                    "priceFrom": 4495
                },
                {
                    "bed": 3,
                    "bath": 2,
                    "priceFrom": 4760
                },

但是,如果我像http://localhost:8080/ag-api/apartments/1平面图这样的单一资源将输出Apartment Object作为其字段之一:

{
"name": "Avalon Silicon Valley",
"address": {
    "streetNumber": "1257",
    "street": "Lakeside Drive",
    "city": "Sunnyvale",
    "state": "CA",
    "zipCode": "94085",
    "fullAddress": "1257 Lakeside Drive, Sunnyvale, CA 94085"
},
"website": "https://www.avaloncommunities.com/california/sunnyvale-apartments/avalon-silicon-valley",
"floorPlans": [
    {
        "bed": 3,
        "bath": 3,
        "priceFrom": 4495,
        "_embedded": {
            "apartment": {
                "name": "Avalon Silicon Valley",
                "website": "https://www.avaloncommunities.com/california/sunnyvale-apartments/avalon-silicon-valley",
                "address": {
                    "streetNumber": "1257",
                    "street": "Lakeside Drive",
                    "city": "Sunnyvale",
                    "state": "CA",
                    "zipCode": "94085",
                    "fullAddress": "1257 Lakeside Drive, Sunnyvale, CA 94085"
                },
                "floorPlans": [

任何人都知道可能会发生什么?真的很感激。谢谢

spring spring-data-jpa spring-data spring-data-rest
1个回答
0
投票

不确定它是否会有所帮助,但在我发现的文档中,为了省略“_embedded”,你需要添加:

“spring.hateoas.use-hal-as-default-json-media-type = false”到application.properties。

© www.soinside.com 2019 - 2024. All rights reserved.