在没有@Path的情况下处理子资源

时间:2019-04-12 01:40:05

标签: java web-services jax-rs

我有一个Jersey Web服务,它将处理多个子资源,并且我想在没有@Path注释的情况下处理它们。

我正在使用jersey 2.16,java和tomcat 9服务器。如果省略子资源中的@Path注释,则会收到错误消息:

java.lang.IllegalArgumentException: The class, class org.rest.project.messenger.resources.CommentResource is not annotated with @Path.

根:

@Path("/messages")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(value = {MediaType.APPLICATION_JSON, MediaType.TEXT_XML})
public class MessageResource {
.
.
.
        @Path("/{messageId}/comments/")
    public CommentResource getCommentResource() {
        return new CommentResource();
    }

    @Path("/{messageId}/shares/")
    public ShareResource getShareResource() {
        return new ShareResource();
    }

    @Path("/{messageId}/likes/")
    public LikeResource getLikeResource() {
        return new LikeResource();
    }
.
.
.
}

子资源1

public class CommentResource {
    .
        .
        .
    @GET
    public List<Comment> getAllComments(@PathParam("messageId") long messageId) {
        return commentService.getAllComments(messageId);
    }

    @POST
    public Comment addComment(@PathParam("messageId") long messageId, Comment comment) {
        return commentService.addComment(messageId, comment);
    }
        .
        .
        .
}

子资源2

public class ShareResource {
    .
        .
        .
    @GET
    public List<Share> getAllShares(@PathParam("messageId") long messageId) {
        return shareService.getAllShares(messageId);
    }

    @POST
    public Share addShare(@PathParam("messageId") long messageId, Share share) {
        return shareService.addShare(messageId, share);
    }
    .
        .
        .
}

子资源3

public class LikeResource {
    .
        .
        .

    @GET
    public List<Like> getAllLikes(@PathParam("messageId") long messageId) {
        return likeService.getAllLikes(messageId);
    }

    @POST
    public Like addLike(@PathParam("messageId") long messageId, Like like) {
        return likeService.addLike(messageId, like);
    }
    .
        .
        .
}

我已阅读多个资源,并且它们声明子资源不需要@Path注释。

另外,如果我使用默认的@Path(“”),则会出现以下错误:

WARNING: A resource model has ambiguous (sub-)resource method for HTTP method GET and input mime-types as defined by"@Consumes" and "@Produces" annotations at Java methods public java.util.List org.rest.project.messenger.resources.CommentResource.getAllComments(long) and public java.util.List org.rest.project.messenger.resources.LikeResource.getAllLikes(long) at matching regular expression /. These two methods produces and consumes exactly the same mime-types and therefore their invocation as a resource methods will always fail.
WARNING: A resource model has ambiguous (sub-)resource method for HTTP method GET and input mime-types as defined by"@Consumes" and "@Produces" annotations at Java methods public java.util.List org.rest.project.messenger.resources.CommentResource.getAllComments(long) and public java.util.List org.rest.project.messenger.resources.ShareResource.getAllShares(long) at matching regular expression /. These two methods produces and consumes exactly the same mime-types and therefore their invocation as a resource methods will always fail.
WARNING: A resource model has ambiguous (sub-)resource method for HTTP method POST and input mime-types as defined by"@Consumes" and "@Produces" annotations at Java methods public org.rest.project.messenger.model.Comment org.rest.project.messenger.resources.CommentResource.addComment(long,org.rest.project.messenger.model.Comment) and public org.rest.project.messenger.model.Like org.rest.project.messenger.resources.LikeResource.addLike(long,org.rest.project.messenger.model.Like) at matching regular expression /. These two methods produces and consumes exactly the same mime-types and therefore their invocation as a resource methods will always fail.
WARNING: A resource model has ambiguous (sub-)resource method for HTTP method POST and input mime-types as defined by"@Consumes" and "@Produces" annotations at Java methods public org.rest.project.messenger.model.Comment org.rest.project.messenger.resources.CommentResource.addComment(long,org.rest.project.messenger.model.Comment) and public org.rest.project.messenger.model.Share org.rest.project.messenger.resources.ShareResource.addShare(long,org.rest.project.messenger.model.Share) at matching regular expression /. These two methods produces and consumes exactly the same mime-types and therefore their invocation as a resource methods will always fail.
.
.
.

0 个答案:

没有答案