Skip to content

Instantly share code, notes, and snippets.

@marianogonzalez
Last active August 29, 2015 14:06
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 marianogonzalez/481595e42383aebb1f68 to your computer and use it in GitHub Desktop.
Save marianogonzalez/481595e42383aebb1f68 to your computer and use it in GitHub Desktop.
package com.my.project.jersey.writers;
@Provider
@Produces("application/xml")
public class MyBeanMessageBodyWriter implements MessageBodyWriter<MyBean> {
@Override
public boolean isWriteable(Class<?> type, Type genericType,
Annotation[] annotations, MediaType mediaType) {
return type == Person.class;
}
@Override
public long getSize(MyBean myBean, 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(Person person,
Class<?> type,
Type genericType,
Annotation[] annotations,
MediaType mediaType,
MultivaluedMap<String, Object> httpHeaders,
OutputStream entityStream)
throws IOException, WebApplicationException {
try {
JAXBContext jaxbContext = JAXBContext.newInstance(Person.class);
jaxbContext.createMarshaller().marshal(person, entityStream);
} catch (JAXBException jaxbException) {
throw new ProcessingException(
"Error serializing a Person to the output stream", jaxbException);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment