泽西错误地调用子资源

时间:2016-04-24 09:29:39

标签: java rest jersey jax-rs exceptionmapper

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

在我的MessageResource我使用测试方法映射的评论子资源,我在MessageResource课程中评论过,因为我不想调用CommentResource

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错误报告页面,则应发送有意义的消息。

@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

测试我的通用异常映射器时
  

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

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

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

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

如果我评论上述方法,我会在Postman中得到适当的回应。

任何人都可以解释这种行为。提前谢谢。

1 个答案:

答案 0 :(得分:3)

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

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