|
2 | 2 |
|
3 | 3 | import java.util.List; |
4 | 4 |
|
| 5 | +import org.springframework.beans.factory.annotation.Value; |
| 6 | +import org.springframework.http.HttpEntity; |
| 7 | +import org.springframework.http.HttpHeaders; |
| 8 | +import org.springframework.http.HttpMethod; |
5 | 9 | import org.springframework.http.ResponseEntity; |
| 10 | +import org.springframework.security.core.annotation.AuthenticationPrincipal; |
6 | 11 | import org.springframework.web.bind.annotation.GetMapping; |
| 12 | +import org.springframework.web.bind.annotation.ModelAttribute; |
7 | 13 | import org.springframework.web.bind.annotation.RequestMapping; |
8 | 14 | import org.springframework.web.bind.annotation.RequestParam; |
9 | 15 | import org.springframework.web.bind.annotation.RestController; |
| 16 | +import org.springframework.web.client.RestTemplate; |
| 17 | +import org.springframework.web.util.UriComponentsBuilder; |
10 | 18 |
|
| 19 | +import com.example.log4u.common.oauth2.dto.CustomOAuth2User; |
| 20 | +import com.example.log4u.domain.map.dto.MyLocationRequestDto; |
| 21 | +import com.example.log4u.domain.map.dto.ReverseGeocodingResponseDto; |
11 | 22 | import com.example.log4u.domain.map.dto.response.DiaryClusterResponseDto; |
12 | 23 | import com.example.log4u.domain.map.dto.response.DiaryMarkerResponseDto; |
13 | 24 | import com.example.log4u.domain.map.service.MapService; |
14 | 25 |
|
15 | 26 | import io.swagger.v3.oas.annotations.tags.Tag; |
16 | 27 | import lombok.RequiredArgsConstructor; |
| 28 | +import lombok.extern.slf4j.Slf4j; |
17 | 29 |
|
18 | 30 | @Tag(name = "지도 API") |
19 | 31 | @RestController |
20 | 32 | @RequiredArgsConstructor |
21 | 33 | @RequestMapping("/maps") |
| 34 | +@Slf4j |
22 | 35 | public class MapController { |
23 | 36 |
|
24 | 37 | private final MapService mapService; |
25 | 38 |
|
| 39 | + @Value("${naver.api.client-id}") |
| 40 | + private String clientId; |
| 41 | + |
| 42 | + @Value("${naver.api.client-secret}") |
| 43 | + private String secret; |
| 44 | + |
| 45 | + private final RestTemplate restTemplate; |
| 46 | + |
| 47 | + @GetMapping("/location") |
| 48 | + public ResponseEntity<ReverseGeocodingResponseDto> getMyLocation( |
| 49 | + @AuthenticationPrincipal CustomOAuth2User customOAuth2User, |
| 50 | + @ModelAttribute MyLocationRequestDto request |
| 51 | + ) { |
| 52 | + |
| 53 | + String naverMapsUrl = UriComponentsBuilder |
| 54 | + .fromHttpUrl("https://maps.apigw.ntruss.com/map-reversegeocode/v2/gc") |
| 55 | + .queryParam("coords", request.coords()) |
| 56 | + .queryParam("output", request.output()) |
| 57 | + .queryParam("orders", "legalcode,admcode,addr,roadaddr") |
| 58 | + .toUriString(); |
| 59 | + |
| 60 | + HttpHeaders headers = new HttpHeaders(); |
| 61 | + headers.set("x-ncp-apigw-api-key-id", clientId); |
| 62 | + headers.set("x-ncp-apigw-api-key", secret); |
| 63 | + |
| 64 | + HttpEntity<Void> entity = new HttpEntity<>(headers); |
| 65 | + |
| 66 | + ResponseEntity<ReverseGeocodingResponseDto> response = restTemplate.exchange( |
| 67 | + naverMapsUrl, |
| 68 | + HttpMethod.GET, |
| 69 | + entity, |
| 70 | + ReverseGeocodingResponseDto.class |
| 71 | + ); |
| 72 | + |
| 73 | + log.debug("역 지오코딩 결과 : " + String.valueOf(response) + "\n"); |
| 74 | + return ResponseEntity.ok(response.getBody()); |
| 75 | + } |
| 76 | + |
26 | 77 | @GetMapping("/diaries/cluster") |
27 | 78 | public ResponseEntity<List<DiaryClusterResponseDto>> getDiaryClusters( |
28 | 79 | @RequestParam double south, |
|
0 commit comments