1

我已经开始使用 Spring 开发 REST API。我正在使用教程项目 gs-accessing-data-rest-initial,它很容易通过 Spring Tool Suite 下载,以便让一些东西尽快工作。

我使用 PagingAndSortingRepository 公开了两个相关实体(aplicacion 和 registros_app),并使用 @RepositoryRestResource 进行了注释,这使我能够正确公开实体。我查询应用程序时得到的结果是

 **GET http://localhost:8090/aplicacion**
{
  "_links" : {
    "self" : {
      "href" : "http://localhost:8090/aplicacion/{?page,size,sort}",
      "templated" : true
    }
  },
  "_embedded" : {
    "aplicacion" : [ {
      "nombre" : "app1",
      "_links" : {
        "self" : {
          "href" : "http://localhost:8090/aplicacion/2"
        },
        "registrosApp" : {
          "href" : "http://localhost:8090/aplicacion/2/registrosApp"
        },
        "tipoRegistrosApp" : {
          "href" : "http://localhost:8090/aplicacion/2/tipoRegistrosApp"
        }
      }
    }, {
      "nombre" : "app2",
      "_links" : {
        "self" : {
          "href" : "http://localhost:8090/aplicacion/1"
        },
        "registrosApp" : {
          "href" : "http://localhost:8090/aplicacion/1/registrosApp"
        },
        "tipoRegistrosApp" : {
          "href" : "http://localhost:8090/aplicacion/1/tipoRegistrosApp"
        }
      }
    } ]
  },
  "page" : {
    "size" : 20,
    "totalElements" : 2,
    "totalPages" : 1,
    "number" : 0
  }
}

这正是我期望得到的。因此,就分页而言,当我导航到 registrosApp 时,我期望得到相同的结果;但是,当我在任何 registrosApp 链接上执行获取时,我从查询中检索到的是

**GET http://localhost:8090/aplicacion/2/registrosApp**

{
  "_embedded" : {
    "registrosapp" : [ {
      "datos" : "{\"FechaInicio\":\"2014-09-16 18:08:44\",\"UsoMemoria\":\"UsedMemory:3 FreeMemory:491 Total Memory:495 Max Memory:989 \",\"InfoPool\":\"Active: 2\"}",
      "fecha_hora" : "2014-09-17T14:04:07.000+0000",
      "codTipoRegistro" : 1,
      "_links" : {
        "self" : {
          "href" : "http://localhost:8090/registrosApp/605"
        },
        "aplicacion" : {
          "href" : "http://localhost:8090/registrosApp/605/aplicacion"
        }
      }
    },{
      "datos" : "{\"FechaInicio\":\"2014-09-16 18:08:44\",\"UsoMemoria\":\"UsedMemory:3 FreeMemory:491 Total Memory:495 Max Memory:989 \",\"InfoPool\":\"Active: 2\"}",
      "fecha_hora" : "2014-09-17T14:04:07.000+0000",
      "codTipoRegistro" : 1,
      "_links" : {
        "self" : {
          "href" : "http://localhost:8090/registrosApp/667"
        },
        "aplicacion" : {
          "href" : "http://localhost:8090/registrosApp/667/aplicacion"
        }
      }
    } ]
  }
}

这实际上没有分页。当我浏览链接时,我需要获得一个分页的 json,因为 registrosApp 表增长得非常快。¿ 我能做些什么呢?

这是我的 registrosApp 和应用程序存储库的代码

@RepositoryRestResource(collectionResourceRel = "registrosapp", path = "registrosApp")
public interface RegistrosAppRepository extends PagingAndSortingRepository<RegistrosApp, Long> {

}

@RepositoryRestResource(collectionResourceRel = "aplicacion", path = "aplicacion")
public interface AplicacionRepository extends PagingAndSortingRepository<Aplicacion, Long> {

//List<Person> findByLastName(@Param("name") String name);

}

这些是我定义的实体

@Entity
@Table(name = "registros_app")
public class RegistrosApp {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long idRegistrosApp;
    private String datos;
    private Date fecha_hora;
    private long codTipoRegistro;
    public long getCodTipoRegistro() {
        return codTipoRegistro;
    }
    public void setCodTipoRegistro(long codTipoRegistro) {
        this.codTipoRegistro = codTipoRegistro;
    }
    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "idAplicacion", nullable = false, insertable = false, updatable = false)
    Aplicacion aplicacion;
    // private long idAplicacion;
    /*
     * public long getRegistros_app() { return idAplicacion; }
     * 
     * public void setRegistros_app(long registros_app) { this.idAplicacion =
     * registros_app; }
     */
    public String getDatos() {
        return datos;
    }
    public void setDatos(String datos) {
        this.datos = datos;
    }
    public Date getFecha_hora() {
        return fecha_hora;
    }
    public void setFecha_hora(Date fecha_hora) {
        this.fecha_hora = fecha_hora;
    }
}

@Entity
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
public class Aplicacion {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long aplicacionId;

    private String nombre;
    //relaciones uno a varios
    //relacion con la tabla registros_app
    @OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @JoinColumn(name = "idAplicacion", nullable = false)
    private Set<RegistrosApp> registrosApp = null;
    //relacion con la tabla tipo_registro_app
    @OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @JoinColumn(name = "idApp", nullable = false)
    private Set<TipoRegistrosApp> tipoRegistrosApp = null;
    public Set<TipoRegistrosApp> getTipoRegistrosApp() {
        return tipoRegistrosApp;
    }
    public void setTipoRegistrosApp(Set<TipoRegistrosApp> tipoRegistrosApp) {
        this.tipoRegistrosApp = tipoRegistrosApp;
    }
    @JsonProperty
    public Set<RegistrosApp> getRegistrosApp() {
        return registrosApp;
    }
    /**
     * Sets list of <code>Address</code>es.
     */
    public void setRegistrosApp(Set<RegistrosApp> rapps) {
        this.registrosApp= rapps;
    }
    @JsonProperty
    public String getNombre() {
        return nombre;
    }
    public void setNombre(String nombre) {
        this.nombre = nombre;
    }
}

您会注意到我的实体中的 apliacion 和 registrosapp 之间有一个 @onetomany 注释。

TL;DR 当我直接在 registrosapp 上查询时,我得到了我期望的分页结果。这里的问题是当我在相关实体之间导航时,我没有得到我需要的分页信息。¿ 在跨实体导航时,我可以做些什么来获得分页?对此的任何帮助将不胜感激。提前致谢。

4

1 回答 1

2

我会回答自己,以便让这个问题对其他正在努力解决这个问题的人有用。这个答案与 - Spring Data Rest Pageable Child Collection密切相关-

我所做的是在 RegistrosAppRepository 中设置一个方法,所以它保持这样

@RepositoryRestResource(collectionResourceRel = "registrosapp", path = "registrosApp")
public interface RegistrosAppRepository extends PagingAndSortingRepository<RegistrosApp, Long> {

    @RestResource(path = "byAplicacion", rel = "byAplicacion")
    public Page<RegistrosApp> findByAplicacion(@Param("aplicacion_id") Aplicacion aplicacion, Pageable p);

}

然后我通过在 registrosApp @RestResource(exported=false)集之前设置注释来隐藏出现在应用程序中的 registrosApp 链接。所以应用实体保持这样

@Entity
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
public class Aplicacion {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long aplicacionId;

    private String nombre;

    //relaciones uno a varios
    //relacion con la tabla registros_app
    @RestResource(exported=false)
    @OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @JoinColumn(name = "idAplicacion", nullable = false)
    private Set<RegistrosApp> registrosApp = null;

    //relacion con la tabla tipo_registro_app
    @OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @JoinColumn(name = "idApp", nullable = false)
    private Set<TipoRegistrosApp> tipoRegistrosApp = null;



    public Set<TipoRegistrosApp> getTipoRegistrosApp() {
        return tipoRegistrosApp;
    }

    public void setTipoRegistrosApp(Set<TipoRegistrosApp> tipoRegistrosApp) {
        this.tipoRegistrosApp = tipoRegistrosApp;
    }

    @JsonProperty
    public String getNombre() {
        return nombre;
    }

    public void setNombre(String nombre) {
        this.nombre = nombre;
    }

}

最后,我可以通过这种方式在这些实体之间导航:

**GET http://localhost:8090/registrosApp/search/byAplicacion?aplicacion_id=2&page=1&size=1**
{
  "_links" : {
    "next" : {
      "href" : "http://localhost:8090/registrosApp/search/byAplicacion?aplicacion_id=2&page=2&size=1"
    },
    "prev" : {
      "href" : "http://localhost:8090/registrosApp/search/byAplicacion?aplicacion_id=2&page=0&size=1"
    },
    "self" : {
      "href" : "http://localhost:8090/registrosApp/search/byAplicacion?aplicacion_id=2&page=1&size=1{&sort}",
      "templated" : true
    }
  },
  "_embedded" : {
    "registrosapp" : [ {
      "datos" : "{\"FechaInicio\":\"2014-09-16 18:08:44\",\"UsoMemoria\":\"UsedMemory:2 FreeMemory:492 Total Memory:495 Max Memory:989 \",\"InfoPool\":\"Active: 2\"}",
      "fecha_hora" : "2014-09-17T14:04:07.000+0000",
      "codTipoRegistro" : 1,
      "_links" : {
        "self" : {
          "href" : "http://localhost:8090/registrosApp/593"
        },
        "aplicacion" : {
          "href" : "http://localhost:8090/registrosApp/593/aplicacion"
        }
      }
    } ]
  },
  "page" : {
    "size" : 1,
    "totalElements" : 56,
    "totalPages" : 56,
    "number" : 1
  }
}

并且应用程序中的链接没有显示json中的registrosApp链接:

**GET http://localhost:8090/aplicacion**

{
  "_links" : {
    "self" : {
      "href" : "http://localhost:8090/aplicacion{?page,size,sort}",
      "templated" : true
    }
  },
  "_embedded" : {
    "aplicacion" : [ {
      "nombre" : "app1",
      "_links" : {
        "self" : {
          "href" : "http://localhost:8090/aplicacion/2"
        },
        "tipoRegistrosApp" : {
          "href" : "http://localhost:8090/aplicacion/2/tipoRegistrosApp"
        },
        "aplicacion" : {
          "href" : "http://localhost:8090/aplicacion/2/aplicacion"
        }
      }
    }, {
      "nombre" : "app2",
      "_links" : {
        "self" : {
          "href" : "http://localhost:8090/aplicacion/1"
        },
        "tipoRegistrosApp" : {
          "href" : "http://localhost:8090/aplicacion/1/tipoRegistrosApp"
        },
        "aplicacion" : {
          "href" : "http://localhost:8090/aplicacion/1/aplicacion"
        }
      }
    } ]
  },
  "page" : {
    "size" : 20,
    "totalElements" : 2,
    "totalPages" : 1,
    "number" : 0
  }
}
于 2014-09-19T22:39:44.933 回答