Skip to content

Instantly share code, notes, and snippets.

@zarub2k
Created May 19, 2015 18:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zarub2k/ab3342b9d7d44578f3b9 to your computer and use it in GitHub Desktop.
Save zarub2k/ab3342b9d7d44578f3b9 to your computer and use it in GitHub Desktop.
It is provider class is responsible to Marshall the Java bean into JSON object
@Provider
@Produces(MediaType.APPLICATION_JSON)
public class BookJsonMarshaller implements MessageBodyWriter<Book> {
@Override
public long getSize(Book book, Class<?> clazz, Type type, Annotation[] annotations, MediaType mediaType) {
return -1;
}
@Override
public boolean isWriteable(Class<?> clazz, Type type, Annotation[] annotations, MediaType mediaType) {
return clazz == Book.class;
}
@Override
public void writeTo(Book book, Class<?> clazz, Type type, Annotation[] annotations, MediaType mediaType,
MultivaluedMap<String, Object> valueMap, OutputStream stream) throws IOException, WebApplicationException {
JsonObject jsonObject = Json.createObjectBuilder()
.add("title", book.getTitle())
.add("author", book.getAuthor()).build();
DataOutputStream outputStream = new DataOutputStream(stream);
outputStream.writeBytes(jsonObject.toString());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment