当执行 POST HTTP 方法时,"Response.status(Status.CREATED)" 被 eclipse 识别为类型 "Response" 未定义
When performing a POST HTTP method the "Response.status(Status.CREATED)" is recognised as undefined for the type "Response" by eclipse
我学习本教程是为了学习如何编写 RESTful Web 服务代码 - tutorial. Here we are trying to send the status code 201 Created
along with the posting of a new message through Postman。当我写他在 4:47 上写的代码行时,我在 eclipse oxygen 中遇到错误:
CREATED cannot be resolved or is not a field
我浏览了文档,但找不到正确的输入方式。也尝试导入更多 Response classes,但这并没有解决问题。
这是出现问题的代码:
return Response.status(Status.CREATED)
.entity(newMessage)
.build();
这是全部class:
`import java.net.URI;
import java.util.List;
import javax.net.ssl.SSLEngineResult.Status;
import javax.ws.rs.BeanParam;
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.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.UriInfo;
import javax.ws.rs.core.Response.ResponseBuilder;
import org.hristo.javabrains.messenger.model.Message;
import org.hristo.javabrains.messenger.resources.beans.MessageFilterBean;
import org.hristo.javabrains.messenger.service.MessageService;
import com.sun.research.ws.wadl.Response;
@Path("messages")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public class MessageResource {
MessageService messageService = new MessageService();
@GET
public List<Message> getMessages(@BeanParam MessageFilterBean filterBean) {
if(filterBean.getYear() > 0) {
return messageService.getAllMessagesForYear(filterBean.getYear());
}
if(filterBean.getStart() >= 0 && filterBean.getSize() >= 0) {
return messageService.getAllMessagesPaginated(filterBean.getStart(), filterBean.getSize());
}
return messageService.getAllMessages();
}
@POST
public Response addMessage(Message message, @Context UriInfo uriInfo) {
Message newMessage = messageService.addMessage(message);
String newId = String.valueOf(newMessage.getId());
URI uri = uriInfo.getAbsolutePathBuilder().path(newId).build();
return Response.status(Status.CREATED)
.entity(newMessage)
.build();
}
@PUT
@Path("{messageId}")
public Message updateMessage(@PathParam("messageId") long messageId, Message message) {
message.setId(messageId);
return messageService.updateMessage(message);
}
@DELETE
@Path("{messageId}")
public Message removeMessage(@PathParam("messageId") long messageId) {
return messageService.removeMessage(messageId);
}
@GET
@Path("{messageId}")
public Message getMessage(@PathParam("messageId") long messageId) {
return messageService.getMessage(messageId);
}
@Path("{messageId}/comments")
public CommentResource getCommentResource() {
return new CommentResource();
} }
Response
导入应该是 javax.ws.rs.core.Response
而 Status
是 Response
的静态内部 class。所以要么导入静态 Response.Status
要么只使用 Response.Status
而不是 Status
.
更多信息,请参考javadoc。
我学习本教程是为了学习如何编写 RESTful Web 服务代码 - tutorial. Here we are trying to send the status code 201 Created
along with the posting of a new message through Postman。当我写他在 4:47 上写的代码行时,我在 eclipse oxygen 中遇到错误:
CREATED cannot be resolved or is not a field
我浏览了文档,但找不到正确的输入方式。也尝试导入更多 Response classes,但这并没有解决问题。
这是出现问题的代码:
return Response.status(Status.CREATED)
.entity(newMessage)
.build();
这是全部class:
`import java.net.URI;
import java.util.List;
import javax.net.ssl.SSLEngineResult.Status;
import javax.ws.rs.BeanParam;
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.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.UriInfo;
import javax.ws.rs.core.Response.ResponseBuilder;
import org.hristo.javabrains.messenger.model.Message;
import org.hristo.javabrains.messenger.resources.beans.MessageFilterBean;
import org.hristo.javabrains.messenger.service.MessageService;
import com.sun.research.ws.wadl.Response;
@Path("messages")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public class MessageResource {
MessageService messageService = new MessageService();
@GET
public List<Message> getMessages(@BeanParam MessageFilterBean filterBean) {
if(filterBean.getYear() > 0) {
return messageService.getAllMessagesForYear(filterBean.getYear());
}
if(filterBean.getStart() >= 0 && filterBean.getSize() >= 0) {
return messageService.getAllMessagesPaginated(filterBean.getStart(), filterBean.getSize());
}
return messageService.getAllMessages();
}
@POST
public Response addMessage(Message message, @Context UriInfo uriInfo) {
Message newMessage = messageService.addMessage(message);
String newId = String.valueOf(newMessage.getId());
URI uri = uriInfo.getAbsolutePathBuilder().path(newId).build();
return Response.status(Status.CREATED)
.entity(newMessage)
.build();
}
@PUT
@Path("{messageId}")
public Message updateMessage(@PathParam("messageId") long messageId, Message message) {
message.setId(messageId);
return messageService.updateMessage(message);
}
@DELETE
@Path("{messageId}")
public Message removeMessage(@PathParam("messageId") long messageId) {
return messageService.removeMessage(messageId);
}
@GET
@Path("{messageId}")
public Message getMessage(@PathParam("messageId") long messageId) {
return messageService.getMessage(messageId);
}
@Path("{messageId}/comments")
public CommentResource getCommentResource() {
return new CommentResource();
} }
Response
导入应该是 javax.ws.rs.core.Response
而 Status
是 Response
的静态内部 class。所以要么导入静态 Response.Status
要么只使用 Response.Status
而不是 Status
.
更多信息,请参考javadoc。