Loading the catalog…
Loading the catalog…
05 · Naver Open API 💡 한 줄 요약 : 네이버 쇼핑 검색 결과를 서버에서 받아 우리 서비스의 상품 검색 데이터로 변환합니다. 호출 흐름 브라우저가 우리 서버의 /api/search 에 검색어를 보냅니다. 우리 서버가 네이버 검색 API용 URI를 만듭니다. Client ID·Client Secret을 요청 헤더에 넣습니다. 네이버가 반환한 JSON에서 items 배열을 읽습니다. 필요한 상품 정보를 ItemDto로 변환해 브라우저에 반환합니다. 인증 설정과 요청 naver.client.id=${NAVER_CLIENT_ID} naver.client.secret=${NAVER_CLIENT_SECRET} @Value("${naver.client.id}") private String clientId; @Value("${naver.client.secret}") private String clientSecret; public List<ItemDto> searchItems(String query) { URI uri = UriComponentsBuilder .fromUriString("https://openapi.naver.com") .path("/v1/search/shop.json") .queryParam("display", 15) .queryParam("query", query) .build().encode().toUri(); RequestEntity<Void> request = RequestEntity.get(uri) .header("X-Naver-Client-Id", clientId) .header("X-Naver-Client-Secret", clientSecret) .build(); ResponseEntity<String> response = restTemplate.exchange(request, String.class); return fromJSONtoItems(response.getBody()); } display=15 는 이번 실습에서 요청하는 검색 결과 개수입니다. API 키는 서버 설정에서 사용하고 브라우저에 전달하지 않습니다. ItemDto로 변환할 데이터 필드 의미 title 검색된 상품명 link 상품 상세 페이지 주소 image 이미지 주소 lprice 검색 응답의 최저가 public List<ItemDto> fromJSONtoItems(String body) { if (body == null) { throw new IllegalStateException("검색 응답 본문이 없습니다."); } JSONObject json = new JSONObject(body); JSONArray items = json.getJSONArray("items"); List<ItemDto> result = new ArrayList<>(); for (int i = 0; i < items.length(); i++) { result.add(new ItemDto(items.getJSONObject(i))); } return result; } DTO 생성자에서 외부 JSON의 필요한 필드를 읽습니다. 외부 응답을 그대로 모든 계층에 흘려보내기보다 서비스에서 쓸 형태로 정리합니다. 검색과 DB 저장 구분 검색 결과를 받는 것만으로 Product가 DB에 저장되지는 않습니다. 사용자가 관심상품으로 선택할 때 별도 등록 API를 호출합니다. 인증 오류·잘못된 요청·응답 없음·외부 장애를 정상적인 검색 결과와 구분합니다. RestTemplate의 기본 오류 처리에서는 4xx·5xx 응답이 예외로 처리될 수 있습니다. 복습 체크 브라우저 요청과 외부 API 요청의 URL을 구분할 수 있습니다. Client ID·Client Secret을 서버에서 헤더로 전송할 수 있습니다. items 배열을 서비스 DTO 목록으로 변환할 수 있습니다. 상품 검색과 관심상품 저장이 별도 처리임을 설명할 수 있습니다.
What RADAR observed and classified to build this opportunity. It is what the source published, not a verification that the offer is still active.
Spring 숙련 Chapter 2 (2) - 외부 서비스 연동. 05 · Naver Open API 💡 한 줄 요약 : 네이버 쇼핑 검색 결과를 서버에서 받아 우리 서비스의 상품 검색 데이터로 변환합니다. 호출 흐름 브라우저가 우리 서버의 /api/search 에 검색어를 보냅니다. 우리 서버가 네이버 검색 API용 URI를 만듭니다. Client ID·Client Secret을 요청 헤더에 넣습니다. 네이버가 반환한 JSON에서 items 배열을 읽습니다. 필요한 상품 정보를 ItemDto로 변환해 브라우저에 반환합니다. 인증 설정과 요청 naver.client.id=${NAVER_CLIENT_ID} naver.client.secret=${NAVER_CLIENT_SECRET}…