Skip to content

Instantly share code, notes, and snippets.

@ihoneymon
Last active May 9, 2018 06:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ihoneymon/8fa579f8497caeb899127601e3effcd3 to your computer and use it in GitHub Desktop.
Save ihoneymon/8fa579f8497caeb899127601e3effcd3 to your computer and use it in GitHub Desktop.
/**
* ObjectMapper 단위테스트
*/
@Slf4j
public class ObjectMapperTest {
private ObjectMapper objectMapper;
@Before
public void setUp() throws Exception {
objectMapper = new ObjectMapper();
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
}
@Test
public void testSourceObject() throws JsonProcessingException {
Message source = Message.builder().subject("Test subject").body("Test body").build();
log.debug("Result: {}", objectMapper.convertValue(source, Map.class));
log.debug("Result: {}", objectMapper.writeValueAsString(source));
}
@Getter
@Builder
public static class Message {
/**
* static 필드인 경우 ObjectMapper 가 변환에서 제외하는군!
*/
private final static String DEFAULT_FIELD = "test";
private String subject;
private String body;
private String description = DEFAULT_FIELD;
@JsonIgnore
private String ignoredField = "ignored";
}
//11:16:11.375 [main] DEBUG ObjectMapperTest - Result: {subject=Test subject, body=Test body}
//11:16:11.382 [main] DEBUG ObjectMapperTest - Result: {"subject":"Test subject","body":"Test body"}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment