2014-09-19 59 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