获取异常java.lang.NoClassDefFoundError:无法初始化类javax.xml.bind.DatatypeConverterImpl

时间:2019-04-10 06:59:35

标签: spring-security jwt

在实现Jwt Spring Security实现时得到以下异常:“ java.lang.NoClassDefFoundError:无法初始化类javax.xml.bind.DatatypeConverterImpl。”在Spring Boot项目中

pom.xml中的依赖项如下:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <!-- For Working with Json Web Tokens (JWT) -->
    <dependency>
        <groupId>io.jsonwebtoken</groupId>
        <artifactId>jjwt</artifactId>
        <version>0.9.0</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
    <!-- marked the embedded servlet container as provided -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>com.zaxxer</groupId>
        <artifactId>HikariCP</artifactId>
        <version>2.6.0</version>
    </dependency>
    <!-- hot swapping, disable cache for template, enable live reload -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
        <version>1.3.3.RELEASE</version>
        <exclusions>
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-logging</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

    <!-- Optional, for bootstrap -->
    <dependency>
        <groupId>org.webjars</groupId>
        <artifactId>bootstrap</artifactId>
        <version>3.3.7</version>
    </dependency>

    <dependency>
        <groupId>com.oracle</groupId>
        <artifactId>ojdbc14</artifactId>
        <scope>compile</scope>
        <version>1.0</version>
    </dependency>

    <dependency>
        <groupId>javax.activation</groupId>
        <artifactId>activation</artifactId>
        <version>1.1</version>
    </dependency>
    <dependency>
        <groupId>org.modelmapper</groupId>
        <artifactId>modelmapper</artifactId>
        <version>2.3.0</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-log4j</artifactId>
        <version>1.2.5.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jersey</artifactId>
    </dependency>
    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-client</artifactId>
        <version>1.8</version>
    </dependency>
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-lang3</artifactId>
        <version>3.7</version>
    </dependency>
    <dependency>
        <groupId>org.json</groupId>
        <artifactId>json</artifactId>
        <version>20180813</version>
    </dependency>

</dependencies>

详细的引导程序如下:

Handler dispatch failed; nested exception is java.lang.NoClassDefFoundError: 
Could not initialize class javax.xml.bind.DatatypeConverterImpl
] with root cause
java.lang.NoClassDefFoundError: Could not initialize class javax.xml.bind.DatatypeConverterImpl
    at javax.xml.bind.DatatypeConverter.initConverter(Unknown Source)
    at javax.xml.bind.DatatypeConverter.parseBase64Binary(Unknown Source)
    at io.jsonwebtoken.impl.Base64Codec.decode(Base64Codec.java:26)
    at io.jsonwebtoken.impl.DefaultJwtBuilder.signWith(DefaultJwtBuilder.java:99)
    at com.mpcz.services.jwt.security.JwtTokenUtil.generateToken(JwtTokenUtil.java:92)
    at com.mpcz.services.jwt.security.JwtTokenUtil.generateToken(JwtTokenUtil.java:85)
    at com.mpcz.services.controller.ConsumerLoginController.register(ConsumerLoginController.java:183)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

在以下代码中生成JWT令牌时获取上述错误:

Jwts.builder().setClaims(claims).setExpiration(generateExpirationDate()).signWith(SignatureAlgorithm.HS512, secret).compact();

5 个答案:

答案 0 :(得分:5)

为将来参考,该解决方案添加了以下依赖项,以避免出现Java 11问题:

对于Maven项目:

<dependency> 
    <groupId>javax.xml.bind</groupId> 
    <artifactId>jaxb-api</artifactId>
    <version>2.1</version>
</dependency>

对于gradle项目:

compile 'javax.xml.bind:jaxb-api:2.1'

@Giggs的积分

答案 1 :(得分:3)

添加 JAXB API 依赖项解决了我的问题。

<!-- https://mvnrepository.com/artifact/javax.xml.bind/jaxb-api -->

<dependency>
    <groupId>javax.xml.bind</groupId>
    <artifactId>jaxb-api</artifactId>
    <version>2.3.1</version>
</dependency>

答案 2 :(得分:0)

使用OpenJDK 11时,这对我也很好,而无需进行任何代码修改。

我在前端类上使用的是java.base。* enc / dec,而不是javax.xml.bind.DatatypeConverter,但是jjwt(0.9.0)在生成令牌时将其重新引入。

您的解决方案很好,不必将Xercesxml添加到我的pom.xml中,可能是因为我只是在做基本的jwt。

此致

Dezman!

答案 3 :(得分:0)

是的,对我来说,添加 javax api 依赖项修复了我的错误。

compile 'javax.xml.bind:jaxb-api:2.1'

谢谢@Andrei

答案 4 :(得分:0)

将此依赖项添加到 pom 文件对我有用:

<dependency>
    <groupId>javax.xml.bind</groupId>
    <artifactId>jaxb-api</artifactId>
    <version>2.3.1</version><!--$NO-MVN-MAN-VER$-->
</dependency>