Search code examples
javacollectionsjjwt

Fetching an object of type Set<String> from a JWT?


I am trying to fetch some data from JSON web token, using the JJWT library : https://github.com/jwtk/jjwt

The method signature is like so :

<T> T get(String claimName, Class<T> requiredType);

In a claim attribute I have a Set of type String.

When trying to fetch from this method like so

Claims claims - Jwts.parser()...
claims.get("Role", Set.class);

I get the following error

! io.jsonwebtoken.RequiredTypeException: Expected value to be of type: interface java.util.Set, but was class java.util.ArrayList

When setting this claim the type is of Set

public Set<String> getRoles() {
    return roles;
}  ....


claim("Role", user.getRoles())

Any ideas how to solve this?


Solution

  • JSON and therefore JWT does not have a concept of Set, retrieve the data as an Array / ArrayList and convert it to a Set: Easiest way to convert a List to a Set in Java