samedi 14 mars 2020

Java Class based on Enum fileds

I'm dealing here with JWT and setting up custom fields. All those custom fields are described in Enum

public enum JwtFields {
    userId,
    manyOtherCustomFieldsBellow,
    blaBlaBla
}

so whenever I create token instead of passing strings for the keys, I'm using an enum as it is faster and safer.

Claims claims = Jwts.claims().setSubject(userId);
claims.put(JwtFields.someCustomFieldFromEnum.name(), "someValue")

Now, once I check that received token is valid and all necessary custom fields are present I want to deserialise it to some TokenDecoded class and attach it to request payload, so whenever I'm processing request I will have all values and fields from the JWT token.

However, that deserialise class pretty much contains all fields from Enum, and if tomorrow I will add new filed to enum, ill have to also manually update my TokenDecoded class to include that new custom filed too

The question: How can I make this TokenDecoded class to be based on enum fields, so if I add a new field to Enum, it will be automatically present in TokenDecoded. Is there reflection involved? or it could be achieved simpler?





Aucun commentaire:

Enregistrer un commentaire