1

我正在学习本教程以学习如何编写 RESTful Web 服务 -教程。在这里,我们尝试通过Postman201 Created发送状态代码以及发布新消息。当我编写他在 4:47 编写的代码行时,我在 Eclipse 氧气中遇到错误:

      CREATED cannot be resolved or is not a field

我浏览了文档,但找不到正确的输入方法。也尝试导入更多响应类,但这并没有解决它。

这是出现问题的代码:

    return Response.status(Status.CREATED)
            .entity(newMessage)
            .build();

这是整个班级:

    `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();
} }
4

1 回答 1

0

导入Response应该是javax.ws.rs.core.Response并且Status是. Response所以要么导入静态的Response.Status,要么只使用Response.Status而不是仅仅使用Status.

有关详细信息,请参阅javadoc

于 2018-07-06T17:47:34.357 回答