javaresthttp-status-code-200

When performing a POST HTTP method the "Response.status(Status.CREATED)" is recognised as undefined for the type "Response" by eclipse


I'm following this tutorial in order to learn how to code RESTful web services - tutorial. Here we are trying to send the status code 201 Created along with the posting of a new message through Postman. When I write the lines of code he writes at 4:47 I get an error in eclipse oxygen:

      CREATED cannot be resolved or is not a field

I went through the documentation, but I couldn't find the correct way to type it. Tried to import more Response classes as well, but this didn't fix it.

This is the code where the problem occurs:

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

And this is the whole 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();
} }

Solution

  • The Response import should be javax.ws.rs.core.Response and Status is a static inner class of Response. So either import the static Response.Status or just use Response.Status instead of just Status.

    For more information, please refer to the javadoc.