如何设置包以为多个类创建一个解组器?

问题描述

我有一个spring boot应用程序,在其中我使用了以下依赖项将xml解组为java类

        <dependency>
            <groupId>com.fasterxml.jackson.module</groupId>
            <artifactId>jackson-module-jaxb-annotations</artifactId>
            <scope>runtime</scope>
        </dependency>

我有两个需要解组的java类

一个java类

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@XmlRootElement(name = "appAsset")
@XmlAccessorType(FIELD)
public class EmployeeEvent {

    @XmlElement(name = "header")
    private Header header;

    @XmlElement(name = "employeeData")
    private Employee employee;
}

第二个java类

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@XmlRootElement(name = "appAsset")
@XmlAccessorType(FIELD)
public class CustomerEvent {

    @XmlElement(name = "header")
    private Header header;

    @XmlElement(name = "customerData")
    private Customer customer;
}

解组器类如下所示:

@Configuration
public class XmlConfig {

    @Bean
    public Unmarshaller unmarshaller() throws JAXBException {
        var jaxbContext = JAXBContext.newInstance("com.example.dto");
        var unmarshaller = jaxbContext.createUnmarshaller();
        unmarshaller.setAdapter(new OffsetDateTimeAdapter());

        return unmarshaller;
    }
}

以上解组器bean的使用方式如下:

@Component
@Validated
@requiredArgsConstructor
public class EmployeeConverter {

    private final Unmarshaller unmarshaller;

    public @Valid EmployeeEvent toEmployeeEvent(String xml) throws JAXBException,XMLStreamException {
        var xmlStreamReader =
                XMLInputFactory.newFactory().createXMLStreamReader(new ByteArrayInputStream(xml.getBytes()));
        var xmlReaderWithoutNamespace = new XMLReaderWithoutNamespace(xmlStreamReader);

        return (EmployeeEvent) unmarshaller.unmarshal(xmlReaderWithoutNamespace);
    }
}

但是,上述配置给出了如下错误

Error creating bean with name 'employeeConverter': Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'unmarshaller' defined in com.example.config.XmlConfig: Bean instantiation via factory method Failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.xml.bind.Unmarshaller]: Factory method 'unmarshaller' threw exception; nested exception is javax.xml.bind.JAXBException: \"com.example.dto\" doesnt contain ObjectFactory.class or jaxb.index"}

错误发生后,我在com.example.d包中添加了jaxb.in​​dex文件,如下所示。

EmployeeEvent
CustomerEvent

但是,仍然错误与无法找到jaxb.in​​dex文件相同。

如何解决此问题? Spring Boot是否需要任何其他配置?

在阅读了多个stackoverflow帖子之后,我尝试了另一种方法来提供类列表,然后您无需添加jaxb.in​​dex或ObjectFactory.class,如下所示。仍然,我没有得到任何运气,因为以下配置提供了ClassCastException。

java.lang.classCastException: class com.example.dto.CustomerEvent cannot be cast to class com.example.dto.EmployeeEvent (com.example.dto.EmployeeEvent and com.example.dto.CustomerEvent are in unnamed module of loader 'app')

现在,Unmarshaller类如下所示:

@Configuration
public class XmlConfig {

    @Bean
    public Unmarshaller unmarshaller() throws JAXBException {
        var jaxbContext = JAXBContext.newInstance(EmployeeEvent.class,CustomerEvent.class);
        var unmarshaller = jaxbContext.createUnmarshaller();
        unmarshaller.setAdapter(new OffsetDateTimeAdapter());

        return unmarshaller;
    }
}

我觉得我缺少一些关于Spring Boot的配置。有解决此问题的指针吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)