https://api.openweathermap.org/data/2.5/weather
ํ๋ผ๋ฏธํฐ | ์ค๋ช |
---|---|
lat | ์๋ |
lon | ๊ฒฝ๋ |
appid | ๋ฐ๊ธ๋ฐ์ API ํค |
units | ๋จ์ (metric: ์ญ์จ, standard: ์ผ๋น) |
lang | ์ธ์ด ์ฝ๋ (kr , en ๋ฑ) |
package com.example.demo.C02OpenWeatherMap;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URLEncoder;
@RestController
@Slf4j
@RequestMapping("/open/weather")
public class OpenWeatherController {
@GetMapping("/get/{lat}/{lon}")
public ResponseEntity<String> get(@PathVariable String lat, @PathVariable String lon) throws UnsupportedEncodingException {
log.info("GET /open/weather/get...");
String url = "https://api.openweathermap.org/data/2.5/weather";
String serviceKey = "๋ฐ๊ธ ๋ฐ์ ํค";
URI uri = UriComponentsBuilder
.fromHttpUrl(url)
.queryParam("appid", URLEncoder.encode(serviceKey, "UTF-8"))
.queryParam("lat", lat)
.queryParam("lon", lon)
.queryParam("units", "metric") // ์ญ์จ ์ค์
.queryParam("lang", "kr") // ํ๊ธ ์๋ต ์ค์
.build(true)
.toUri();
RestTemplate rt = new RestTemplate();
ResponseEntity<String> response = rt.exchange(uri, HttpMethod.GET, null, String.class);
return response;
}
}
http://localhost:8090/open/weather/get/35.8662861111111/128.608397222222
{
"name": "Daegu",
"weather": [
{
"main": "Clear",
"description": "clear sky",
"icon": "01d"
}
],
"main": {
"temp": 23.0,
"feels_like": 22.2,
"humidity": 35,
"pressure": 1010
},
"wind": {
"speed": 3.09,
"deg": 270
}
}
โป units=metric ์ค์ ๋๋ถ์ ์จ๋๋ ์ญ์จ(ยฐC)๋ก ๋ณํ๋์ด ์ถ๋ ฅ๋จ
โป
lang=kr
์ค์ ์ผ๋กdescription
์ ํ๊ธ ์ง์ ๊ฐ๋ฅ
JSON Key ๊ฒฝ๋ก | ์ค๋ช | ์์ ๊ฐ |
---|---|---|
name | ๋์๋ช | Daegu |
weather[0].main | ๋ ์จ ์์ฝ | Clear |
weather[0].description | ์์ธ ์ค๋ช | clear sky |
main.temp | ํ์ฌ ์จ๋ (ยฐC) | 23.0 |
main.feels_like | ์ฒด๊ฐ ์จ๋ | 22.2 |
main.humidity | ์ต๋ (%) | 35 |
wind.speed | ํ์ (m/s) | 3.09 |
RestTemplate
์ ํ์ฉํ๋ฉด Spring Boot์์๋ ๊ฐํธํ๊ฒ ์ฐ๋ ๊ฐ๋ฅunits=metric
, lang=kr
ํ๋ผ๋ฏธํฐ ์ค์ ์ผ๋ก ์ญ์จ ๋ฐ ํ๊ธ ์ฒ๋ฆฌ๊น์ง ๊ฐ๋ฅ