From 1f385d0911b7df26652856685ef58a4e62c18979 Mon Sep 17 00:00:00 2001 From: Relaxed-Mind Date: Sat, 4 Nov 2023 16:59:06 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20book=20search=20api=20=EC=B1=85=20?= =?UTF-8?q?=EC=A0=95=EB=B3=B4=20=EA=B0=80=EC=A0=B8=EC=98=A4=EB=8A=94=20API?= =?UTF-8?q?=20=EA=B8=B0=EB=8A=A5=20=EA=B5=AC=ED=98=84=20(XML=20->=20JSON)?= =?UTF-8?q?=20book=20search=20api=20#1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle | 3 ++ .../bookdiary/config/RestTemplateConfig.java | 13 +++++++ .../bookdiary/controller/BookController.java | 22 +++++++++++ .../bookdiary/service/BookService.java | 37 +++++++++++++++++++ src/main/java/capstone/util/XmlToJson.java | 21 +++++++++++ 5 files changed, 96 insertions(+) create mode 100644 src/main/java/capstone/bookdiary/config/RestTemplateConfig.java create mode 100644 src/main/java/capstone/bookdiary/controller/BookController.java create mode 100644 src/main/java/capstone/bookdiary/service/BookService.java create mode 100644 src/main/java/capstone/util/XmlToJson.java diff --git a/build.gradle b/build.gradle index ddc1a09..0fabf95 100644 --- a/build.gradle +++ b/build.gradle @@ -38,6 +38,9 @@ dependencies { //annotationProcessor "com.querydsl:querydsl-apt:5.0.0:jakarta" annotationProcessor "jakarta.annotation:jakarta.annotation-api" annotationProcessor "jakarta.persistence:jakarta.persistence-api" + + //json in java + implementation group: 'org.json', name: 'json', version: '20230227' } tasks.named('test') { diff --git a/src/main/java/capstone/bookdiary/config/RestTemplateConfig.java b/src/main/java/capstone/bookdiary/config/RestTemplateConfig.java new file mode 100644 index 0000000..a4a2a60 --- /dev/null +++ b/src/main/java/capstone/bookdiary/config/RestTemplateConfig.java @@ -0,0 +1,13 @@ +package capstone.bookdiary.config; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.client.RestTemplate; + +@Configuration +public class RestTemplateConfig { + @Bean + public RestTemplate restTemplate() { + return new RestTemplate(); + } +} diff --git a/src/main/java/capstone/bookdiary/controller/BookController.java b/src/main/java/capstone/bookdiary/controller/BookController.java new file mode 100644 index 0000000..1d4e9e6 --- /dev/null +++ b/src/main/java/capstone/bookdiary/controller/BookController.java @@ -0,0 +1,22 @@ +package capstone.bookdiary.controller; + +import capstone.bookdiary.service.BookService; +import lombok.RequiredArgsConstructor; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequestMapping("/api") +@RequiredArgsConstructor + +public class BookController { + private final BookService bookService; + + + @GetMapping("/search/{title}") + public void searchBook(@PathVariable String title){ + bookService.searchBook(title); + } +} diff --git a/src/main/java/capstone/bookdiary/service/BookService.java b/src/main/java/capstone/bookdiary/service/BookService.java new file mode 100644 index 0000000..3adeffa --- /dev/null +++ b/src/main/java/capstone/bookdiary/service/BookService.java @@ -0,0 +1,37 @@ +package capstone.bookdiary.service; + +import capstone.util.XmlToJson; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.http.HttpEntity; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpMethod; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; +import org.springframework.stereotype.Service; +import org.springframework.web.client.RestTemplate; + +@Service +public class BookService { + @Value("${api-key.library}") + private String libraryKey; + @Autowired + private RestTemplate restTemplate; + + + //TODO: 도서 검색 외부 api 요청 + + public void searchBook(String title){ + String bookSearchApiUrl = "http://data4library.kr/api/srchBooks?authKey="+libraryKey+"&title="+title; + + HttpHeaders headers = new HttpHeaders(); + headers.setContentType(MediaType.APPLICATION_JSON); + + HttpEntity entity = new HttpEntity<>(headers); + + ResponseEntity exchange = restTemplate.exchange(bookSearchApiUrl, HttpMethod.GET, entity, String.class); + XmlToJson.xmlToJson(exchange.getBody()); + + } + //TODO: 도서 세부 정보 외부 api 요청 +} diff --git a/src/main/java/capstone/util/XmlToJson.java b/src/main/java/capstone/util/XmlToJson.java new file mode 100644 index 0000000..76241c7 --- /dev/null +++ b/src/main/java/capstone/util/XmlToJson.java @@ -0,0 +1,21 @@ +package capstone.util; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.SerializationFeature; +import org.json.JSONObject; +import org.json.XML; + +public class XmlToJson { + public static void xmlToJson(String xmlString){ + try{ + JSONObject jsonObject = XML.toJSONObject(xmlString); + ObjectMapper mapper = new ObjectMapper(); + mapper.enable(SerializationFeature.INDENT_OUTPUT); + Object json = mapper.readValue(jsonObject.toString(), Object.class); + String output = mapper.writeValueAsString(json); + System.out.println(output); + }catch (Exception e){ + e.printStackTrace(); + } + } +}