๐ต ๋ฐฐ๊ฒฝ
ObjectMapper์ readValue(InputStream in, Class<T> valueType)๋ฅผ ์ฌ์ฉํ๊ธฐ ์ํด์๋ Class<T>๊ฐ ํ์ํ๋ค. ์ผ๋ฐ์ ์ธ ๊ฒฝ์ฐ ๊ตฌ์ฒด์ ์ธ POJO ํด๋์ค ํ์ ์ ๋ช ์ํด์ ์ฌ์ฉํ๋ฉด ๋๋ค.
ํ์ง๋ง Bean์ผ๋ก ๋ฑ๋กํ RestTeamplte์ Jackson2JsonRedisSerializer<T>๋ฅผ ์ถ๊ฐํ๋ ๊ฒ์ฒ๋ผ ํด๋์ค ํ์ ์ ์ด๊ธฐ ์ค์ ํ ์ดํ ๋ณ๊ฒฝ์ด ์ด๋ ค์ด ๊ฒฝ์ฐ, ์ธํฐํ์ด์ค ๋๋ ์ถ์ ํด๋์ค๋ฅผ ํ์ฉํ ๋คํ์ฑ์ด ํ์ํ ์ ์๋ค.
์ด๋ฐ ๊ฒฝ์ฐ ์ผ๋ฐ์ ์ธ ์์ ๋ฐฉ์์ผ๋ก๋ ์ ์ฉ๋์ง ์์ผ๋ฉฐ ๋ณ๋์ ์ค์ ์ด ํ์ํ๋ค. ํด๋น ๋ฐฉ๋ฒ์ ๋ํด์ ์์๋ณด์.
1. ํ๋์ ๊ตฌํ์ฒด๋ง ์๋ ๊ฒฝ์ฐ
์ค์ง ํ๋์ ๊ตฌํ์ฒด๋ง ์๋ ๊ฒฝ์ฐ, @JsonDeserialze ์ด๋ ธํ ์ด์ ์ ํตํด์ Jackson์๊ฒ ์ญ์ง๋ ฌํ ๋ฐฉ๋ฒ์ ๋ํด์ ์๋ ค์ฃผ๋ฉด ํด๊ฒฐ์ด ๊ฐ๋ฅํ๋ค.
public class JacksonTest {
@Test
void jacksonTest() throws IOException {
ObjectMapper objectMapper = new ObjectMapper();
ShibaHolic blackShiba = new BlackShiba("ํ์๋ฐ");
byte[] serializedBlackShiba = objectMapper.writeValueAsBytes(blackShiba);
ShibaHolic shibaHolic = objectMapper.readValue(serializedBlackShiba, ShibaHolic.class);
System.out.println(shibaHolic);
}
@JsonDeserialize(as = BlackShiba.class)
public interface ShibaHolic {}
@NoArgsConstructor
@AllArgsConstructor
@Getter
@ToString
public static class BlackShiba implements ShibaHolic {
private String name;
}
}
2. ๋ ์ด์์ ๊ตฌํ์ฒด๊ฐ ์๋ ๊ฒฝ์ฐ
๊ตฌํ์ฒด๊ฐ ๋ ์ด์ ์๋ ๊ฒฝ์ฐ๋ ์กฐ๊ธ ๋ณต์กํด์ง ์ ์๋ค. Jackson์๊ฒ ์ฌ์ฉํ ๊ตฌํ์ฒด๋ฅผ ๊ตฌ๋ถํ ์ ์๋ ๋ฐฉ๋ฒ์ ์ ๊ณตํด์ผ ํ๋ค.
public class JacksonTest {
@Test
void jacksonTest() throws IOException {
ObjectMapper objectMapper = new ObjectMapper();
ShibaHolic blackShiba = new BlackShiba("ํ์๋ฐ");
byte[] serializedBlackShiba = objectMapper.writeValueAsBytes(blackShiba);
ShibaHolic deserializedBlackShiba = objectMapper.readValue(serializedBlackShiba, ShibaHolic.class);
System.out.println(deserializedBlackShiba);
String jsonBlackShiba = objectMapper.writeValueAsString(deserializedBlackShiba);
System.out.println(jsonBlackShiba);
ShibaHolic whiteShiba = new WhiteShiba("๋ฐฑ์๋ฐ");
byte[] serializedWhiteShiba = objectMapper.writeValueAsBytes(whiteShiba);
ShibaHolic deserializedWhiteShiba = objectMapper.readValue(serializedWhiteShiba, ShibaHolic.class);
System.out.println(deserializedWhiteShiba);
String jsonWhiteShiba = objectMapper.writeValueAsString(deserializedWhiteShiba);
System.out.println(jsonWhiteShiba);
}
@JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
include = JsonTypeInfo.As.PROPERTY,
property = "type"
)
@JsonSubTypes({
@JsonSubTypes.Type(value = BlackShiba.class, name = "black"),
@JsonSubTypes.Type(value = WhiteShiba.class, name = "white")
})
public interface ShibaHolic {}
@NoArgsConstructor
@AllArgsConstructor
@ToString
@Getter
public static class BlackShiba implements ShibaHolic {
private String name;
}
@NoArgsConstructor
@AllArgsConstructor
@ToString
@Getter
public static class WhiteShiba implements ShibaHolic {
private String name;
}
}
๊ตฌํ ๋ฐฉ๋ฒ์ ์๋นํ ๋จ์ํ๋ค. ์ฌ๋ฌ ๊ตฌํ์ฒด๋ฅผ ์ฌ์ฉํ๊ธฐ ์ํด ๋จ์ง @JsonTypeInfo, @JsonSubTypes.Type ์ด๋ ธํ ์ด์ ์ ์ ์ธํด์ Jackson์ด type์ด๋ผ๋ ์์ฑ์ ์ฝ๊ณ ์ฌ๋ฐ๋ฅธ ๊ตฌํ์ฒด๋ฅผ ์ ํํ ์ ์๋๋ก ์๋ ค์ฃผ๊ธฐ๋ง ํ๋ฉด ๋๋ค.
ํ์ง๋ง ํด๋น ๋ฐฉ์์ ๊ฒฐ๊ตญ API์ type ์์ฑ์ ๋ ธ์ถํ๋ค๋ ๋จ์ ์ด ์๋ค. type ์์ฑ์ Jackson์์๋ง ์ฌ์ฉ๋๊ธฐ ๋๋ฌธ์ API์ ํฌํจํ๋ ๊ฒ์ด ๊ณผ์ฐ ์ฌ๋ฐ๋ฅธ์ง์ ๋ํด ํ๋จํ๋ ๊ฒ์ ๊ตฌํ์์ ๋ชซ์ด๋ค.
๋๊ธ