无法获取JSON中的嵌套对象列表

时间:2017-02-12 19:29:05

标签: java json spring hibernate

我遇到了这个问题,JSON不包含嵌套对象列表。

我有两个课程,其中一个课程载有关于汽车服务的信息,另一个课程包含有关服务的信息。

一个autoservice可以有很多服务。所以,我们有一对一的关系。

AutoService上课:

@Entity
@Table(name = "AutoRate")
public class AutoService {

    public AutoService() {
    }

    @Id
    @GeneratedValue(generator = "increment")
    @GenericGenerator(name = "increment", strategy = "increment")
    private long id;

    @Column(name = "serviceName", nullable = false)
    private String serviceName;

    @Column(name = "imageURL", nullable = false)
    private String imageURL;

    @Column(name = "mapCoordinate", nullable = false)
    private String mapCoordinate;

    @Column(name = "websiteURL", nullable = false)
    private String websiteURL;

    @Column(name = "phoneNumber", nullable = false)
    private String phoneNumber;

    @OneToMany(fetch = FetchType.EAGER)
    @JoinColumn(name = "autoServiceId")
    private List<Service> services;

    public long getId() {
        return id;
    }

    public String getServiceName() {
        return serviceName;
    }

    public String getImageURL() {
        return imageURL;
    }

    public String getMapCoordinate() {
        return mapCoordinate;
    }

    public String getWebsiteURL() {
        return websiteURL;
    }

    public String getPhoneNumber() {
        return phoneNumber;
    }
}

Service上课:

@Entity
@Table(name = "Service")
public class Service {

    public Service() {
    }

    @Id
    @GeneratedValue(generator = "increment")
    @GenericGenerator(name = "increment", strategy = "increment")
    @Column(name = "serviceId", unique = true, nullable = false)
    private long serviceId;

    @Column(name = "serviceName", nullable = false)
    private String serviceName;

    @Column(name = "category", nullable = false)
    private String category;

    @Column(name = "price", nullable = false)
    private int price;

    @Column(name = "autoServiceId", nullable = false)
    private long autoServiceId;

    public long getId() {
        return serviceId;
    }

    public String getCategory() {
        return category;
    }


    public int getPrice() {
        return price;
    }

    public String getServiceName() {
        return serviceName;
    }

    public long getAutoServiceId() {
        return autoServiceId;
    }
}

我也使用JpaRepository从db获取对象:

public interface AutoRateRepository extends JpaRepository<AutoService, Long> {
}

这是Controller类:

@RestController
@RequestMapping("/directory")
public class ServiceController {

    @Autowired
    private AutoRateService dataBaseService;

    @RequestMapping(value = "/get", method = RequestMethod.GET)
    @ResponseBody
    public List<AutoService> getData(){
        List<AutoService> dataList = dataBaseService.getAll();
        return dataList;
    }
}

但是当我试图获得JSON对象时,我接下来会:

[
  {
    "id": 1,
    "serviceName": "SpeedyName",
    "imageURL": "Url for speedy",
    "mapCoordinate": "123123 44121 ",
    "websiteURL": "speedy.com",
    "phoneNumber": "1231251"
  },
  {
    "id": 2,
    "serviceName": "Другой сервис",
    "imageURL": "Урл для второго сервиса",
    "mapCoordinate": "123 12фывфы",
    "websiteURL": "другойсервис.ком",
    "phoneNumber": "12312333"
  }
]

没有来自数据库(List<Services>

的嵌套对象列表

你能帮我解决这个问题吗?

我被告知我错过了@JsonManagedReference注释,但它没有帮助我(

1 个答案:

答案 0 :(得分:1)

根据评论中的说明,您在getServices()实体中错过了AutoService方法,从而暴露了序列化集合。

作为旁注,如果您的Service实体具有对父AutoService的反向引用,则会感到厌倦。如果是这样,您需要确保您的JSON框架知道如何处理循环循环,或者在序列化AutoService实例时需要触发忽略后引用以避免堆栈溢出。