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

引入JPOS依赖后logback-spring.xml初始化失败问题求助

问题解决:Logback因Crimson SAX解析器导致初始化失败

问题根源

JPOS的GenericValidatingPackager类硬编码了Crimson作为默认SAX解析器,而Crimson是老旧的XML解析库,不支持Logback初始化时尝试设置的http://xml.org/sax/features/external-general-entities特性,从而抛出SAXNotSupportedException,导致Logback无法加载配置文件。

解决方案

方案一:通过JVM参数强制指定现代SAX解析器(推荐)

在应用启动时添加JVM参数,覆盖JPOS的默认解析器设置,使用JDK自带的SAX解析器:

-Dsax.parser=com.sun.org.apache.xerces.internal.parsers.SAXParser

方案二:在代码中提前设置系统属性

如果无法通过JVM参数配置,可在Spring Boot启动类的main方法最开头添加代码,确保在Logback初始化前完成解析器指定:

public static void main(String[] args) {
    System.setProperty("sax.parser", "com.sun.org.apache.xerces.internal.parsers.SAXParser");
    SpringApplication.run(Application.class, args);
}

方案三:移除Crimson依赖(可选)

既然已通过系统属性指定了其他解析器,可直接删除pom.xml中的Crimson依赖项,避免老旧库带来的潜在冲突:

<!-- 删除该依赖 -->
<dependency>
    <groupId>crimson</groupId>
    <artifactId>crimson</artifactId>
    <version>1.1.3</version>
    <optional>true</optional>
</dependency>

验证

启动应用后,Logback将正常加载logback-spring.xml,同时JPOS的ISO消息转换功能也能通过指定的SAX解析器正常处理XML配置。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.06 16:55:26