Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- JoinColumn
- json
- Spring Spring boot 차이
- REST API 규칙
- uncheck Exception
- jpa회원가입
- 스프링 부트 기능
- git
- REST란
- @IdClass
- json gson 차이
- ERD 작성
- 최종 프로젝트
- 인텔리제이
- 빈생성안됨
- queryDSL
- 스프링 부트 공식 문서
- 스프링부트오류
- Unsatisfied dependency
- JPA
- jwt메서드
- 1차캐시
- github
- JPA주의사항
- Error creating bean with name
- 복합키
- Q 클래스
- Filter
- spring서버
- jpa에러
Archives
- Today
- Total
Everyday Dev System
Client 서버에서 네이버 검색 API 활용 본문
네이버 검색 API 활용
https://developers.naver.com/products/intro/plan/ 에서 애플리케이션 등록.
API 사용 설명 관련된 사이트
https://developers.naver.com/docs/serviceapi/search/shopping/shopping.md#%EC%87%BC%ED%95%91
Postman에서 요청하기
URI : https://openapi.naver.com/v1/search/shop.json?query=macbook
Client 서버에 코드 추가해서 요청하기
Code:
더보기
[naver] - [controller]
package com.sparta.springresttemplateclient1.naver.controller;
import com.sparta.springresttemplateclient1.naver.dto.ItemDto;
import com.sparta.springresttemplateclient1.naver.service.NaverApiService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("/api")
public class NaverApiController {
private final NaverApiService naverApiService;
public NaverApiController(NaverApiService naverApiService) {
this.naverApiService = naverApiService;
}
@GetMapping("/search")
public List<ItemDto> searchItems(@RequestParam String query) {
return naverApiService.searchItems(query);
}
}
[naver] - [dto]
package com.sparta.springresttemplateclient1.naver.dto;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.json.JSONObject;
@Getter
@NoArgsConstructor
public class ItemDto {
private String title;
private String link;
private String image;
private int lprice;
public ItemDto(JSONObject itemJson) {
this.title = itemJson.getString("title");
this.link = itemJson.getString("link");
this.image = itemJson.getString("image");
this.lprice = itemJson.getInt("lprice");
}
}
[naver] - [service]
package com.sparta.springresttemplateclient1.naver.service;
import com.sparta.springresttemplateclient1.naver.dto.ItemDto;
import lombok.extern.slf4j.Slf4j;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
@Slf4j(topic = "NAVER API")
@Service
public class NaverApiService {
private final RestTemplate restTemplate;
public NaverApiService(RestTemplateBuilder builder) {
this.restTemplate = builder.build();
}
public List<ItemDto> searchItems(String query) {
// 요청 URL 만들기
URI uri = UriComponentsBuilder
.fromUriString("https://openapi.naver.com")
.path("/v1/search/shop.json")
.queryParam("display", 15)
.queryParam("query", query)
.encode()
.build()
.toUri();
log.info("uri = " + uri);
RequestEntity<Void> requestEntity = RequestEntity
.get(uri)
.header("X-Naver-Client-Id", "Q_vSbTuzKLqzlwTTKYBh")
.header("X-Naver-Client-Secret", "fSWtmQocFu")
.build();
ResponseEntity<String> responseEntity = restTemplate.exchange(requestEntity, String.class);
log.info("NAVER API Status Code : " + responseEntity.getStatusCode());
return fromJSONtoItems(responseEntity.getBody());
}
public List<ItemDto> fromJSONtoItems(String responseEntity) {
JSONObject jsonObject = new JSONObject(responseEntity);
JSONArray items = jsonObject.getJSONArray("items");
List<ItemDto> itemDtoList = new ArrayList<>();
for (Object item : items) {
ItemDto itemDto = new ItemDto((JSONObject) item);
itemDtoList.add(itemDto);
}
return itemDtoList;
}
}
해당 메서드에 매핑된다.
@GetMapping("/search")
public List<ItemDto> searchItems(@RequestParam String query) {
return naverApiService.searchItems(query);
}
아래와 같이 요청 url을 생성하고, header에 네이버 open API id, secret key 값을 입력한다.
URI uri = UriComponentsBuilder
.fromUriString("https://openapi.naver.com")
.path("/v1/search/shop.json")
.queryParam("display", 15)
.queryParam("query", query)
.encode()
.build()
.toUri();
log.info("uri = " + uri);
RequestEntity<Void> requestEntity = RequestEntity
.get(uri)
.header("X-Naver-Client-Id", "Q_vSbTuzKLqzlwTTKYBh")
.header("X-Naver-Client-Secret", "fSWtmQocFu")
.build();
결과 :
'내배캠 주요 학습 > Spring 숙련' 카테고리의 다른 글
@OneToOne 외래키 활용 (1) | 2023.06.21 |
---|---|
Entity 연관관계 (0) | 2023.06.20 |
RestTemplate - Post 방식 : Client, Server 코드 설명 (0) | 2023.06.20 |
RestTemplate - get방식 : Server 코드 설명 (0) | 2023.06.20 |
RestTemplate - get방식 : Client 서버 코드 설명 (0) | 2023.06.20 |