SAX Feature Not Supported in Spring Boot 2.6.1
Asked Answered
G

5

6

Spring boot 2.6.1 supports logback 1.2.7

I've added the following dependencies in project

        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-core</artifactId>
        </dependency>

But the build fails with below exception:

Caused by: javax.xml.parsers.ParserConfigurationException: SAX feature 'http://xml.org/sax/features/external-general-entities' not supported.
    at oracle.xml.jaxp.JXSAXParserFactory.setFeature(JXSAXParserFactory.java:272)
    at ch.qos.logback.core.joran.event.SaxEventRecorder.buildSaxParser(SaxEventRecorder.java:82)
    ... 44 more

In SaxEventRecorder.class, it fails while building the SAX parser

    private SAXParser buildSaxParser() throws JoranException {
        try {
            SAXParserFactory spf = SAXParserFactory.newInstance();
            spf.setValidating(false);
            //spf.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
            spf.setFeature("http://xml.org/sax/features/external-general-entities", false);
            spf.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
            spf.setNamespaceAware(true);
            return spf.newSAXParser();
        } catch (Exception pce) {
            String errMsg = "Parser configuration error occurred";
            addError(errMsg, pce);
            throw new JoranException(errMsg, pce);
        }
    }

Is there a way to override the implementation?

Thanks :)

Ghastly answered 7/12, 2021 at 6:25 Comment(0)
Y
13

Had the same issue during an upgrade. It wasn't a Spring issue, and you don't want to override the implementation yourself.

SAXParserFactory is an abstract class - it can have multiple implementations. The newInstance method picks the last/top implementation on the classpath.

I had found that I had an "extra" implementation on my classpath due to a dependency having their own implementation of SAXParserFactory rather than the 'typical' implementation that Logback expects. And this "extra" SAXParserFactory did not support the feature that Logback is trying to enable.

I had to explicitly declare a xerces implementation higher in my dependencies in order for a "real" implementation to take precedence over the other "extra" implementation on the classpath.

So my advice is for you to see what implementations of this abstract class you have (I could easily see this using the IntelliJ IDE), then manage/re-arrange your dependencies so that a proper implementation supporting the feature has a higher precedence on your classpath.

Yield answered 12/12, 2021 at 20:10 Comment(2)
The implementation to use also can be defined via "-Djavax.xml.parsers.SAXParserFactory=com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl" as java command line argumentSeptum
In case someone likes to know, I fixed it by adding xerces:xercesImpl:2.12.2. In my case the error was caused because I need the com.oracle.database.xml:xmlparserv2 dependency. The xerces was added as the first dependency in the pom.Bamberg
L
2

May be it is not exactly what you're looking for, but for my project we made a decision to exclude this parser:

    <dependency>
            <groupId>com.oracle.database.jdbc</groupId>
            <artifactId>ojdbc8-production</artifactId>
            <type>pom</type>
        
            <exclusions>
                <exclusion>
                    <groupId>com.oracle.database.xml</groupId>
                    <artifactId>xmlparserv2</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

But if you really need it, I suggest you to look at @matzeihnsein's answer and to upgrade your version. Doing so also helped me.

Letti answered 14/5, 2023 at 20:23 Comment(0)
S
0

for me it was that we used an old dependency

 <groupId>com.oracle.jdbc</groupId>
 <artifactId>ojdbc7</artifactId>
 <version>12.1.0.2</version>

updating to

<groupId>com.oracle.jdbc</groupId>
<artifactId>ojdbc8</artifactId>
<version>12.2.0.1</version>

ojdbc8 solved my issue!

Sherrisherrie answered 26/4, 2022 at 9:45 Comment(0)
S
0

In my case it helped:

implementation ('com.oracle.jdbc:ojdbc7:12.1.0.2') {
    exclude group: 'com.oracle.jdbc', module: 'xmlparserv2'
}
Sexuality answered 13/6, 2024 at 19:43 Comment(0)
W
0

This is covered by the comment from @BitfulByte under the accepted answer but I am adding the answer for the better visibility since at the first glance I didn't read the comments

If you get the following error:

Caused by: javax.xml.parsers.ParserConfigurationException: SAX feature 'http://xml.org/sax/features/external-general-entities' not supported.
    at oracle.xml.jaxp.JXSAXParserFactory.setFeature(JXSAXParserFactory.java:272)
    at ch.qos.logback.core.joran.event.SaxEventRecorder.buildSaxParser(SaxEventRecorder.java:88)
    ... 45 more

Then as explained in the comment you need to ensure that the SAXParserFactory implementation from xercesImpl comes first so that logback can use it.

Simply define it first (before xmlparserv2) in pom.xml:

<dependency>
    <groupId>xerces</groupId>
    <artifactId>xercesImpl</artifactId>
    <version>2.12.2</version>
</dependency>
<dependency>
    <groupId>com.oracle.database.xml</groupId>
    <artifactId>xmlparserv2</artifactId>
    <version>21.15.0.0</version>
</dependency>
Wroughtup answered 23/8, 2024 at 8:44 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.