java JAXB“此上下文中也不知道它的任何超类”避免@XmlSeeAlso
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25410831/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JAXB "nor any of its super class is known to this context" avoid @XmlSeeAlso
提问by el n00b
Explanation & Workaround
解释和解决方法
Currently I am using JAX-RS and letting JAXB bindings automatically handle converting the data to XML and JSON for me in a JEE6 project. Everything is working absolutely fantastically until I try to create a generic response object to wrap all of my information in.
目前我正在使用 JAX-RS 并让 JAXB 绑定自动处理在 JEE6 项目中为我将数据转换为 XML 和 JSON。一切都非常出色,直到我尝试创建一个通用响应对象来包装我的所有信息。
When I attempt to use my generic response class com.eln00b.Wrapper
(which contains a private Object result
attribute within it) I get:
当我尝试使用我的通用响应类com.eln00b.Wrapper
(其中包含一个private Object result
属性)时,我得到:
javax.xml.bind.MarshalException - with linked exception: [com.sun.istack.SAXException2: class com.eln00b.CustomObject nor any of its super class is known to this context. javax.xml.bind.JAXBException: class com.eln00b.CustomObject nor any of its super class is known to this context.]
javax.xml.bind.MarshalException - 带有链接异常:[com.sun.istack.SAXException2: class com.eln00b.CustomObject 或其任何超类在此上下文中都是已知的。javax.xml.bind.JAXBException:类 com.eln00b.CustomObject 或其任何超类在此上下文中都是已知的。]
So I add to com.eln00b.Wrapper
:
所以我补充说com.eln00b.Wrapper
:
@XmlSeeAlso ({com.eln00b.CustomObject})
public class Wrapper {
}
Everything works fine.
一切正常。
The Problem
问题
I want this to be extremely generic. I do not want t constantly add classes to the @XmlSeeAlso
annotation on the com.eln00b.Wrapper
class. How do I have the system automatically locate all of my classes for the JAXB context?
我希望这是非常通用的。我不想不断地在类的@XmlSeeAlso
注释中添加com.eln00b.Wrapper
类。 我如何让系统自动为 JAXB 上下文定位我的所有类?
Even if it's a hack where I use something like Reflectionsto load the data, that's fine. I'm just not sure how to get the context to load all of that data without the @XmlSeeAlso
annotation. With the large amount of annotations I will be creating it will just simply not work.
即使我使用Reflections 之类的东西来加载数据是一种黑客行为,也没关系。我只是不确定如何获取上下文以在没有@XmlSeeAlso
注释的情况下加载所有数据。有了大量的注释,我将创建它根本不起作用。
How It Worked Manually
它是如何手动工作的
It worked manually just by adding the data like so doing manual conversions. However, I do notwant to use manual XML/JSON creation unless I absolutely need to (I don't want to deal with content negotiation or anything like that).
它只是通过添加数据来手动工作,就像手动转换一样。不过,我不希望使用手动XML / JSON的创建,除非我绝对需要(我不想处理内容协商或类似的东西)。
Sample:
样本:
JAXBContext.newInstance(new Class[] {Wrapper.class, CustomObject.class});
回答by el n00b
So here is what the essence of the custom resolver looks like:
所以这就是自定义解析器的本质:
@Provider
@Produces ({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
public class JaxbContextResolver implements ContextResolver<JAXBContext> {
@Override
public JAXBContext getContext(Class<?> type) {
// load appropriate context data
Class[] bindTypes = ...
// create
try {
return JAXBContext.newInstance(bindTypes);
} catch (JAXBException e) {
// todo: this can be handled better but works for the example
throw new RuntimeException(e);
}
}
}
Now, the processing for "load appropriate context data" is pretty simple. By basically mimicking @XmlSeeAlso
using runtime data:
现在,“加载适当的上下文数据”的处理非常简单。通过基本上模仿@XmlSeeAlso
使用运行时数据:
- Create a custom something (annotation, processing method, whatever) that marks a particular field/method as "contextual"
- Load the field/method data pulling the data types out
- Make sure you do not load duplicates and check for infinite recursion possibilities
- 创建一个自定义的东西(注释、处理方法等),将特定字段/方法标记为“上下文”
- 加载字段/方法数据拉出数据类型
- 确保不加载重复项并检查无限递归的可能性
Now, I used some caching to help make things more efficient for myself. I also created a slightly more complex setup for my root object where it actually kept track of the class data on its own and made it pretty speedy. I also created an alternative that marked classes as "contextual" that I used package inspection to load via annotations and just automatically add to the context but I have not checked efficiency on that yet. I have some ideas for a 3rd implementation, but I want to get more benchmarking completed.
现在,我使用了一些缓存来帮助自己提高效率。我还为我的根对象创建了一个稍微复杂一些的设置,它实际上自己跟踪类数据并使其速度非常快。我还创建了一个替代方法,将类标记为“上下文”,我使用包检查通过注释加载并自动添加到上下文,但我还没有检查效率。我对第三个实现有一些想法,但我想完成更多的基准测试。