Jersey View with Status

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

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

I wonder how the status code can be returned with this approach. The above implicitly returns 200 , but this is in no way suitable. Is there a way to explicitly state the status code?

+7
java jersey jax-rs
source share
2 answers

You need to return a Response with the correct status code and headers containing your Viewable , for example:

 @GET public Response get() { return Response.status(myCode).entity(new Viewable("/index", "FOO")).build(); } 
+12
source share

Hmm, you can create a custom Response object in a jersey this way: this will return 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 else, use this approach:

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

Hope this helps ....

+5
source share

All Articles