본문 바로가기

SpringBoot/스파르타 웹개발의 봄 spring

[스파르타 웹개발의 봄 spring] 04.04 자바로 네이버 쇼핑 API 이용하기

728x90
반응형

##ARC 에서 자바 코드 확인하기

1. get 요청을 한 뒤 응답이 성공하면

2.code에서 java 코드를 복사해서 클래스 메서드에 붙여넣기

 

## 인텔리제이  세팅

1. 패키지

-spring web

-h2

-mysql

-jpa

-lombok

2.setting

-auto import검색 - Always, Add unambiguous imports on the fly 선택

-compiler검색 - annotation processors - enable annotaion processing 선택

 

 

##

- - 네이버 쇼핑 검색 클래스

- 실행해서 응답 확인

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package com.sparta_spring.sparta_spring_week04.utils;
 
import org.springframework.http.*;
import org.springframework.web.client.RestTemplate;
 
public class NaverShopSearch {
    public String search() {
        // ARC에서 가져온 네이버 api GET 코드
        RestTemplate rest = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.add("X-Naver-Client-Id""발급받은 아이디");
        headers.add("X-Naver-Client-Secret""발급받은 비밀번호");
        String body = "";
 
        HttpEntity<String> requestEntity = new HttpEntity<String>(body, headers);
        // 밑의 코드에서 호출 url 변수에 검색어나 여러 변수 설정 가능
        ResponseEntity<String> responseEntity = rest.exchange("https://openapi.naver.com/v1/search/shop.json?query=iphone", HttpMethod.GET, requestEntity, String.class);
        HttpStatus httpStatus = responseEntity.getStatusCode();
        int status = httpStatus.value();
        String response = responseEntity.getBody();
        System.out.println("Response status: " + status);
        System.out.println(response);
 
        return response;
    }
 
    public static void main(String[] args) {
        NaverShopSearch naverShopSearch = new NaverShopSearch();
        naverShopSearch.search();
    }
}
 
cs
반응형