From 48da30af9f4cbdc9f3ee8334659e3784244c9517 Mon Sep 17 00:00:00 2001 From: joonghyun Date: Mon, 3 Jun 2024 13:59:48 +0900 Subject: [PATCH 1/2] =?UTF-8?q?#90=20fix:=20Json=20=EC=97=AD=EC=A7=81?= =?UTF-8?q?=EB=A0=AC=ED=99=94=20=EA=B3=BC=EC=A0=95=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../puzzle/application/PuzzleService.java | 27 ++++++++++--------- 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/src/main/java/com/lesso/neverland/puzzle/application/PuzzleService.java b/src/main/java/com/lesso/neverland/puzzle/application/PuzzleService.java index 78aab5b..60b3a18 100644 --- a/src/main/java/com/lesso/neverland/puzzle/application/PuzzleService.java +++ b/src/main/java/com/lesso/neverland/puzzle/application/PuzzleService.java @@ -1,5 +1,8 @@ package com.lesso.neverland.puzzle.application; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; import com.lesso.neverland.album.domain.Album; import com.lesso.neverland.album.repository.AlbumRepository; import com.lesso.neverland.common.base.BaseException; @@ -31,7 +34,6 @@ import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; -import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.client.RestTemplate; @@ -131,7 +133,7 @@ public BaseResponse createPuzzle(Long groupIdx, MultipartFile image, Cre Team group = groupRepository.findById(groupIdx).orElseThrow(() -> new BaseException(INVALID_GROUP_IDX)); User writer = userRepository.findById(userService.getUserIdxWithValidation()).orElseThrow(() -> new BaseException(INVALID_USER_IDX)); - String imagePath = imageService.uploadImage("group", image); + String imagePath = imageService.uploadImage("puzzle", image); LocalDate puzzleDate = convertToLocalDate(createPuzzleRequest.puzzleDate()); Puzzle newPuzzle = createPuzzle(createPuzzleRequest, group, writer, imagePath, puzzleDate); @@ -163,7 +165,7 @@ private static LocalDate convertToLocalDate(String date) { } // puzzle entity 생성 - private Puzzle createPuzzle(CreatePuzzleRequest createPuzzleRequest, Team group, User writer, String imagePath, LocalDate puzzleDate) { + private Puzzle createPuzzle(CreatePuzzleRequest createPuzzleRequest, Team group, User writer, String imagePath, LocalDate puzzleDate) throws JsonProcessingException { Puzzle puzzle = Puzzle.builder() .user(writer) .team(group) @@ -176,23 +178,24 @@ private Puzzle createPuzzle(CreatePuzzleRequest createPuzzleRequest, Team group, return puzzle; } - private PuzzleLocation convertAddressToCoordinates(String address) { + private PuzzleLocation convertAddressToCoordinates(String address) throws JsonProcessingException { RestTemplate restTemplate = new RestTemplate(); HttpHeaders headers = new HttpHeaders(); headers.set("Authorization", "KakaoAK " + API_KEY); - HttpEntity entity = new HttpEntity<>("parameters", headers); + HttpEntity entity = new HttpEntity<>(headers); String url = API_URL + address; - ResponseEntity response = restTemplate.exchange(url, HttpMethod.GET, entity, KakaoApiResponse.class); - KakaoApiResponse responseBody = response.getBody(); + String responseBody = restTemplate.exchange(url, HttpMethod.GET, entity, String.class).getBody(); + + ObjectMapper objectMapper = new ObjectMapper(); + JsonNode jsonNode = objectMapper.readTree(responseBody); + JsonNode firstDocument = jsonNode.path("documents").get(0); + String x = firstDocument.path("x").asText(); + String y = firstDocument.path("y").asText(); PuzzleLocation puzzleLocation = null; - if (responseBody != null && responseBody.documents().length > 0) { - String x = responseBody.documents()[0].x(); - String y = responseBody.documents()[0].y(); - puzzleLocation = new PuzzleLocation(address, x, y); - } + puzzleLocation = new PuzzleLocation(address, x, y); return puzzleLocation; } From 92d9b020067c5110d2fd2f67680a7ca2690801c1 Mon Sep 17 00:00:00 2001 From: joonghyun Date: Mon, 3 Jun 2024 14:06:28 +0900 Subject: [PATCH 2/2] =?UTF-8?q?#90=20chore:=20=EB=B6=88=ED=95=84=EC=9A=94?= =?UTF-8?q?=ED=95=9C=20=EC=BD=94=EB=93=9C=20=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../lesso/neverland/puzzle/application/PuzzleService.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/lesso/neverland/puzzle/application/PuzzleService.java b/src/main/java/com/lesso/neverland/puzzle/application/PuzzleService.java index 60b3a18..fba6a7b 100644 --- a/src/main/java/com/lesso/neverland/puzzle/application/PuzzleService.java +++ b/src/main/java/com/lesso/neverland/puzzle/application/PuzzleService.java @@ -178,6 +178,7 @@ private Puzzle createPuzzle(CreatePuzzleRequest createPuzzleRequest, Team group, return puzzle; } + // String address값을 KakaoMap API를 활용해 x,y 좌표로 변환 private PuzzleLocation convertAddressToCoordinates(String address) throws JsonProcessingException { RestTemplate restTemplate = new RestTemplate(); @@ -194,9 +195,7 @@ private PuzzleLocation convertAddressToCoordinates(String address) throws JsonPr String x = firstDocument.path("x").asText(); String y = firstDocument.path("y").asText(); - PuzzleLocation puzzleLocation = null; - puzzleLocation = new PuzzleLocation(address, x, y); - return puzzleLocation; + return new PuzzleLocation(address, x, y); } // [작성자] 퍼즐 수정