Hammad Ahmad
Hammad Ahmad

Reputation: 69

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

I want to implement Subresource using JAX-RS. The URI of the resource would be like this 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.

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.

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

public class CommentResource {

private CommentService commentService = new CommentService();

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

Upvotes: 0

Views: 106

Answers (1)

Hammad Ahmad
Hammad Ahmad

Reputation: 69

I have figured out that i am using @GET annotation on the getComments() method which is causing the problem. I have removed it and now the code is working fine.

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

Upvotes: 1

Related Questions