2016-04-19 1 views
0

Я пытаюсь прочитать параметр корневого пути ресурса в файле вспомогательных ресурсов, но я получаю сообщение об ошибке. Пожалуйста помогите.REST JAX RS: Jersey: Как читать pathparam корневого ресурса в дополнительном ресурсе?

Путь, я следующее:

Root Service ресурс:

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

дополнительный код ресурса:

@Path("/") 
public class CommentResource { 

    private CommentDAOImpl commentDaoObject = new CommentDAOImpl(); 

    @GET 
    @Produces(MediaType.APPLICATION_JSON) 
    public ArrayList<Comment> getAllCommentsForAMessage(@PathParam("messageId") long messageId){ 
     return commentDaoObject.getAllCommentsForMessage(messageId); 
    } 

    @Path("/{commentId}") 
    @GET 
    @Produces(MediaType.APPLICATION_JSON) 
    public Comment getCommentForAMessage(@PathParam("commentId") long commentId, @PathParam("messageId") long messageId){ 
     return commentDaoObject.getCommentForMessage(messageId, commentId); 
    } 
} 

При чтении "MESSAGEID" путь из параметров в подэкранном ресурса я являюсь получение ошибки:

Error: @PathParam value 'messageId' does not match any @Path annotation template parameters of the java method 'getCommentForAMessage' and its enclosing java type 'org.ramesh.jrs.Messenger.resources.CommentResource'.

Может ли кто-нибудь помочь мне решить проблему?

ответ

2

Если вы хотите передать параметр классу ресурсов, вы должны использовать ResourceContext.initResource method.

Это, как изменить код:

служба Корневой ресурс

@Path("/{messageId}/comments") 
public CommentResource getCommentResources(@PathParam("messageId") long messageId, @Context ResourceContext resourceContext){ 
    return resourceContext.initResource(new CommentResource(messageId)); 
} 

Суб код ресурса:

public class CommentResource { 

    private CommentDAOImpl commentDaoObject = new CommentDAOImpl(); 
    private long messageId; 

    public CommentResource(long messageId) { 
     this.messageId = messageId; 
    } 

    @GET 
    @Produces(MediaType.APPLICATION_JSON) 
    public ArrayList<Comment> getAllCommentsForAMessage(){ 
     return commentDaoObject.getAllCommentsForMessage(messageId); 
    } 

    @GET 
    @Path("/{commentId}") 
    @Produces(MediaType.APPLICATION_JSON) 
    public Comment getCommentForAMessage(@PathParam("commentId") long commentId){ 
     return commentDaoObject.getCommentForMessage(messageId, commentId); 
    } 

} 
Смежные вопросы