Last active
August 29, 2015 14:06
-
-
Save marianogonzalez/481595e42383aebb1f68 to your computer and use it in GitHub Desktop.
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
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