1

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

Reporter
  • 3,897
  • 5
  • 33
  • 47
berlot83
  • 141
  • 1
  • 2
  • 9

3 Answers3

2

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.

Reporter
  • 3,897
  • 5
  • 33
  • 47
berlot83
  • 141
  • 1
  • 2
  • 9
0

Adding the project dependency 'io.jsonwebtoken:jjwt-jackson:0.11.2' will resolve the issue, as it contains what you need (as per my comment on berlot83's answer).

Also best check for updated versions (compatible with your other dependencies of course) as it's always wise to stay updated.

Continuity8
  • 2,403
  • 4
  • 19
  • 34
0

Add this dependency into your pom.xml file to fix the issue :)

<dependency>
    <groupId>io.jsonwebtoken</groupId>
    <artifactId>jjwt-jackson</artifactId>  
    <version>0.11.1</version>
    <scope>runtime</scope>
</dependency>
Laurel
  • 5,965
  • 14
  • 31
  • 57