๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Java

ObjectMapper readValue ๋‹คํ˜•์„ฑ ์ ์šฉํ•˜๊ธฐ (+ Jackson2JsonRedisSerializer)

by ํ‘์‹œ๋ฐ” 2023. 7. 16.

๐Ÿ˜ต ๋ฐฐ๊ฒฝ

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์— ํฌํ•จํ•˜๋Š” ๊ฒƒ์ด ๊ณผ์—ฐ ์˜ฌ๋ฐ”๋ฅธ์ง€์— ๋Œ€ํ•ด ํŒ๋‹จํ•˜๋Š” ๊ฒƒ์€ ๊ตฌํ˜„์ž์˜ ๋ชซ์ด๋‹ค.

๋Œ“๊ธ€