본문 바로가기

JAVA

POJO를 Map으로 바꾸기

package com.fasterxml.jackson.databind;

해당 패키지에 있는 ObjectMapper를 써보자.

ObjectMapper oMapper = new ObjectMapper();

 

Map<String, Object> map = oMapper.convertValue(object, Map.class);

convertValue 메서드로 POJO 객체를 Map으로 변경 가능하다

혹시 LocalDateTime이 이상하게 나오는가? 

String format = "yyyy-MM-dd HH:mm:ss";

map.put("entryTime", dateUtils.convertDateTimeToCustomFormat(object.getDateTime(),format));

이렇게 entry를 하나 더 넣자