Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jersey Viewable with status code

The JAX-RS implementation Jersey supports MVC style web applications through the Viewable class, which is a container for a template name and a model object. It is used like this:

@GET
public Viewable get() {
  return new Viewable("/index", "FOO");
}

I wonder how a status code could be returned with this approach. The above would implicitly return 200, but that wouldn't be appropriate in any case. Is there a way to set a status code explicitly?

like image 704
deamon Avatar asked Jul 29 '10 11:07

deamon


2 Answers

You will have to return a Response set up with the correct status code and headers containing your Viewable, e.g.:

@GET
public Response get() {
  return Response.status(myCode).entity(new Viewable("/index", "FOO")).build();
}
like image 199
Moritz Avatar answered Oct 02 '22 20:10

Moritz


Hmm you can create custom Response object in jersey thusly: this will return a 200:

@GET
public Response get() {
    URI uri=new URI("http://nohost/context");
    Viewable viewable=new Viewable("/index", "FOO");
    return Response.ok(viewable).build();
}

to return something different use this approach:

@GET
public Response get() {
    int statusCode=204;
    Viewable myViewable=new Viewable("/index","FOO");
    return Response.status(statusCode).entity(myViewable).build();
}

Hope that helped....

like image 26
fasseg Avatar answered Oct 02 '22 20:10

fasseg