Jersey错误地调用子资源 [英] Jersey invoking sub resource wrongly

查看:134
本文介绍了Jersey错误地调用子资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用JAX-RS学习RESTful Web服务. 我有一个MessageResource和一个子资源CommentResource.

I am learning RESTful web services using JAX-RS. I have a MessageResource and a sub resource CommentResource.

在我的MessageResource中,我将注释子资源与测试方法映射在一起,因为我不想调用CommentResource,所以我在MessageResource类中进行了注释.

In my MessageResource I mapped comment sub resource with test method, which I commented in the MessageResource class because I don't want CommentResource to be invoked.

package org.kaushik.javabrains.messenger.resource;

import java.net.URI;
import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import javax.ws.rs.core.UriBuilder;
import javax.ws.rs.core.UriInfo;

import org.glassfish.jersey.server.Uri;
import org.kaushik.javabrains.messenger.exception.DataNotFoundException;
import org.kaushik.javabrains.messenger.model.Message;
import org.kaushik.javabrains.messenger.services.MessageService;

@Path("/messages")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public class MessageResource {

    public MessageResource() {
        // TODO Auto-generated constructor stub
    }

    @GET
    public List<Message> getAllMessage() {

        MessageService obj = new MessageService();
        return obj.getAllMessages();
    }

    @GET
    @Path("/{messageId}")
    public Message getMessageById(@PathParam("messageId") String messageId) {

        MessageService obj = new MessageService();
        Message retrivedMessage = obj.getMessage(Long.parseLong(messageId));
        return retrivedMessage;
    }

    /*
     * This method is used to add new message in database returns created
     * message with generated id
     */
    /*
     * @POST public Message postMessage(Message msg){
     * 
     * MessageService obj = new MessageService(); msg = obj.addMessage(msg);
     * 
     * return msg; }
     */

    /**
     * This method created the new message and sends new message with 201 status
     * code using Response
     * 
     * @param msg
     * @return
     */
    @POST
    public Response postMessage(Message msg, @Context UriInfo uriInfo) {

        MessageService obj = new MessageService();
        msg = obj.addMessage(msg);
        // This is to send only created status
        // return Response.status(Status.CREATED).entity(msg).build();

        // If we want to send status + new created resource path

        String newId = msg.getId() + "";
        URI uri = uriInfo.getAbsolutePathBuilder().path(newId).build();
        return Response.created(uri).entity(msg).build();
    }

    @PUT
    @Path("/{messageId}")
    public Message updateMessage(Message msg, @PathParam("messageId") long id) {

        MessageService obj = new MessageService();
        msg.setId(id);
        msg = obj.updateMessage(msg);

        return msg;
    }

    @DELETE
    @Path("/{messageId}")
    public void deleteMessage(@PathParam("messageId") long id) {

        MessageService obj = new MessageService();
        obj.removeMessage(id);
    }

    /**
     * This method is invoked if url asked for comments irrespective of http
     * method, it will invoke the CommentResource which is a sub resouce of
     * Message
     * 
     * @return
     */
    /*
     * @Path("/{messageId}/comments") public CommentResource test() { return new
     * CommentResource(); }
     */
}

我有以下子资源CommentResource:

package org.kaushik.javabrains.messenger.resource;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;

@Path("/")
public class CommentResource {

    public CommentResource() {
        // TODO Auto-generated constructor stub
    }

    @GET
    public String test() {

        return "new sub resource";
    }

    @GET
    @Path("/{commentId}")
    public String getCommentById(@PathParam("messageId") String messageId, @PathParam("commentId") String commentId) {

        return commentId + " This is dummy comment for messageID=" + messageId;
    }

}

我创建了一个通用的异常映射器,该映射器被映射到Throwable类. 为此创建该文件是为了在服务器端发生任何异常时,而不是显示Tomcat错误报告页面时,应该发送有意义的消息.

I created a generic exception mapper, which is mapped to Throwable class. It is created for the purpose, if any exception happens at server end, rather displaying Tomcat error report page, meaningful message should be sent.

@Provider
public class GenericExceptionMapper implements ExceptionMapper<Throwable> {
    @Override
    public Response toResponse(Throwable ex) {
        System.out.println("in Genereic mapper");
        ErrorMessage message = new ErrorMessage(ex.getMessage(),"/Messenger/documentation",500);
        return Response.status(Status.INTERNAL_SERVER_ERROR).entity(message).build();
    }
}

此外,当我通过调用url测试通用异常映射器时

Further, when I am testing my generic exception mapper by invoking a url

http://localhost:8080/messenger/webapi/messa

,未映射到MessageResource中的任何方法.它正在调用CommentResource以下方法:

which is not mapped to any method in MessageResource. It is invoking the CommentResource following method:

@GET
@Path("/{commentId}")
public String getCommentById(@PathParam("messageId") String messageId, @PathParam("commentId") String commentId) {

    return commentId + " This is dummy comment for messageID=" + messageId;
}

如果我评论以上方法,我将在邮递员中得到适当的答复.

If I comment the above method I am getting proper response in Postman.

任何人都可以解释这种行为.预先感谢.

Can anybody explain this behavior. Thanks in advance.

推荐答案

CommentResource删除@Path("/").它被添加为根资源.因此,当您访问/mesa时,CommentResource会映射到/,并且会命中映射到@Path("{whatever}")的资源方法.

Remove @Path("/") from the CommentResource. It is being added as a root resource. So when you access /mesa, the CommentResource is mapped to /, and it hits the resource method mapped to @Path("{whatever}").

子资源定位器类不应具有@Path.这仅适用于根资源类.在子资源定位器请求中,它将被忽略,但是在应用启动时,它仍像常规根资源一样注册.

Sub resource locator classes should not have @Path. That is only for root resource classes. In a sub-resource locator request, it will be ignored, but on app startup, it is still registered like a regular root resource.

这篇关于Jersey错误地调用子资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆