Creating Custom JAX-RS MessageBodyWriter
A JAX-RS MessageBodyWriter
is responsible for converting Java types to a stream. In this example we are creating a JAX-RS MessageBodyWriter
that produces text/html. This class may be annotated using the @Produces
annotation to restrict the media types for which it will be considered suitable. To register your own MessageBodyWriter
you can either register it programatically in the Application
or annotate it with the @Provider
annotation to be automatically discovered by the JAX-RS runtime.
JAX-RS MessageBodyWriter
This resource produces text/html and returns a simple instance of the User
class.
package com.memorynotfound.jaxrs;
import javax.ws.rs.*;
import javax.ws.rs.core.Response;
@Path("/users")
public class UserResource {
@GET
@Path("{id}")
@Produces("text/html")
public Response getUser(@PathParam("id") int id){
User user = new User(id, "name");
return Response
.ok()
.entity(user)
.build();
}
}
Here is the representation of the User
class.
package com.memorynotfound.jaxrs;
public class User {
private int id;
private String name;
public User(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
}
We create a custom message body writer by implementing the MessageBodyWriter
interface and override the appropriate methods. By annotating it with @Provider
annotation, JAX-RS will automatically discover this bean during the runtime scanning. We add the @Produces
annotation to restrict the media types for which it will be considered suitable. Finally we implement the writeTo()
method which is responsible for converting the Java types to a stream output.
package com.memorynotfound.jaxrs;
import javax.ws.rs.Produces;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.ext.MessageBodyWriter;
import javax.ws.rs.ext.Provider;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.Writer;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
@Provider
@Produces("text/html")
public class UserMessageBodyWriter implements MessageBodyWriter<User> {
@Override
public boolean isWriteable(Class<?> type, Type genericType,
Annotation[] annotations, MediaType mediaType) {
return type == User.class;
}
@Override
public long getSize(User user, Class<?> type, Type genericType,
Annotation[] annotations, MediaType mediaType) {
// deprecated by JAX-RS 2.0 and ignored by Jersey runtime
return 0;
}
@Override
public void writeTo(User user, Class<?> type, Type genericType, Annotation[] annotations,
MediaType mediaType, MultivaluedMap<String, Object> httpHeaders,
OutputStream out) throws IOException, WebApplicationException {
Writer writer = new PrintWriter(out);
writer.write("<html>");
writer.write("<body>");
writer.write("<h2>JAX-RS Message Body Writer Example</h2>");
writer.write("<p>Id: " + user.getId() + "</p>");
writer.write("<p>Name: " + user.getName() + "</p>");
writer.write("</body>");
writer.write("</html>");
writer.flush();
writer.close();
}
}
Demo
URL: http://localhost:8081/jaxrs-message-body-writer/api/users/32