Spring REST Jpa HATEOAS链接未被创建

时间:2016-07-09 15:45:23

标签: java spring rest spring-mvc jpa

我知道之前已经提到过非常类似的问题,但我很难将其应用到我的问题中。

我最近开始使用jpa存储库来满足我的数据持久性需求,直到现在我一直在构建我想要的HAL链接。然后我发现,如果我开始使用@JoinTable和@JoinColumn注释,那么我可以为我生成我的链接。

我的问题是,当我点击我的/帖子的端点时,我没有在响应中获得评论的HAL链接。

@Entity
@Table(name="post")
public class Post {

@Id
@Column(name="id")
private @JsonIgnore Long id;

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

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

@Column(name="event_id")
private Long eventId;

protected Post () {};

public Long getId() {
    return id;
}

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

 public String getText() {
    return text;
}

public void setText(String text) {
    this.text = text;
}

public Long getSenderId() {
    return senderId;
}

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

public Long getEventId() {
    return eventId;
}

public void setEventId(Long eventId) {
    this.eventId = eventId;
}
}


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

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

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

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

@JsonIgnore
@JoinColumn(name="post_id")
private Long post_id;

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

@ManyToOne
@JoinTable(name="post")
private Post post;

protected Comment() {};

public Long getId() {
    return id;
}

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

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

public Long getPostId() {
    return post_id;
}

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

public Post getPost() {
    return post;
}

public void setPost(Post post) {
    this.post 
}
}


@RestController
public class CommentController {

private JdbcOperations jdbc;

private final CommentRepository commentDao;

@Autowired
public CommentController(CommentRepository postDao) {
    this.commentDao = postDao;
}

@RequestMapping(value = "/comments", method = RequestMethod.GET)
public ResponseEntity<Resources<Resource<Comment>>> getPagedList(@RequestParam(value = "user-id", required = true) long userId,
                                                                 @RequestParam(value = "page-size", required = false, defaultValue = "20") Long pageSize,
                                                                 @RequestParam(value = "page", required = false, defaultValue = "1") Long pageNum) {

    List<Comment> commentsList = (ArrayList) commentDao.findAll();

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

    for (Comment comment : commentsList) {

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

        postResource.add(linkTo(methodOn(CommentController.class)
                .getPagedList(userId, pageSize,pageNum)).withSelfRel());


        resourceList.add(postResource);
    }

    Resources<Resource<Comment>> resources = new Resources<>(resourceList,
            linkTo(methodOn(PostController.class)
                    .getPagedList(userId, pageSize, pageNum)).withSelfRel());

    return ResponseEntity.ok(resources);
}


@RestController
public class PostController {

private final PostRepository postDao;

@Autowired
public PostController(PostRepository postDao) {
    this.postDao = postDao;
}

@RequestMapping(value = "/posts", method = RequestMethod.GET)
public ResponseEntity<Resources<Resource<Post>>> getPagedList(@RequestParam(value="user-id", required = true) long userId,
                                                                 @RequestParam(value = "page-size", required = false, defaultValue = "20") Long pageSize,
                                                                 @RequestParam(value = "page", required = false, defaultValue = "1") Long pageNum) {

    List<Post> modelsList = (ArrayList) postDao.readBydeletedIsFalseOrderByCreated();
    List<Resource<Post>> resourceList = new ArrayList<>();

    for (Post post : modelsList) {

        Resource<Post> resource = new Resource<>(post);

        resource.add(linkTo(methodOn(PostController.class)
                .getSpecificModel(post.getId())).withSelfRel());

        resource.add(linkTo(methodOn(UserController.class).getSpecificModel(post.getSenderId()))
                .withSelfRel().withRel("sender"));

        if (post.getEventId() != null) {
            resource.add(linkTo(methodOn(EventController.class)
                    .getSpecificModel(post.getEventId())).withSelfRel().withRel("event"));
        }

        resourceList.add(resource);
    }

    Resources<Resource<Post>> resources = new Resources<>(resourceList,
            linkTo(methodOn(PostController.class)
                    .getPagedList(userId, pageSize, pageNum)).withSelfRel());

    return ResponseEntity.ok(resources);
}

当我点击/ posts端点时得到的响应是这样的,没有评论链接:

http://localhost:8090/posts?user-id=1

{
  "_embedded": {
    "postList": [
      {
        "created": -84330000000,
        "text": "second post",
        "senderId": 2,
        "eventId": null,
        "_links": {
          "self": {
            "href": "http://localhost:8090/posts/2"
          },
      "sender": {
        "href": "http://localhost:8090/users/2"
      }
    }
  },
  {
    "created": 1286665200000,
    "text": "dfgtfy",
    "senderId": 1,
    "eventId": null,
    "_links": {
      "self": {
        "href": "http://localhost:8090/posts/1"
      },
      "sender": {
        "href": "http://localhost:8090/users/1"
      }
    }
  },
  {
    "created": 1464735600000,
    "text": "third",
    "senderId": 1,
    "eventId": null,
    "_links": {

0 个答案:

没有答案