java - 如何在 JAX-RS 中为子资源中的子资源设计路径?

标签 java web-services rest jersey jax-rs

我是一个完全的初学者,正在学习构建 RESTful 网络服务。我想知道如何在 JAX-RS 的子资源中设置子资源的路径。

我有三个资源:个人资料、消息和评论。 我希望我的 URL 如下所示。

个人资料

 /profiles

对于消息

/profiles/{profileName}/messages

征求意见

/profiles/{profileName}/messages/{messageId}/comments

我的资源路径如下。

Profile Resource

@Path("/profiles")
public class ProfileResource {

    @Path("/{profileName}/messages")
    public MessageResource getMessageResource() {
        return new MessageResource();
    }

}

Message Resource

@Path("/")
public class MessageResource {
    @Path("/{messageId}/comments")
    public CommentResource getCommentResource() {
        return new CommentResource();
    }

    @POST
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public Message addMessage(@PathParam("profileName") String profileName, Message message){
        return messageService.addMessage(profileName, message);
    }   
}

Comment Resource

@Path("/")
public class CommentResource {

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public Comment postComment(@PathParam("messageId") long messageId, Comment comment) {
         return commentService.addComment(messageId, comment);
    }

}

但是我得到以下错误,

SEVERE: Servlet [Jersey Web Application] in web application [/messenger] threw 
load() exception org.glassfish.jersey.server.model.ModelValidationException: 
Validation of the  application resource model has failed during application 
initialization.
[[FATAL] 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 sokkalingam.restapi.messenger.model.Message 
sokkalingam.restapi.messenger.resources.MessageResource.addMessage(java.lang.Strin
 g,sokkalingam.restapi.messenger.model.Message) and public 
sokkalingam.restapi.messenger.model.Comment 
sokkalingam.restapi.messenger.resources.CommentResource.postComment(long,sokkaling
 am.restapi.messenger.model.Comment) 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.;

问题:

  1. 我应该如何为我的子资源设置我的路径?

  2. 在子资源中做子资源的更好方法是什么?是吗 子资源内做子资源有什么共同之处?

最佳答案

How should I set my paths for my sub resources?

去掉子资源类上的@Path。当类用路径注释时,它被作为根资源添加到 Jersey 应用程序。所以你有一堆资源映射到 /,这是错误的,因为有多个 @POST(具有相同的 @Consumes@Produces) 映射到同一路径

对于子资源类,您不需要@Path。就子资源路径而言,它将被忽略。

What is a better way to do sub resource within sub resource? Is it common to do sub-resource within sub-resource?

我看不出你在做什么有什么问题。

关于java - 如何在 JAX-RS 中为子资源中的子资源设计路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35906999/

相关文章:

java - 条件事件处理程序

Java 套接字应用程序在尝试发送文件时发送额外的字节

ios - 构建 iOS 网络应用程序(REST 客户端)的最佳架构方法

c# - 使用 NLog 通过 WebService 记录自定义类

java - Vaadin BeanFieldGroup - 绑定(bind)嵌套属性时为 "Property is not cascaded"

java - 如何将 <ObjectType> 编码到新类中

java - 如何构建耗时的 Web 服务

json - 需要管理测试 RESTful Web 服务的测试人员的最佳实践是什么?

c# - WCF DataContracts - 如何将单个 DataContract 与复杂对象一起用于 WCF SOAP 和 REST 服务?

javascript - REST路由多个文件 Node js