System adapter in jersey?

I am trying to set up a "system" custom javax.xml.bind.annotation.adapters.XmlAdapterfor a type java.util.Localein Jersey. It is easy to use @XmlJavaTypeAdapterfor classes that I control, but this is not always the case (third-party code that I cannot comment on).

It seems like this will be a fairly common problem, but I can't find any good examples or doco on how to handle it.

So is this possible?

Thank!

+3
source share
3 answers

I see three possible options:

1, .

+1

, , EclipseLink JAXB (MOXy).

:

<xml-bindings xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm">
    <java-types>
        <java-type name="java.util.Locale">
            <xml-java-type-adapter value="some.package.YourAdapter"/>
        </java-type>
    </java-types>
</xml-bindings>

EclipseLink MOXy jaxb.properties :

javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory
+1

JAXBIntroductions , . . , JAX-RS. , . JAXBContextResolver JAXBIntroductions, .

import com.sun.xml.bind.api.JAXBRIContext;
import org.jboss.jaxb.intros.IntroductionsAnnotationReader;
import org.jboss.jaxb.intros.IntroductionsConfigParser;
import org.jboss.jaxb.intros.configmodel.JaxbIntros;

import javax.ws.rs.ext.ContextResolver;
import javax.ws.rs.ext.Provider;
import javax.xml.bind.JAXBContext;
import java.util.*;

@Provider
public class JAXBContextResolverForJAXBIntroductions implements ContextResolver<JAXBContext> {

    private final JAXBContext context;
    private final Set<Class> types;
    private final Class[] cTypes = {Customer.class};

    public JAXBContextResolverForJAXBIntroductions() throws Exception {
        this.types = new HashSet(Arrays.asList(cTypes));
        JaxbIntros config = IntroductionsConfigParser.parseConfig(this.getClass().getResourceAsStream("/intro-config.xml"));
        IntroductionsAnnotationReader reader = new IntroductionsAnnotationReader(config);
        Map<String, Object> jaxbConfig = new HashMap<String, Object>();
        jaxbConfig.put(JAXBRIContext.ANNOTATION_READER, reader);
        this.context = JAXBContext.newInstance(cTypes, jaxbConfig);
    }

    public JAXBContext getContext(Class<?> objectType) {
        return (types.contains(objectType)) ? context : null;
    }
}
0

Source: https://habr.com/ru/post/1753978/


All Articles