본문 바로가기

프로그래밍 TIP/Spring

Spring에서 Retrofit 라이브러리 활용하기 - 1

  신입일때 어려운 개념이었던 Http +_+ 이제야 감이 좀 옵니다.

기본적으로 스프링에서는 HttpURlConnection으로 요청 후 스트림을 열어서 데이터를 

얻어오는 방법 그리고 RestTemplate 요청/응답을 처리할 수 도 있습니다.

 그러나 요즘은 Android에서 주로 사용하던 Retrofit을 사용하기도 한다고 하더라구요

Android  개발 시 많이 사용했던 라이브러리라 노하우를 공유하겠습니다.

해당 라이브러리에 대한 정보는 아래의 Git 저장소에서 참고하시면 되겠습니다.

https://github.com/square/retrofit

 

square/retrofit

Type-safe HTTP client for Android and Java by Square, Inc. - square/retrofit

github.com

1. 라이브러리 url로 이동합니다. 

저는 개인적으로 아래의 사이트를 자주 사용합니다.

https://mvnrepository.com/

 

2. 해당 라이브러리 사이트로 이동

https://mvnrepository.com/artifact/com.squareup.retrofit2/retrofit/2.5.0

 

Maven Repository: com.squareup.retrofit2 » retrofit » 2.5.0

com.squareup.retrofit2 retrofit 2.5.0 // https://mvnrepository.com/artifact/com.squareup.retrofit2/retrofit compile group: 'com.squareup.retrofit2', name: 'retrofit', version: '2.5.0' // https://mvnrepository.com/artifact/com.squareup.retrofit2/retrofit li

mvnrepository.com

3. 의존성 추가 관련 소스 확인

Spring  개발자라면 Maven이나 Gradle 등등을 확인하시면 되겠습니다. Jar 파일 형태로도 제공 되기 때문에 

직접 다운로드 후 사용할 수 도 있습니다.

저는 Maven을 사용하기 때문에 아래와 같이 추가했습니다.

라이브러리 추가는 끝났습니다.

4. Retrofit용 인터페이스 구성

문서에도 잘 나와있지만 Retrofit은 클라이언트를 어노테이션 기반 인터페이스로 구성하셔야 합니다.

다음편에 이어서 하겠습니다.