ํ๋ก๊ทธ๋๋ฐ ์ธ๊ณ๋ฅผ ํ๊ตฌํฉ์๋ค.
๊ตฌํ ํ๋ฉด
๋ค์ด๋ฒ API๋ฅผ ํ์ฉํด์ ๋ด์ค ๋ฐ์ดํฐ๋ฅผ ํ์ด์ง์ ์ถ๋ ฅํ ์ ์์ต๋๋ค.
1. ๋ค์ด๋ฒ ๊ฐ๋ฐ์์ผํฐ (NAVER Developers) ์ ์ ์ํฉ๋๋ค.
๋ค์ด๋ฒ ๊ฐ๋ฐ์ ์ผํฐ - NAVER Developers
2. Documents > ์๋น์ค API > ๊ฒ์
๊ฒ์ > ๋ด์ค - Search API (naver.com)
์ ํ์ด์ง์์ ์์ธํ API ๋ฑ๋ก, ํ์ฉ ๋ฐฉ์์ ํ์ธํ ์ ์์ต๋๋ค.
๋จผ์ ์คํ API ์ด์ฉ์ ์ ์ฒญํ์ฌ ์ ํ๋ฆฌ์ผ์ด์ ๋ฑ๋ก (API ์ด์ฉ์ ์ฒญ) ์ ์งํํฉ๋๋ค.
์ฌ์ฉ API๋ ๊ฒ์, ๋ฐ์ดํฐ๋ฉ (๊ฒ์์ด ํธ๋ ๋), ๋ฐ์ดํฐ๋ฉ (์ผํ์ธ์ฌ์ดํธ)๋ก ์ค์ ํ์ต๋๋ค.
๋น๋ก์ธ ์คํ API ์๋น์ค ํ๊ฒฝ์ http://localhost ์ ๋ ฅํ์์ต๋๋ค.
3. ๋ด์ค ๊ตฌํ ์์
๋ด์ค ๊ตฌํ ์์ ๋ ๋ธ๋ก๊ทธ ๊ฒ์ ์์ ์ ๋์ผํ๋ฏ๋ก, ๋ธ๋ก๊ทธ ๊ฒ์ ์ฌ์ดํธ์์ ํ์ธํ ์ ์์ต๋๋ค.
๊ฒ์ > ๋ธ๋ก๊ทธ - Search API (naver.com)
JAVA, PHP, Node.js, Python, C# ์ฝ๋๊ฐ ์ ๊ณต๋์ด ์์ต๋๋ค.
4. Spring Boot ์์ ์ฝ๋
@Controller
public class NewsController {
@Autowired
HttpSession session;
@GetMapping("media/news")
public String news(String searchText, Model model) {
User user = (User) session.getAttribute("user_info");
if (user != null) {
int userCoins = user.getCoin();
model.addAttribute("userCoin", userCoins);
}
String clientId = "์ ํ๋ฆฌ์ผ์ด์
ํด๋ผ์ด์ธํธ ์์ด๋";
String clientSecret = "์ ํ๋ฆฌ์ผ์ด์
ํด๋ผ์ด์ธํธ ์ํฌ๋ฆฟ";
String text = null;
try {
text = URLEncoder.encode(searchText, "UTF-8");
} catch (UnsupportedEncodingException e) {
throw new RuntimeException("๊ฒ์์ด ์ธ์ฝ๋ฉ ์คํจ", e);
}
String apiURL = "https://openapi.naver.com/v1/search/news?query=" + text + "&display=30";
// String apiURL = "https://openapi.naver.com/v1/search/blog?query=" + text +
// "&display=30";
// String apiURL = "https://openapi.naver.com/v1/search/blog.xml?query="+ text;
// // XML ๊ฒฐ๊ณผ
Map<String, String> requestHeaders = new HashMap<>();
requestHeaders.put("X-Naver-Client-Id", clientId);
requestHeaders.put("X-Naver-Client-Secret", clientSecret);
String responseBody = get(apiURL, requestHeaders);
ObjectMapper om = new ObjectMapper(); // ์ฌ๊ธฐ์ 'om'์ด ์ ์ธ๋์์ต๋๋ค.
Map json = new HashMap<>();
try {
json = om.readValue(responseBody, Map.class); // 'om'์ ์ฌ์ฉํ์ฌ JSON ์๋ต์ ํ์ฑํฉ๋๋ค.
} catch (JsonMappingException e) {
e.printStackTrace();
} catch (JsonProcessingException e) {
e.printStackTrace();
}
model.addAttribute("news", json.get("items"));
return "/media/news";
}
private static String get(String apiUrl, Map<String, String> requestHeaders) {
HttpURLConnection con = connect(apiUrl);
try {
con.setRequestMethod("GET");
for (Map.Entry<String, String> header : requestHeaders.entrySet()) {
con.setRequestProperty(header.getKey(), header.getValue());
}
int responseCode = con.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) { // ์ ์ ํธ์ถ
return readBody(con.getInputStream());
} else { // ์ค๋ฅ ๋ฐ์
return readBody(con.getErrorStream());
}
} catch (IOException e) {
throw new RuntimeException("API ์์ฒญ๊ณผ ์๋ต ์คํจ", e);
} finally {
con.disconnect();
}
}
private static HttpURLConnection connect(String apiUrl) {
try {
URL url = new URL(apiUrl);
return (HttpURLConnection) url.openConnection();
} catch (MalformedURLException e) {
throw new RuntimeException("API URL์ด ์๋ชป๋์์ต๋๋ค. : " + apiUrl, e);
} catch (IOException e) {
throw new RuntimeException("์ฐ๊ฒฐ์ด ์คํจํ์ต๋๋ค. : " + apiUrl, e);
}
}
private static String readBody(InputStream body) {
InputStreamReader streamReader = new InputStreamReader(body);
try (BufferedReader lineReader = new BufferedReader(streamReader)) {
StringBuilder responseBody = new StringBuilder();
String line;
while ((line = lineReader.readLine()) != null) {
responseBody.append(line);
}
return responseBody.toString();
} catch (IOException e) {
throw new RuntimeException("API ์๋ต์ ์ฝ๋ ๋ฐ ์คํจํ์ต๋๋ค.", e);
}
}
}
<a class="word" th:href="@{/media/news?searchText=์ ๊ธฐ์ฐจ}">์ ๊ธฐ์ฐจ ๋ด์ค</a>
์์ ๊ฐ์ด ์ค์ ํ๋ฉด ์ ๊ธฐ์ฐจ ํค์๋ ๊ด๋ จ ๋ด์ค๋ฅผ ์ถ๋ ฅํ ์ ์์ต๋๋ค.
'IT > Java - Spring Boot' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Spring Boot] ์นด์นด์ค ์ง๋ api ํ์ฉํ์ฌ ์ง๋ ์ถ๋ ฅํ๊ธฐ (1) | 2023.11.01 |
---|---|
[Spring Boot] ํ์ด์ง์ ์๋จ ๋ฐ ์ฝ๋ฉํ๊ธฐ (0) | 2023.11.01 |
[Spring Boot] ํ์ด์ง์ ์ฌ์ด๋ ๋ฐ ์ฝ๋ฉํ๊ธฐ (0) | 2023.11.01 |
[Spring Boot] ํ์ด์ง์ ํ์ ์ฐฝ ๋์ฐ๊ธฐ (0) | 2023.11.01 |
[HTML/CSS/JS] codepen ํ์ด์ง์ ๋ฒ๊ฐ ํจ๊ณผ ๊ตฌํํ๊ธฐ (2) | 2023.10.30 |