title: How do manually configure a JAXBContext?
body: <p>There's a lot of examples out there explaining how to turn annotated POJO's (alternatively wrapping your non-annotated POJO's in JAXBElement) into XML with Spring and Jersey. This is all good, but it's also a bit mystical to me how it all happens.</p>
  
  <p>I'd like to learn more about how this, and I'd like to provide Jersey with a JAXB implementation of my own choice. I'd like to use <a href="http://www.eclipse.org/eclipselink/documentation/2.7/moxy/runtime001.htm" rel="nofollow noreferrer">MOXy</a> for this.</p>
  
  <ol>
  <li><p>Do I need to "manually" create a (<a href="http://www.eclipse.org/eclipselink/documentation/2.7/moxy/runtime002.htm" rel="nofollow noreferrer">MOXy</a>) <a href="https://docs.oracle.com/javaee/7/api/javax/xml/bind/JAXBContext.html" rel="nofollow noreferrer">JAXBContext</a> for Jersey to use, and somehow provide Jersey with that <code>JAXBContext</code>. Will I have to do that through a <a href="https://docs.oracle.com/javase/9/docs/api/javax/xml/bind/JAXBContextFactory.html" rel="nofollow noreferrer">JAXBContextFactory</a>?</p></li>
  <li><p>If the above assumption is correct, how do I provide Jersey with that <code>JAXBContext</code>? I've attempted to set up <code>@Provider</code> for <code>ContextResolver&lt;JAXBContext&gt;</code>, but that didn't really work / make a difference. How do I tell Jersey which JAXBContext to use?</p></li>
  </ol>
  
  <p><strong>Spring Configuration</strong></p>
  
  <pre><code>@Provider
  public class JAXBConfiguration implements ContextResolver&lt;JAXBContext&gt; {
  
      private JAXBContext context;
  
      public JAXBConfiguration() throws Exception {
          this.context = JAXBContext.newInstance("package.where.model.lives");
      }
  
      @Override
      public JAXBContext getContext(Class&lt;?&gt; objectType) {
          return context;
      }
  }
  </code></pre>
  
  <p><strong>Jersey Resource</strong></p>
  
  <pre><code>@Component
  @Path("/test")
  public class LabelsResource {
  
      @GET
      @Produces(MediaType.APPLICATION_XML)
      public Response getLabels(@QueryParam("labelIds") String labelIds) {
  
          try {
              DefaultLabel entity = new DefaultLabel();
              entity.setName("name");
              entity.setCode("code");
  
              // Works    
              //return Response.ok(new JAXBElement&lt;DefaultLabel&gt;(new QName("label"), DefaultLabel.class, entity)).build();
  
              // Does not work
              return Response.ok(entity).build();
  
          } catch (Exception e) {
              return Response.serverError().entity(e.getMessage()).build();
          }
  
      }
  </code></pre>