[TIL] 자바로 네이버 쇼핑 API 이용

김시원·2022년 11월 14일
0

TIL

목록 보기
16/30
public class NaverShopSearch {
    public String search() {
        RestTemplate rest = new RestTemplate(); //기본적으로 spring에서 restTemplate이란걸 씀.
        HttpHeaders headers = new HttpHeaders(); //header 클래스도 만듦
        headers.add("X-Naver-Client-Id", "여러분이 발급받은 Client ID");
        headers.add("X-Naver-Client-Secret", "여러분이 발급받은 Client Secret"); // header 값 넣어줌
        String body = ""; //body 라는 string 하나 생성

        HttpEntity<String> requestEntity = new HttpEntity<String>(body, headers); // requestEntity 라는 곳에 응답 저장.
        ResponseEntity<String> responseEntity = rest.exchange("https://openapi.naver.com/v1/search/shop.json?query=adidas", HttpMethod.GET, requestEntity, String.class);
        HttpStatus httpStatus = responseEntity.getStatusCode();
        int status = httpStatus.value(); //httpStauts는 200숫자
        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();
    }
}
profile
IT 개발자 시원

0개의 댓글

관련 채용 정보