gpt4 book ai didi

java - 实现 JAXBContext 解析器类 : What classes are stored in the list?

转载 作者:行者123 更新时间:2023-11-30 07:19:30 25 4
gpt4 key购买 nike

我正在尝试实现 JAXBContext 解析器类。我不明白的是cTypes中包含哪些类型的类?我见过几个类似的例子,但没有一个解释 cTypes 列表是什么。

@Provider
public class JaxbContextResolver implements ContextResolver<JAXBContext> {

private final JAXBContext context;
private final Set<Class<?>> types;
private final Class<?>[] cTypes = {Flights.class, FlightType.class, AircraftType.class};

public JaxbContextResolver() throws Exception {
this.types = new HashSet<Class<?>>(Arrays.asList(cTypes));
this.context = new JettisonJaxbContext(JettisonConfig.DEFAULT, cTypes);
}

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

最佳答案

您的 cTypes 类列表必须包含表示根 xml 元素的所有类(您使用 @XmlRootElement 注释的类)。

说明:

当您创建 JAXBContext 时,您为其提供要绑定(bind)的类列表:

this.context = new JettisonJaxbContext(JettisonConfig.DEFAULT, cTypes);

这将使 JAXB 能够实际将元素与类关联起来。JAXB 将自动绑定(bind)根元素类中包含的任何类(@XmlTransient 注释的类除外)。

然后,当使用 getContext 方法时,仅当参数类已被 JAXB 绑定(bind)到所述上下文时,它才会返回类中初始化的上下文,因为您的 JAXBContext 将无法使用任何以下类:未绑定(bind)。

关于java - 实现 JAXBContext 解析器类 : What classes are stored in the list?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37818122/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com