Jackson ObjectMapper 정리

juryang han·2022년 7월 27일
1

내용을 확인하거나 내용물의 Parsing 과정이 필요할 때가 있다.

메이븐일 경우

<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.12.3</version>
</dependency>

gradle일 경우

//https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind
implementation 'com.fasterxml.jackson.core:jackson-databind:2.12.3'

ㄷㅇㅇ프로젝트에 제이슨 파싱 에러가 났다.
이유는 InstaRes로 변환하려고 하니까 에러가 났다.


InstaRes(Instagram instagram) 안에 Instagram 데이터를 저장시켜
변환하려고 해서 값이 주입이 안되었다.

    public InstaRes(){
        
    }

빈 껍데기로 만든 후 내가 원하는 값들을 주입시켜 데이터를 가져와야 한다.

//에러 
ObjectMapper mapper = new ObjectMapper();
instaRes info = mapper.readValue(geoInfo, instaRes.class);
            
// 방법1            
ObjectMapper mapper = new ObjectMapper();
Map<String, Object> map = new HashMap<>();
Integer expire = (Integer)map.get("expires_in");

Instagram instagram = new Instagram();
instagram.setExpires_in(expire);
instagramService.updateInstaToken(instagram);
//방법2
ObjectMapper mapper = new ObjectMapper();
InstaRes insta = mapper.readValue(json, InstaRes.class);

0개의 댓글