gpt4 book ai didi

java - 运行 JJWT Json token 时出现运行时或类错误

转载 作者:行者123 更新时间:2023-12-02 02:53:17 25 4
gpt4 key购买 nike

我有一个小问题。我一直在尝试使用不同的库来生成 json token ,现在我正在使用 Stormpath 中的 JJWT。他们有很好解释的教程。但我的问题是,当我尝试在“public static void main”方法中运行 String 方法时,出现运行时或类错误。在他们的官方网站上说,有一个要求,jackson库必须比2.8版本更新。所以我下载了这样的库。

这是我的源代码:

package org.comunidadIT.proyecto.accesoDatos;

import java.security.Key;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import io.jsonwebtoken.impl.crypto.MacProvider;

public class ValidarToken {

public String token(){
// We need a signing key, so we'll create one just for this example. Usually
// the key would be read from your application configuration instead.
Key key = MacProvider.generateKey();

String compactJws = Jwts.builder()
.setSubject("Joe")
.signWith(SignatureAlgorithm.HS512, key)
.compact();

return compactJws;
}

public static void main(String args[]){

ValidarToken t= new ValidarToken();
System.out.println(t.token());
}

}

控制台显示以下错误消息:

Exception in thread "main" java.lang.NoSuchFieldError: USE_DEFAULTS
at com.fasterxml.jackson.annotation.JsonInclude$Value.<clinit>(JsonInclude.java:204)
at com.fasterxml.jackson.databind.cfg.MapperConfig.<clinit>(MapperConfig.java:44)
at com.fasterxml.jackson.databind.ObjectMapper.<init>(ObjectMapper.java:549)
at com.fasterxml.jackson.databind.ObjectMapper.<init>(ObjectMapper.java:465)
at io.jsonwebtoken.impl.DefaultJwtBuilder.<clinit>(DefaultJwtBuilder.java:42)
at io.jsonwebtoken.Jwts.builder(Jwts.java:116)
at org.comunidadIT.proyecto.accesoDatos.ValidarToken.token(ValidarToken.java:16)
at org.comunidadIT.proyecto.accesoDatos.ValidarToken.main(ValidarToken.java:27)

Image from maven dependencies where appears to be fine with jackson

Image from the console with erros

如您所见,jackson 依赖项似乎没问题。

此外,我还向引用库的构建路径附加了更多库,但它们位于 pom.xml 之外。

我做错了什么?

谢谢

最佳答案

我自己回答,所以也许有人会遇到同样的问题。

我已经遇到这个问题大约一个月了,直到我有勇气删除项目中的一些旧库。

问题似乎是我声明了与 jackson 2.8.2 或更高版本的 Maven 依赖关系,并且在“引用库”中我有低于 1.9 的库,当我从构建路径中删除时,问题就消失了。现在我可以看到字符串 token 。

This is the picture with the problem solved.

谢谢。

关于java - 运行 JJWT Json token 时出现运行时或类错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43470940/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com