Jersey: Print the actual request

If you’re just using Jersey Client API, LoggingFilter (client filter) should help you: Client client = Client.create(); client.addFilter(new LoggingFilter(System.out)); WebResource webResource = client.resource(“http://localhost:9998/”); ClientResponse response = webResource.accept(MediaType.APPLICATION_JSON) .get(ClientResponse.class); Otherwise, you can again log both request and response on server using other LoggingFilter (container filter).

Jackson + Builder Pattern?

As long as you are using Jackson 2+, then there is now built in support for this. First you need to add this annotation to your Address class: @JsonDeserialize(builder = Address.Builder.class) Then you need to add this annotation to your Builder class: @JsonPOJOBuilder(buildMethodName = “create”, withPrefix = “set”) You can skip this second annotation if … Read more

Dependency injection with Jersey 2.0

You need to define an AbstractBinder and register it in your JAX-RS application. The binder specifies how the dependency injection should create your classes. public class MyApplicationBinder extends AbstractBinder { @Override protected void configure() { bind(MyService.class).to(MyService.class); } } When @Inject is detected on a parameter or field of type MyService.class it is instantiated using the … Read more

Input and Output binary streams using JERSEY?

I managed to get a ZIP file or a PDF file by extending the StreamingOutput object. Here is some sample code: @Path(“PDF-file.pdf/”) @GET @Produces({“application/pdf”}) public StreamingOutput getPDF() throws Exception { return new StreamingOutput() { public void write(OutputStream output) throws IOException, WebApplicationException { try { PDFGenerator generator = new PDFGenerator(getEntity()); generator.generatePDF(output); } catch (Exception e) { … Read more

what’s the correct way to send a file from REST web service to client?

I don’t recommend encoding binary data in base64 and wrapping it in JSON. It will just needlessly increase the size of the response and slow things down. Simply serve your file data using GET and application/octect-streamusing one of the factory methods of javax.ws.rs.core.Response (part of the JAX-RS API, so you’re not locked into Jersey): @GET … Read more

RESTful on Play! framework

As per request, a simple REST-like approach. It works almost the same way Codemwncis’ solution works but uses the Accept header for content negotiation. First the routes file: GET /user/{id} Application.user POST /user/ Application.createUser PUT /user/{id} Application.updateUser DELETE /user/{id} Application.deleteUser You don’t specify any content type here. Doing so is IMHO only necessary when you … Read more

How to access parameters in a RESTful POST method

Your @POST method should be accepting a JSON object instead of a string. Jersey uses JAXB to support marshaling and unmarshaling JSON objects (see the jersey docs for details). Create a class like: @XmlRootElement public class MyJaxBean { @XmlElement public String param1; @XmlElement public String param2; } Then your @POST method would look like the … Read more

What does Provider in JAX-RS mean?

Providers are a simply a way of extending and customizing the JAX-RS runtime. You can think of them as plugins that (potentially) alter the behavior of the runtime, in order to accomplish a set of (program defined) goals. Providers are not the same as resources classes, they exist, conceptually, at a level in-between resources classes … Read more