java webservice response object vs Produces

Viewed 7092

Im trying to java webservices and trying to follow couple of tutorial examples.

In one of the example, I see @Produces annotation being used to specify the type of response that is being returned.

Example:

@GET
@Produces(MediaType.TEXT_PLAIN)
public String sayPlainTextHello() {
    return "Hello Jersey";
}

but in another case, I see the Response object is being used as a Response...

Example:

@GET
@Path("/{param}")
public Response getMsg(@PathParam("param") String msg) {

    String output = "Jersey say : " + msg;

    return Response.status(200).entity(output).build();

}

Question:

  1. Which is the correct way to send a response back - Response object or @Produces annotation?
  2. When both scenarios can be used?
1 Answers
Related