Mike Cooper
Mike Cooper

Reputation: 1105

How to use default Jersey JAXB MessageBodyWriter for XML mixed with custom providers?

I can't seem to get my Jersey/Jetty based server to use the default JAXB MessageBodyWriter for XML. I'm getting this error:

MessageBodyWriter not found for media type=application/xml, type=...

I have an Application class which looks like this:

import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;

import cmb.agent.engine.resource.AgentCertificateResource;
import cmb.agent.engine.resource.ProfileResource;
import cmb.agent.engine.servlet.SessionRequestFilter;

@ApplicationPath("/")
public class AgentExample extends Application {
    private Set<Object>                         singletons = new HashSet<Object>();
    private Set<Class<?>>                       perRequestServices = new HashSet<>();

    public AgentExample() {
        perRequestServices.add(ProfileResource.class);
        perRequestServices.add(AgentCertificateResource.class);
        singletons.add(new NotificationXmlMarshaller());
        singletons.add(new SessionRequestFilter());
    }

    @Override
    public Set<Class<?>> getClasses() {
        return perRequestServices;
    }

    @Override
    public Set<Object> getSingletons() {
        return singletons;
    }
}

In the above example NotificationXmlMarshaller is one of my custom marshallers, but the error occurs when trying to marshall any other entity type.

My understanding is that when getClasses() and getSingletons() returns non-empty values, all the defaults are ignored. So I presume that I am disabling the default JAXB xml marshaller.

My question is how can I use my own custom marshallers but use the default JAXB marshaller for everything else?

Here are my gradle dependencies for jersey for this project:

compile("org.glassfish.jersey.containers:jersey-container-servlet:$jerseyGlassfishVersion")
compile("org.glassfish.jersey.media:jersey-media-jaxb:$jerseyGlassfishVersion")
compile("org.glassfish.jersey.core:jersey-client:$jerseyGlassfishVersion")

Jersey version 2.25.

Upvotes: 0

Views: 623

Answers (1)

Mike Cooper
Mike Cooper

Reputation: 1105

I finally figured out I had a major error in my entity class. I forgot the @XmlRootElement annotation. Sigh. :(

Upvotes: 0

Related Questions