2014-09-19 38 views
1

我已经开始使用Spring处理REST API。我正在使用教程项目gs-accessible-data-rest-initial,它很容易通过Spring Tool Suite下载,以便尽快获得一些工作。href链接检索未分页的json - 弹簧数据rest jpa

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

**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和aplicacion库

@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; 
    } 
} 

实体可以注意到,我有aplicacion之间的@OneToMany批注registrosapp在我的实体中。

TL; DR当我直接在registrosapp上查询时,我得到了一个分页结果,如我所料。这里的问题是当我在相关实体之间导航时,我没有得到我需要的分页信息。 í我在跨实体浏览时为了获得分页而做什么?任何与此有关的帮助将得到真正的赞赏。提前致谢。

回答

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出现在aplicacion,通过在registrosApp集之前设置注释@RestResource(exported=false)。所以aplicacion实体保持这样

@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 
    } 
} 

和aplicacion链接不显示whithin的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 
    } 
} 
+0

请注意,'self'应该不是模板化的uri。 – Evert 2017-11-27 23:18:59