Runtime or class error running JJWT Json Token

吃可爱长大的小学妹 提交于 2019-12-12 03:27:51

问题


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.

Here my source code:

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());
    }

}

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 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

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

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

What do I do wrong?

Thank you


回答1:


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.

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.

This is the picture with the problem solved.

Thank you.



来源:https://stackoverflow.com/questions/43470940/runtime-or-class-error-running-jjwt-json-token

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!