title: Jersey + Moxy + JAXB - how to marshal XML without annotations
body: <p>In <a href="http://blog.bdoughan.com/2013/06/moxy-is-new-default-json-binding.html" rel="nofollow">http://blog.bdoughan.com/2013/06/moxy-is-new-default-json-binding.html</a> about halfway down there's a heading "Customizing the JSON-Binding".  How do you similarly customize the XML binding?  </p>
  
  <p>There seem to be fundamental differences between the way Jersey handles Moxy JSON binding and the XML equivalent.  If I follow the instructions in the Jersey documentation for creating a custom JAXBContext resolver  to configure Moxy's mapping file, that resolver fires in the JSON case but not in the XML case.  See <a href="https://bitbucket.org/jmetcher/resttest/" rel="nofollow">https://bitbucket.org/jmetcher/resttest/</a> for a very small project demonstrating this.</p>
  
  <p>I have the correct jaxb.properties file in place, and I'm building with the jersey-media-moxy module.  I can get the identical Moxy JAXB setup working in standalone mode, I just can't get Jersey to take any notice of it.</p>
  
  <p>The debugging I've done indicates that the only way to get this to work is to create a custom MessageBodyWriter.  The jersey-media-moxy module registers a JSON MessageBodyWriter which will then invoke any registered ContextResolvers.  It does not register an equivalent MessageBodyWriter for XML, and Jersey's default MBW's seem to completely ignore registered ContextResolvers.</p>
  
  <p>Howver, the fact that the need to create a custom MessageBodyWriter is completely missing from the docs and nearly every example seems to indicate that I'm missing something fundamental.</p>
  
  <p>I'd provide links to more info but I don't seem to have the rep to do much at all on SO.</p>
  
  <p>EDIT: More info in response to comments:</p>
  
  <p>Moxy is certainly being picked up as the JAXB provider.  I can see this in the debugger.</p>
  
  <p>Jersey version is 2.9, EclipseLink version is 2.5.  Java 7.</p>
  
  <p>The ContextResolver approach works fine for JSON.  So does the approach of registering MoxyXMLFeature in an Application class.  In both cases, the correct context configured with the right mapping file is used.  In the XML case, it seems that Jersey never even attempts to create a context.</p>
  
  <p>IF, however, I add an @XmlRootElement annotation to the class I'm trying to marshal, it all works.  Even with no other annotations, the class can be marshaled to both XML and JSON according to the oxm mapping file.  I've updated the bitbucket project to show this.  </p>
  
  <p>So, I can work around the issue either by creating a custom MBW or providing an annotated root class.  I guess at this point I'm to work out what the best or recommended approach would be so I can log a documentation patch.</p>