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 |
29 | 30 | 31 |
Tags
- 파이썬
- MSA
- JVM
- 백준 16719
- 백준 19238
- spring oauth
- JPA
- 백준 16236
- 프로그래머스
- 프로래머스
- spring security
- 백준 16235
- Spring
- java
- 백준 파이썬
- 백준 17626
- 웹어플리케이션 서버
- MySQL
- 백준 15685
- java 기술면접
- spring cloud
- sql 기술면접
- Coroutine
- 백준 17779
- with recursive
- 백준
- springboot
- Spring Boot
- re.split
- Kotlin
Archives
- Today
- Total
시작이 반
[Spring] 네이버 검색 API 사용 본문
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
'Programming > Spring' 카테고리의 다른 글
[Spring] RestTemplate (Content-Type에러) (2) | 2021.02.01 |
---|---|
[Spring] @Controller @Service @Repository (0) | 2021.01.29 |
[Spring] @RequestParam, @PathVariable (0) | 2021.01.25 |
[Spring] @Controller @RestController (0) | 2021.01.25 |
[Spring] @RequestMapping- @PostMapping @GetMapping (0) | 2021.01.25 |