Skip to content

Commit

Permalink
feat: book search api
Browse files Browse the repository at this point in the history
책 정보 가져오는 API 기능 구현 (XML -> JSON)
book search api #1
  • Loading branch information
Relaxed-Mind committed Nov 4, 2023
1 parent 4452464 commit 1f385d0
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 0 deletions.
3 changes: 3 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/capstone/bookdiary/config/RestTemplateConfig.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
22 changes: 22 additions & 0 deletions src/main/java/capstone/bookdiary/controller/BookController.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
37 changes: 37 additions & 0 deletions src/main/java/capstone/bookdiary/service/BookService.java
Original file line number Diff line number Diff line change
@@ -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<String> entity = new HttpEntity<>(headers);

ResponseEntity<String> exchange = restTemplate.exchange(bookSearchApiUrl, HttpMethod.GET, entity, String.class);
XmlToJson.xmlToJson(exchange.getBody());

}
//TODO: 도서 세부 정보 외부 api 요청
}
21 changes: 21 additions & 0 deletions src/main/java/capstone/util/XmlToJson.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
}

0 comments on commit 1f385d0

Please sign in to comment.