带有 Postgresql 的 Spring Boot REST API - 显示 JSON 数据、JdbcTemplate

问题描述

我用 Postgresql 作为数据库设计了 RESTful API。最初,我曾经在数据库中只有一张名为 Place 的表,在我的 API 中只有一个模型,如下所示:

public Place(@JsonProperty("id") UUID id,@JsonProperty("title") String title,@JsonProperty("description") String description,@JsonProperty("latitude") String latitude,@JsonProperty("longitude") String longitude,@JsonProperty("photo") String photo) {
    this.id = (id==null) ? UUID.randomUUID() : id;
    this.title = title;
    this.description = description;
    this.latitude = latitude;
    this.longitude = longitude;
    this.photo = photo;
}

我得到的 Json 输出如下:

[
    {
        "id": "dc507b33-ec47-4174-ad4b-111f7a5364b7","title": "Place title","description": "Some description","latitude": "latitude","longitude": "longitude","photo": "photoname",},

但我刚刚向应用程序添加了一些功能,我想从另外两个表中显示有关该地点的信息。我想要实现的 JSON 输出是这样的:

    [
        {
            "id": "dc507b33-ec47-4174-ad4b-111f7a5364b7","facts": [
            {"1" : "fact describtion},...                      
            ]
        },

显示所有数据字段的方法如下所示:

@Override
public List<Place> selectAllPlaces() {
    final String sql = "SELECT * FROM place";

    return jdbcTemplate.query(sql,(resultSet,i) -> {
        return new Place(
                UUID.fromString(resultSet.getString("id")),resultSet.getString("title"),resultSet.getString("description"),resultSet.getString("latitude"),resultSet.getString("longitude"),resultSet.getString("photo")
        );
    });
}

有人可以帮我吗?如何在 json 输出中的一处实现嵌套多个事实?如果需要,我可以提供更多代码

谢谢:)

解决方法

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

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

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