알쓸전컴(알아두면 쓸모있는 전자 컴퓨터)
intellij 에서 json 을 class object Dto 자동 생성 Plugin 소개 본문
intellij 에서 json 을 class object Dto 자동 생성 Plugin 소개
백곳 2019. 10. 28. 21:07
Java Spring 작업중
RESTAPI로 받는 JSON 데이터 형태의 Dto 를 매번 작성하기 힘들어서 쉬운 방법을 찾아 보았다.
코드를 생성 해주는 Plugin 이 있는것을 체험하고 정말 편해서 강추 하며 글을 적는다.
File>>Setting>>Plugins
을 들어가면
라는것을 설치 하여 줍니다.
{
"id": 12314234,
"properties": {
"nickname": "TEST",
"profile_image": "",
"thumbnail_image": ""
},
"kakao_account": {
"profile_needs_agreement": false,
"profile": {
"nickname": "TSET",
"thumbnail_image_url": "TEST",
"profile_image_url": "ETES"
},
"has_email": true,
"email_needs_agreement": false,
"is_email_valid": true,
"is_email_verified": true,
"email": "TEST@naver.com",
"has_age_range": false,
"age_range_needs_agreement": false,
"has_birthday": false,
"birthday_needs_agreement": false,
"has_gender": false,
"gender_needs_agreement": false
}
}
REST API 이렇게 받은 데이터를
Alt+insert 를 눌러 DTO from JSON 을 눌러 줍니다.
Validate 를 눌러 주고
Settings 을 눌러 줍니다.
저는 Jackson 라이브러리를 사용하기에 위와 같이 선택해 주었습니다.
그리고 만들어 줍니다.
그럼 위와 같이 짜잔 하고 나옵니다.
여기서 1가지 중요한게 abstract 추상 클래스로써
objectmapper 로 바로 read 하면 오류가 납니다.
지워 줍니다.
그래서 저는 아래와 같이 사용 하였습니다.
HttpHeaders header = new HttpHeaders();
header.add(HttpHeaders.AUTHORIZATION,Auth);
ResponseEntity<String> response = new RestTemplate().exchange("https://kapi.kakao.com/v2/user/me",
HttpMethod.GET, new HttpEntity(header), String.class);
System.out.println(response.getBody());
ObjectMapper objectMapper = new ObjectMapper();
KaKaoUserInfo kakaouser;
kakaouser = objectMapper.readValue(response.getBody(),KaKaoUserInfo.class);
매우 편하네요.
'Web > Spring Framework tip' 카테고리의 다른 글
spring security + CustomProvider + OAuth2 + JWT Server 설정 및 설명 (GrantTypes은 Password,refresh_token) (0) | 2020.03.04 |
---|---|
Spring @Transactional 사용시 주의점 (0) | 2020.03.01 |
Spring Boot, MyBatis 연동[멀티 데이터 베이스] (0) | 2019.07.08 |
spring -> tomcat 원격 배포 deploy (0) | 2018.10.23 |
spring oracle db + mybatis +log4jdbc 연결 (2) | 2018.04.07 |