简体   繁体   English

使用JAX-RS实现子资源时出现问题?

[英]I am having issue while implementing sub-resources using JAX-RS?

I want to implement Subresource using JAX-RS. 我想使用JAX-RS实现Subresource。 The URI of the resource would be like this http://localhost:8080/messenger/webapi/messages/1/comments . 资源的URI就像这样http:// localhost:8080 / messenger / webapi / messages / 1 / comments I am able to get messages using the following URI http://localhost:8080/messenger/webapi/messages/1 but when I try to get the comments for a message I just get empty curly braces. 我可以使用以下URI http:// localhost:8080 / messenger / webapi / messages / 1来获取消息,但是当我尝试获取消息的注释时,我只会得到空的花括号。

Both the resource classes are in the same package. 两种资源类都在同一包中。 I understand that if the mapping of the URI is incorrect I will get a 404 error but I get 200 status code with empty braces. 我了解,如果URI的映射不正确,我会收到404错误,但我会得到200个带有空花括号的状态代码。

    @Path("/messages")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class MessageResource {
    MessageService ms = new MessageService();


    @GET
    @Path("/{messageId}")
    public Message getMessage(@PathParam("messageId") long messageId) {
        return ms.getMessage(messageId);
    }

    @POST
    public Message addMessage(Message message) {
        return ms.addMessage(message);
    }

    @PUT
    @Path("/{messageId}")
    public Message updateMessage(@PathParam("messageId") long messageId, Message message) {
        message.setId(messageId);
        return ms.updateMessage(message);
    }

    @DELETE
    @Path("/{messageId}")
    public void deleteMessage(@PathParam("messageId") long messageId) {
        ms.removeMessage(messageId);
    }

    @GET
    @Path("/{messageId}/comments")
    public CommentResource getComments() {
        return new CommentResource();
    }

}

CommentResource Class Code: CommentResource类代码:

public class CommentResource {

private CommentService commentService = new CommentService();

@GET
public String test() {
    return "new sub resource";
}
}

I have figured out that i am using @GET annotation on the getComments() method which is causing the problem. 我已经发现我在导致问题的getComments()方法上使用@GET批注。 I have removed it and now the code is working fine. 我已经删除了它,现在代码可以正常工作了。

@GET
@Path("/{messageId}/comments")
public CommentResource getComments() {
    return new CommentResource();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM