2016-07-11 8 views
0

私はSpringブートデータRESTからPagingAndSortingRepositoryを実装しようとしています。Spring REST PagingAndSortingRepositoryは 'nex't&' previous 'リンクを表示しません

確かにページ応答を返しますが、ページ間のナビゲーションリンクを表示する方法はありません。私はこれに関する多くのチュートリアルとドキュメンテーションを見てきました。提供されているPagingAndSortingRepositoryを使用した結果として起こるはずです。

誰かに助言してください!それはとても簡単なはずです。 ありがとうございます。

クラス:

@RestController 
public class CommentController { 

private final CommentFacade facade; 

@Autowired 
public CommentController(CommentRepository commentdao) { 

    this.facade = new CommentFacadeImpl(commentdao); 
} 


@RequestMapping(value = "/comments", method = RequestMethod.GET) 
public PagedResources<Resource<Comment>> getPagedList(@RequestParam(value = "user-id", required = true) Long userId) { 

    Pageable pageable = new PageRequest(1, 10); 

    Page<Comment> commentsList = facade.findAll(pageable); 

    List<Resource<Comment>> resourceList = new ArrayList<>(); 

    for (Comment comment : commentsList) { 

     Resource<Comment> commentResource = new Resource<>(comment); 

     commentResource.add(linkTo(methodOn(CommentController.class) 
       .getSpecificComment(comment.getId())).withSelfRel()); 

     resourceList.add(commentResource); 
    } 

    PagedResources<Resource<Comment>> resources = new PagedResources<>(resourceList, 
      new PageMetadata(commentsList.getNumber(), commentsList.getTotalElements(), commentsList.getTotalPages(), 5), 
      linkTo(methodOn(CommentController.class) 
        .getPagedList(userId)).withSelfRel()); 

    return resources; 
} 

}

@RepositoryRestResource 
public interface CommentRepository extends  PagingAndSortingRepository<Comment, Long> { 

Page findAll(Pageable pageRequest); 

}

@Entity 
@Table(name="comment") 
@EnableEntityLinks 
@RepositoryRestResource 
public class Comment { 

@Id 
@GeneratedValue(strategy= GenerationType.AUTO) 
@Column(name="id") 
private @JsonIgnore Long id; 

@Column(name="created") 
private Date created; 

@Column(name="latitude") 
private @JsonIgnore Float latitude; 

@Column(name="longitude") 
private @JsonIgnore Float longitude; 

@Column(name="ip") 
private @JsonIgnore String ip; 

@Column(name="text") 
private String comment; 


@Column(name="sender_id") 
private Long senderId; 

@JsonIgnore 
private Long postId; 

@JsonIgnore 
@Column(name="deleted") 
private Boolean deleted; 

protected Comment() {}; 

public Comment(String comment, Long senderId, Long post_id) { 
    this.id = null; 
    this.created = new Date(); 
    this.latitude = 12.1111111138563456f; 
    this.longitude = -17.444333333333f; 
    this.ip = "34.6.5.213"; 
    this.comment = "some comment"; 
    this.senderId = 1L; 
    this.postId = post_id; 
    this.deleted = false; 
} 

public Long getId() { 
    return id; 
} 

public void setId(Long id) { 
    this.id = id; 
} 

public Date getCreated() { 
    return created; 
} 

public void setCreated(Date created) { 
    this.created = created; 
} 

public Float getLatitude() { 
    return latitude; 
} 

public void setLatitude(Float latitude) { 
    this.latitude = latitude; 
} 

public Float getLongitude() { 
    return longitude; 
} 

public void setLongitude(Float longitude) { 
    this.longitude = longitude; 
} 

public String getIp() { 
    return ip; 
} 

public void setIp(String ip) { 
    this.ip = ip; 
} 

public String getComment() { 
    return comment; 
} 

public void setComment(String comment) { 
    this.comment = comment; 
} 

public Long getSenderId() { 
    return senderId; 
} 

public void setSenderId(Long senderId) { 
    this.senderId = senderId; 
} 

@ManyToOne 
@JoinColumn(name = "post_id") 
@JoinTable(name = "post") 
public Long getPostId() { 
    return postId; 
} 

public void setPostId(Long postId) { 
    this.postId = postId; 
} 

public Boolean getDeleted() { 
    return deleted; 
} 

public void setDeleted(Boolean deleted) { 
    this.deleted = deleted; 
} 

}

マイJSON HAL出力:

{ 
 
    "_embedded": { 
 
    "commentList": [ 
 
     { 
 
     "created": 1468176648000, 
 
     "comment": "some comment", 
 
     "senderId": 1, 
 
     "_links": { 
 
      "self": { 
 
      "href": "http://localhost:8090/comments/11" 
 
      } 
 
     } 
 
     }, 
 
     { 
 
     "created": 1468176649000, 
 
     "comment": "some comment", 
 
     "senderId": 1, 
 
     "_links": { 
 
      "self": { 
 
      "href": "http://localhost:8090/comments/12" 
 
      } 
 
     } 
 
     }, 
 
     { 
 
     "created": 1468176650000, 
 
     "comment": "some comment", 
 
     "senderId": 1, 
 
     "_links": { 
 
      "self": { 
 
      "href": "http://localhost:8090/comments/13" 
 
      } 
 
     } 
 
     }, 
 
     { 
 
     "created": 1468176650000, 
 
     "comment": "some comment", 
 
     "senderId": 1, 
 
     "_links": { 
 
      "self": { 
 
      "href": "http://localhost:8090/comments/14" 
 
      } 
 
     } 
 
     }, 
 
     { 
 
     "created": 1468176651000, 
 
     "comment": "some comment", 
 
     "senderId": 1, 
 
     "_links": { 
 
      "self": { 
 
      "href": "http://localhost:8090/comments/15" 
 
      } 
 
     } 
 
     }, 
 
     { 
 
     "created": 1468176651000, 
 
     "comment": "some comment", 
 
     "senderId": 1, 
 
     "_links": { 
 
      "self": { 
 
      "href": "http://localhost:8090/comments/16" 
 
      } 
 
     } 
 
     }, 
 
     { 
 
     "created": 1468176678000, 
 
     "comment": "some comment", 
 
     "senderId": 1, 
 
     "_links": { 
 
      "self": { 
 
      "href": "http://localhost:8090/comments/17" 
 
      } 
 
     } 
 
     }, 
 
     { 
 
     "created": 1468176679000, 
 
     "comment": "some comment", 
 
     "senderId": 1, 
 
     "_links": { 
 
      "self": { 
 
      "href": "http://localhost:8090/comments/18" 
 
      } 
 
     } 
 
     }, 
 
     { 
 
     "created": 1468176679000, 
 
     "comment": "some comment", 
 
     "senderId": 1, 
 
     "_links": { 
 
      "self": { 
 
      "href": "http://localhost:8090/comments/19" 
 
      } 
 
     } 
 
     }, 
 
     { 
 
     "created": 1468176680000, 
 
     "comment": "some comment", 
 
     "senderId": 1, 
 
     "_links": { 
 
      "self": { 
 
      "href": "http://localhost:8090/comments/20" 
 
      } 
 
     } 
 
     } 
 
    ] 
 
    }, 
 
    "_links": { 
 
    "self": { 
 
     "href": "http://localhost:8090/comments?user-id=1" 
 
    } 
 
    }, 
 
    "page": { 
 
    "size": 1, 
 
    "totalElements": 4, 
 
    "totalPages": 5, 
 
    "number": 38 
 
    } 
 
}

動作しているようですが、ないナビゲーションリンク。何か案は?

+0

自分でUIを実装する必要があります。ここではかなり良いチュートリアルです:http://springinpractice.com/2012/05/11/pagination-and-sorting-with-spring-data-jpa –

+0

本当ですか?春のブーツでも?使用可能なチュートリアルでは、CrudRepositoryの代わりにPagingAndSortingRepositoryを使用し、Page not Listを返すと、自動的にそれらを追加する必要があります。私はこれを手作業でやっていたのですが、ドキュメンテーションは私が必要としないことを示唆しています。誰でもこれを確認できますか? – gezinspace

+0

あなたは春のブートを使用していたと言っていません:) –

答えて

0

リンクはPagedResourcesAssemblerで追加されます。これは最初にPagedResourcesを手動で作成する必要があります。

Spring Data RESTを使用している場合、コントローラコードはまったく必要なく、Spring Data RESTにリソースを作成させることができます。サンプルについてはgetting started guideを参照してください。

+0

よろしくお願いいたします。これは私が持っている印象でした.....その春のデータはコントローラが必要なのですが、1)検証/検証などの理由で私のプロジェクトでコントローラを使用しなければなりません。私は実際に投稿者の著者か、著者との友人かどうかを確認してください。さもなければ私はそれをしません。 2)エンドポイントがヒットした後、複数のDAO /リポジトリコールを使用する必要があるシナリオ。 私はコントローラを持つことができますが、まだ機能を得るいくつかの方法はありますか?私は複雑な十分な例を見つけたことはありません – gezinspace

+0

私は多くの他の人々が同じ必要性を持っていたと推測しています...ユーザーがログインしているだけでデータを返すのではなく、そのデータを処理するために、他のソースなどのデータと組み合わせてください。 – gezinspace

+0

「私がspring-data-restを使用すると私のプログラムLOGICはどこに行くの? " – gezinspace

関連する問題