1. dependency 추가

- 주의 : spring boot 와 spring cloud 호환 체크

- spring.io/projects/spring-cloud

 

Spring Cloud

Spring Cloud is an umbrella project consisting of independent projects with, in principle, different release cadences. To manage the portfolio a BOM (Bill of Materials) is published with a curated set of dependencies on the individual project. Go here to r

spring.io

사용한 버전

plugins {
	id 'org.springframework.boot' version '2.3.5.RELEASE'
	id 'io.spring.dependency-management' version '1.0.10.RELEASE'
	id 'java'
}

group = 'com.solution'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '14'

repositories {
	mavenCentral()
}

ext {
	set('springCloudVersion', "Hoxton.SR5")
}

dependencyManagement {
	imports {
		mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
	}
}

dependencies {
	//feign
	compile("org.springframework.cloud:spring-cloud-starter-openfeign")
    
    ...
}

2. 설정 추가

package com.solution.framework.config;

import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Configuration;

@Configuration
//@FeignClient가 있는 페키지명 
@EnableFeignClients(basePackages = {"com.solution.common.feign"})
public class FeignConfig {

}

3. DTO 생성

package com.solution.common.feign.dto;

import java.util.List;
import java.util.Map;

import lombok.Data;

@Data
public class GoogleDTO {
	List<Map<String,Object>> results;
	String status;
    String error_message;
}

4. FeignClient 생성

package com.solution.common.feign;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.solution.common.feign.dto.GoogleDTO;
import com.solution.framework.config.FeignConfig;

/**
 * 구글 API
 * @author ljo
 *
 */
@FeignClient(name = "apilayerProvider", url = "http://maps.googleapis.com", configuration = FeignConfig.class)
public interface GoogleProvider {
	@GetMapping(value = "/maps/api/geocode/json?sensor=false&language=ko&latlng={latlng}")
    GoogleDTO getGeocode(@RequestParam(value = "latlng") String latlng);
}

5. API호출

@Autowired
private GoogleProvider googleProvider;

GoogleDTO result = googleProvider.getGeocode("latlng");

6. 결과

{
   "error_message" : "Invalid request. Invalid 'latlng' parameter.",
   "results" : [],
   "status" : "INVALID_REQUEST"
}

'개발 > java&kotlin' 카테고리의 다른 글

스프링부트에서 Scheduling 사용하기  (0) 2020.11.25
Spring boot - logback 로그처리  (0) 2020.03.27
java 로 캡챠 구현  (0) 2020.03.26
WebBindingInitializer 활용  (0) 2018.07.12
spring 크로스 도메인 처리  (0) 2018.07.12