Convert a Map<String, String> to a POJO

user86834 picture user86834 · May 7, 2013 · Viewed 190.2k times · Source

I've been looking at Jackson, but is seems I would have to convert the Map to JSON, and then the resulting JSON to the POJO.

Is there a way to convert a Map directly to a POJO?

Answer

Jongwook Choi picture Jongwook Choi · May 8, 2013

Well, you can achieve that with Jackson, too. (and it seems to be more comfortable since you were considering using jackson).

Use ObjectMapper's convertValue method:

final ObjectMapper mapper = new ObjectMapper(); // jackson's objectmapper
final MyPojo pojo = mapper.convertValue(map, MyPojo.class);

No need to convert into JSON string or something else; direct conversion does much faster.