시작이 반

[Spring] 네이버 검색 API 사용 본문

Programming/Spring

[Spring] 네이버 검색 API 사용

G_Gi 2021. 1. 25. 20:24
SMALL

책 검색 API를 사용해볼것

 

RestTemplate를 스프링 빈으로 등록

package com.mkl.book.Configuration;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

@Configuration
public class Config {

    @Bean
    public RestTemplate restTemplate(){
        return  new RestTemplate();
    }

}

 

 

@PathVariable 를 사용해서 인자값을 가져왔음 

자기 코드에 맞게 바꾸면됨

package com.mkl.book.booktest;

import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequiredArgsConstructor
public class BookController {

    private final BookApiClient bookApiClient;

    @GetMapping("/book/{keyword}")
    public BooksResponseDto goBookPage(@PathVariable("keyword") String keyword){
        return bookApiClient.requestBook(keyword);
    }
}
​

 

타이틀로 검색하는 코드

package com.mkl.book.booktest;

import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
@RequiredArgsConstructor
public class BookApiClient {
    private final RestTemplate restTemplate;

    private final String CLIENT_ID = //ID
    private final String CLIENT_SECRET = //key

    private  final String OpenNaverBookUrl_getBooks = "https://openapi.naver.com/v1/search/book_adv?d_titl={keyword}";

    public BooksResponseDto requestBook(String keyword){
        final HttpHeaders headers = new HttpHeaders();
        headers.set("X-Naver-Client-Id", CLIENT_ID);
        headers.set("X-Naver-Client-Secret", CLIENT_SECRET);

        final HttpEntity<String> entity = new HttpEntity<>(headers);

        return restTemplate.exchange(OpenNaverBookUrl_getBooks, HttpMethod.GET, entity, BooksResponseDto.class, keyword).getBody();
    }

}

 

 

API에서 가져온 정보를 담을 객체

developers.naver.com/docs/search/book/

출력결과 확인

임의로 제목과 저자명만 출력

package com.mkl.book.booktest;

import lombok.Data;

@Data
public class BooksResponseDto {
    private int display;
    private Item[] items;

    @Data
    static class Item{
        private String title;	//책 제목
        private String author;  //저자명

    }

}

 

결과화면

 

 


검색 Form만들어서 사용

booktest.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>책 검색</h1>

<div class="container">
    <form action="/booksearch/search" method="post">
        <div class="form-group">
            <input type="text" name="keyword" placeholder="검색어를 입력하세요">
        </div>
        <button type="submit" onclick="http://localhost:8080/booksearch/search" >검색</button>
    </form>
</div>

</body>
</html>

 

package com.mkl.book.booktest;

import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class BookForm {
    private String keyword;
}

 

package com.mkl.book.booktest;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@RequiredArgsConstructor
@Controller
public class BookController {

    private final BookApiClient bookApiClient;

    @GetMapping("/booksearch")
    public String goBookSearch(){
        return "booktest";
    }

    @PostMapping("/booksearch/search")
    @ResponseBody
    public BooksResponseDto search(BookForm form){
        return bookApiClient.requestBook(form.getKeyword());
    }
}

 

결과 화면

이런식인듯....?

LIST