Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.