<순서>
1. application.yml에 naver key 등록
2. 지역검색, 응답 & 이미지검색, 응답 클래스 작성
3. naver client 정보를 이요하여 검색 기능 구현
4. 검색 기능 테스트
*application.yml
application.yml에 아래의 코드를 등록한다. 보통 key값이나 개인정보가 있는 경우에 따로 관리한다.
naver:
url:
search:
local: ...
image: ...
client:
id: ...
secret: ...
*SearchLocalReq.java / SearchLocalRes.java
지역검색 요청과 응답에 대한 dto를 따로 만든다.
@Data
@NoArgsConstructor
@AllArgsConstructor
public class SearchLocalReq {
private String query = "";
private int delay = 1;
private int start = 1;
private String sort = "sim";
public MultiValueMap<String, String> toMultiValueMap() {
var map = new LinkedultiValueMap<String, String>();
map.add("query", query);
map.add("display", String.valueOf(display));
map.add("satrt", String.valueOf(start));
map.add("sort", sort);
return map;
}
}
================================
@Data
@NoArgsConstructor
@AllArgsConstructor
public class SearchLocalRes {
private String lastBuildDate;
private int total;
private int start;
private int display;
private String category;
private List<SerachLocalItem> items;
@Data
@NoArgsConstructor
@AllArgsConstructor
public static class SearchLocalItem {
private String title;
private String link;
private String description;
private String telehpone;
private String address;
private String readAddress;
private int mapx;
private int mapy;
}
}
* SearchImageReq.java / SearchImageRes.java
이미지검색 요청과 응답에 대한 dto를 따로 만든다.
@Data
@NoArgsConstructor
@AllArgsConstructor
public class SearchImageReq {
private String query = "";
private int delay = 1;
private int start = 1;
private String sort = "random";
private String filter = "all";
public MultiValueMap<String, String> toMultiValueMap() {
var map = new LinkedultiValueMap<String, String>();
map.add("query", query);
map.add("display", String.valueOf(display));
map.add("satrt", String.valueOf(start));
map.add("sort", sort);
map.add("filter", filter);
return map;
}
}
================================
@Data
@NoArgsConstructor
@AllArgsConstructor
public class SearchImageRes {
private String lastBuildDate;
private int total;
private int start;
private int display;
private List<SerachImageItem> items;
@Data
@NoArgsConstructor
@AllArgsConstructor
public static class SearchImageItem {
private String title;
private String link;
private String thumnail;
private String sizeheight;
private String sizewidth;
}
}
*NaverClient.java
application.yml에 저장한 계정정보를 @Component로 등록한다.
@Component
public class NaverClinet {
@Value("${naver.client.id}");
private String naverClinetId;
@Value("${naver.client.secret}");
private Stirng naverClinetSecret;
@Value("${naver.client.local}");
private Sting naverLocalSearchUrl;
@Value("${naver.client.image}");
private String naverImageSearchUrl;
public SearchLocalRes localSearch(SearchLocalReq searchLocalReq) {
var uri = UriComponentsBuilder.fromUriString(naverLocalSearchUrl)
.queryParams(serachLocalReq.toMultiValueMap())
.build()
.encode()
.toUri();
var headers = new HttpHeaders();
headers.set("X-Naver-Client-Id", naverClientId);
headers.set("X-NAVER-Client-Secret", naverClinetSecret);
headers.setContentType(MediaType.APPLICATION_JSON);
var httpEntity = new HttpEntity<>(headers);
var responseType = new ParameterTypeReference<SearchImageRes>(){};
var responseEntity = new RestTemplate().exchange(
uri, HttpMethod.GET, httpEntity, responseType
);
return responseEntity.getBody();
}
public SearchImageRes imageSearch(SearchImageReq searchImageReq) {
var uri = UriComponentsBuilder.fromUriString(naverImageSearchUrl)
.queryParams(serachImageReq.toMultiValueMap())
.build()
.encode()
.toUri();
var headers = new HttpHeaders();
headers.set("X-Naver-Client-Id", naverClientId);
headers.set("X-NAVER-Client-Secret", naverClinetSecret);
headers.setContentType(MediaType.APPLICATION_JSON);
var httpEntity = new HttpEntity<>(headers);
var responseType = new ParameterTypeReference<SearchImageRes>(){};
var responseEntity = new RestTemplate().exchange(
uri, HttpMethod.GET, httpEntity, responseType
);
return responseEntity.getBody();
}
}
*NaverClientTest.java
NaverClient의 검색 메서드를 테스트하는데, query만 설정해주면 된다.
@SpringBootTest
public class NaverClientTest {
@Autowired
private NaverClient naverClient;
@Test
public void searchLocalTest() {
var search = new SearchLocalReq();
search.setQuery("갈비집");
var result = naverClient.searchLocal(search);
System.out.println(result);
}
@Test
public void searchImageTest() {
var search = new SearchImageReq();
search.setQuery("갈비집");
var result = naverClient.searchImage(search);
System.out.println(result);
}
}
반응형
'Spring' 카테고리의 다른 글
네이버 지역검색 API를 활용한 맛집 List 제작 - (4) (0) | 2021.08.29 |
---|---|
네이버 지역검색 API를 활용한 맛집 List 제작 - (3) (0) | 2021.08.22 |
네이버 지역검색 API를 활용한 맛집 List 제작 - (1) (0) | 2021.08.22 |
Swagger (0) | 2021.08.21 |
@Valid를 위한 Blank, Empty, Null 차이 (0) | 2021.05.13 |