dimanche 24 octobre 2021

Jackson typeRefernce with dynamic method parameter type

I want to be able to create Jackson typeRefernce with dynamic method parameter type with below example using reflection method.getParameterTypes()[1].

Since TypeRefernce only takes compile type parameters but I heard you can do some trick also to work with dynamic types.

 Method method = methodsMap.getOrDefault("createXXX".toLowerCase(), null);

        if(method != null && myBean!= null){
       
               Object retval  = method.invoke(myBean, mapper.convertValue(product , new TypeReference<method.getParameterTypes()[1]>(){}));
        }




Aucun commentaire:

Enregistrer un commentaire