泽西岛的系统范围适配器?

发布于 2024-09-09 01:39:23 字数 260 浏览 8 评论 0 原文

我正在尝试为 Jersey 中的 java.util.Locale 类型配置“系统范围”自定义 javax.xml.bind.annotation.adapters.XmlAdapter 。在我控制的类上使用 @XmlJavaTypeAdapter 很容易,但情况并非总是如此(我无法注释的第三方代码)。

这似乎是一个非常常见的问题,但我找不到任何好的例子或文档来说明如何处理它。

那么,有可能吗?

谢谢!

I'm trying configure a "system wide" custom javax.xml.bind.annotation.adapters.XmlAdapter for the java.util.Locale type in Jersey. It's easy enough to use @XmlJavaTypeAdapter on classes I control but that's not always the case (3rd party code that I can't annotate).

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

So, is it possible?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

木格 2024-09-16 01:39:23

我可以看到三个可能的选项:

  1. 使用 setAdapter()。您可以拥有一个静态构建器函数,它将所有“系统级”类型适配器添加到您在应用程序中使用的所有编组器。这完全取决于您对“系统级别”的定义
  2. 使用委托
  3. 做一些奇特的字节码欺骗< /a> 将注释添加到现有的类文件中。

我的建议是使用方法 1,该方法简单明了。

I can see three possible options:

  1. Register the converter with the marshaller with setAdapter(). You can have a static builder function which adds all your 'system level' type adapters to all marshallers which you use in your application. It all depends on your definition of 'system level'
  2. Use a delegate
  3. Do some fancy bytecode trickery to add the annotations to existing class files.

My advice would be to use approach 1, which is simple and straightforward.

心凉 2024-09-16 01:39:23

如果您需要注释无法修改的类,则始终可以使用 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

If you need to annotate classes you can't modify, you could always use the externalized metadata feature of EclipseLink JAXB (MOXy).

The metadata file would look something like:

<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>

To you EclipseLink MOXy you need to add a jaxb.properties file in with your model classes with the following entry:

javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory
救星 2024-09-16 01:39:23

您可能还想查看用于类似目的的 JAXBIntroductions 项目。注释配置保存在文件中,无需修改源代码。通过实现 JAX-RS 提供程序,它确实可以与 Jersey 很好地配合。您可以查看我的博客条目 它通过一个例子详细解释了这一点。下面是一个简单的 JAXBContextResolver,为 JAXBIntroductions 提供,可以在 Jersey 应用程序中使用。

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;
    }
}

You may also want to look at JAXBIntroductions project which is intended for similar purpose. The annotation configuration is kept in a file, without requiring modification to source code. It does work nicely with Jersey, by implementing a JAX-RS provider. You can check out my blog entry which explains this in detail with an example. Here is a simple JAXBContextResolver provide for JAXBIntroductions that can be used in your Jersey application.

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;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文