Handling custom error response in JAX-RS 2.0 client library

I believe you want to do something like this: Response response = builder.get( Response.class ); if ( response.getStatusCode() != Response.Status.OK.getStatusCode() ) { System.out.println( response.getStatusType() ); return null; } return response.readEntity( MyEntity.class ); Another thing you can try (since I don’t know where this API puts stuff — i.e. in the header or entity or what) … Read more

In JAX RS, differences between returning Response and Bean or Collection of Beans (DTO)

The differences are explained in the JAX-RS specification: 3.3.3 Return Type Resource methods MAY return void, Response, GenericEntity, or another Java type, these return types are mapped to a response entity body as follows: void Results in an empty entity body with a 204 status code. Response Results in an entity body mapped from the … Read more

How does one intercept a request during the Jersey lifecycle?

I’ve found the answer. First, create a class that implements ContainerRequestFilter. The interface specifies the following method, in which the filtering takes place. The ContainerRequest object contains information about the current request. public ContainerRequest filter(ContainerRequest req); After that, include the following XML in the servlet configuration in web.xml <init-param> <param-name>com.sun.jersey.spi.container.ContainerRequestFilters</param-name> <param-value>path.to.filtering.class</param-value> </init-param> Sources: http://jersey.576304.n2.nabble.com/ContainerRequestFilter-and-Resources-td4419975.html http://markmail.org/message/p7yxygz4wpakqno5

Logging request and response in one place with JAX-RS

You can create filters and easily bind them to the endpoints you need to log, keeping your endpoints lean and focused on the business logic. Defining a name binding annotation To bind filters to your REST endpoints, JAX-RS provides the meta-annotation @NameBinding and it can be used as following: @NameBinding @Retention(RUNTIME) @Target({TYPE, METHOD}) public @interface … Read more

JAX-RS Frameworks [closed]

FWIW we’re using Jersey as its packed full of features (e.g. WADL, implicit views, XML/JSON/Atom support) has a large and vibrant developer community behind it and has great spring integration. If you use JBoss/SEAM you might find RESTeasy integrates a little better – but if you use Spring for Dependency Injection then Jersey seems the … Read more

Servlet vs RESTful

RESTful is more an architecture style than a different technology. In server perspective, it is designed to be entirely stateless and self-contained on a per-request basis (i.e. there are no sessions). In client perspective, it’s more a way of getting information in different formats via URLs with (self-documenting) path parameters instead of request parameters. Surely … Read more

How to set response header in JAX-RS so that user sees download popup for Excel?

You don’t need HttpServletResponse to set a header on the response. You can do it using javax.ws.rs.core.Response. Just make your method to return Response instead of entity: return Response.ok(entity).header(“Content-Disposition”, “attachment; filename=\”” + fileName + “\””).build() If you still want to use HttpServletResponse you can get it either injected to one of the class fields, or … Read more