title: How to write an XML MessageBodyWriter provider with jersey
body: <p>I am not trying to solve any problem in particular, but rather I am on a learning path to jersey.</p>
  
  <p>I have an entity class marked like this:</p>
  
  <pre><code>@Entity
  @Table(name = "myentity")
  @XmlRootElement
  public class MyEntity implements serializable {
    // lots of methods...
  }
  </code></pre>
  
  <p>and the corresponding jersey service</p>
  
  <pre><code>@Stateless
  @Path("entity")
  public class EntityFacade {
  
      @GET
      @Path("{param}")
      @Produces({"application/xml;charset=UTF-8"})
      public List&lt;MyEntity&gt; find(@PathParam("param") String param) {
         List entities = entityManager.getResultList(); // retrieve list from db
         return entities;
      }
  
  }
  </code></pre>
  
  <p>Which give a correct XML response.
  Supposing I want to write a MessageBodyWriter which replicate same behavior, which is producing an XML response, how could I do that ?</p>
  
  <pre><code>@Provider
  public class TrasformerMessageBodyWriter implements MessageBodyWriter&lt;Object&gt; {
  
      @Override
      public long getSize(Object o, Class&lt;?&gt; type, Type genericType,
              Annotation[] annotations, MediaType mediaType) {
          return 0;
      }
  
      @Override
      public boolean isWriteable(Class&lt;?&gt; type, Type genericType,
              Annotation[] annotations, MediaType mediaType) {
          // return true or false depending if I want to rewrite the response
      }
  
      @Override
      public void writeTo(Object o, Class&lt;?&gt; type, Type genericType,
              Annotation[] annotations, MediaType mediaType,
              MultivaluedMap&lt;String, Object&gt; httpHeaders,
              OutputStream entityStream) throws IOException,
              WebApplicationException {
          // what do I need to write here...
      }
  }
  </code></pre>
  
  <p>by marking with @Provider annotation I can see the message body writer is correctly invoked.</p>
  
  <p>When writeTo is invoked, Object o is a Vector and Type genericType is a List but at this point I am completely lost on how I could transform the object in XML.</p>
  
  <p>Last, if everything is already provided by jersey and its annotations, how can a MessageBodyWriter be useful ?</p>
  
  <p>Again, I am repeating that this is just an academic exercise.</p>