我正在使用 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 来测试我的通用异常映射器时
它没有映射到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 中得到适当的回应。
任何人都可以解释这种行为。提前致谢。