Created
May 19, 2015 18:01
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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