0

I am trying to make annotation in spring boot project. I want to use this annotation for multiple methods which has got different type of request body model classes. But I have to convert request bodies to general object. Because I can't predict type of object before.

By the way I will take only one field which is "username" from any object. Every object have "username" field but classes are different.

Map<String, Object> map = new HashMap<>();
final ObjectMapper mapper = new ObjectMapper();
final Object pojo = mapper.convertValue(map, Object.class);
String username = pojo.getUsername();
gurkan
  • 509
  • 1
  • 4
  • 18

1 Answers1

0

I found the solution:

https://stackoverflow.com/a/52406946/10277150

public static Map<String, Object> parameters(Object obj) {
        Map<String, Object> map = new HashMap<>();
        for (Field field : obj.getClass().getDeclaredFields()) {
            field.setAccessible(true);
            try { map.put(field.getName(), field.get(obj)); } catch (Exception e) { }
        }
        return map;
    }
gurkan
  • 509
  • 1
  • 4
  • 18