You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot 3/Spring Batch 5:StaxEventItemWriterBuilder设置Marshaller报错

Spring Boot 3 + Spring Batch 5 中 StaxEventItemWriterBuilder Marshaller 错误解决

问题描述

在Spring Boot 3(搭配Spring Batch 5)项目的批处理配置类中,给StaxEventItemWriterBuilder设置Marshaller时,Eclipse提示错误:

The method marshaller(Marshaller) from the type StaxEventItemWriterBuilder refers to the missing type Marshaller

相关代码:

@Bean
public StaxEventItemWriter<EmployeeReport> writerTERXFCDJ(DataSource dataSource, JAXBContext jaxbContext) {
    jaxbContext = JAXBContext.newInstance(EmployeeReport.class);//class names
    Marshaller jaxbMarshaller = jaxbContext.createMarshaller();

    return new StaxEventItemWriterBuilder<EmployeeReport>()
            .name("employeeReportItemWriter")
            .version("1.0")
            .rootTagName("EmployeeReportList")
            .resource(new FileSystemResource("a.xml"))
            .marshaller(jaxbMarshaller)// 此处报错
            .build();
}

项目pom.xml相关依赖:

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-batch</artifactId>
</dependency>

<dependency>
      <groupId>jakarta.xml.bind</groupId>
      <artifactId>jakarta.xml.bind-api</artifactId>
</dependency>

<!-- JAXB Implementation -->
<dependency>
        <groupId>org.glassfish.jaxb</groupId>
        <artifactId>jaxb-runtime</artifactId>
</dependency>
    
<dependency>
         <groupId>com.sun.xml.bind</groupId>
         <artifactId>jaxb-impl</artifactId>
         <scope>runtime</scope>
</dependency>

问题根源

  1. 类型不匹配:StaxEventItemWriterBuilder的marshaller方法要求传入org.springframework.oxm.Marshaller接口的实现类,但代码中使用的是JAXB自带的jakarta.xml.bind.Marshaller,两者不属于同一类型体系。
  2. 缺少依赖:Spring Boot 3的spring-boot-starter-batch默认不再包含spring-oxm依赖,而org.springframework.oxm.Marshaller正是该包下的类,导致IDE找不到该类型。

解决方案

1. 引入spring-oxm依赖

在pom.xml中添加spring-oxm依赖:

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-oxm</artifactId>
</dependency>

2. 使用Spring OXM的Jaxb2Marshaller替代原生JAXB Marshaller

Spring提供的Jaxb2Marshaller实现了org.springframework.oxm.Marshaller接口,同时封装了JAXB的XML处理逻辑,修改代码如下:

@Bean
public StaxEventItemWriter<EmployeeReport> writerTERXFCDJ() throws JAXBException {
    // 使用Spring的Jaxb2Marshaller
    Jaxb2Marshaller jaxbMarshaller = new Jaxb2Marshaller();
    // 指定需要绑定的实体类
    jaxbMarshaller.setClassesToBeBound(EmployeeReport.class);

    return new StaxEventItemWriterBuilder<EmployeeReport>()
            .name("employeeReportItemWriter")
            .version("1.0")
            .rootTagName("EmployeeReportList")
            .resource(new FileSystemResource("a.xml"))
            .marshaller(jaxbMarshaller) // 此处不再报错
            .build();
}

3. 清理冗余JAXB依赖

pom中同时引入了jaxb-runtime和jaxb-impl,两者都是JAXB的实现,保留一个即可,建议保留org.glassfish.jaxb:jaxb-runtime,删除com.sun.xml.bind:jaxb-impl依赖,避免版本冲突。

关于spring-oxm的说明

spring-oxm在Spring Boot 3和Spring Batch 5中仍然被使用,它提供了统一的XML Marshaller/Unmarshaller抽象接口,让Spring Batch等组件可以兼容不同的XML处理框架(JAXB、XStream等)。只是spring-boot-starter-batch不再默认包含该依赖,需要手动引入。

内容的提问来源于stack exchange,提问作者Hasanka Sapumal

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.17 16:23:08