运行 JJWT Json 令牌的运行时或类错误

编程入门 行业动态 更新时间:2024-10-09 08:29:13
本文介绍了运行 JJWT Json 令牌的运行时或类错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

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

I have a little problem. I´ve been trying to use different libraries to produce a json token and now I'm using JJWT from Stormpath. They have tutorials well explained. But my problem is, when I try to run the String method in a "public static void main" method, I get a runtime or class error. In their official website says there is a requeriment that the jackson library must being newer than Version 2.8. So I downloaded such library.

这是我的源代码:

package orgunidadIT.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)
          pact();

        return compactJws;
    }

    public static void main(String args[]){

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

}

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

The console show following error message:

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 orgunidadIT.proyecto.accesoDatos.ValidarToken.token(ValidarToken.java:16)
at orgunidadIT.proyecto.accesoDatos.ValidarToken.main(ValidarToken.java:27)

来自 maven 依赖项的图像,其中 jackson 似乎没问题

来自控制台的带有错误的图像

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

As you can see the jackson dependencies appears to be fine.

此外,我还在参考库的构建路径中添加了更多库,但它们位于 pom.xml 之外.

Also I aatached more libreries to the build-path on reference libraries, but they are outside from the pom.xml.

我做错了什么?

谢谢

推荐答案

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

I answer myself so perhaps someone could have the same problem.

我已经遇到这个问题大约一个月了,直到我有机会删除位于我的项目中的一些旧库.

I've been with this problem about a month, until I got balls to delete some old libraries located in my project.

问题似乎是我使用 jackson 2.8.2 或更高版本声明了 Maven 依赖关系,并且在参考库"中我的库低于 1.9,当我从构建路径中删除时,问题就消失了.现在我可以看到字符串令牌了.

The problem appeared to be that I declared a Maven dependecy with jackson 2.8.2 or later and in the 'reference libraries' I had libraries lowers than 1.9, when I removed from my Build-Path the problem was gone. And now I can see the String Token.

这是解决问题的图片.

谢谢.

这篇关于运行 JJWT Json 令牌的运行时或类错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-30 04:45:50,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1388949.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:令牌   错误   JJWT   Json

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!