JAVA/SPRING

[NAVER maps ๊ธธ ์ฐพ๊ธฐ] springboot + webClient ๋กœ API ํ˜ธ์ถœ

girin_dev 2022. 9. 2. 00:00
728x90
๋ฐ˜์‘ํ˜•

spring boot / java 11 / react client

 

 

๐Ÿฅจ ๊ฐœ์ธ ํ”„๋กœ์ ํŠธ์—์„œ naver ๊ธธ์ฐพ๊ธฐ api๋ฅผ ์‚ฌ์šฉํ•œ ๊ณผ์ •์„ ์ ์–ด๋‘๋ คํ•œ๋‹ค. 

 

 

๐Ÿฅช ๊ณต์‹ ๋ฌธ์„œ๋ฅผ ๋ณด๋Š” ๊ฒƒ์ด ๊ฐ€์žฅ ์ •ํ™•ํ•˜๋ฏ€๋กœ ๊ณต์‹๋ฌธ์„œ๋ฅผ ์ฐธ๊ณ ํ•˜๋Š”๊ฒŒ ์ œ์ผ ๋ฒ ์ŠคํŠธ์ด๋‹ค.

 

 

 

๐Ÿถ ์„ค์ •๋ถ€ํ„ฐ API ์š”์ฒญ๊นŒ์ง€, API ์š”์ฒญ์—์„œ response data๋ฅผ ๋ฐ›์•„์˜ค๊ธฐ๊นŒ์ง€ ์ •๋ฆฌํ•˜๋ ค ํ•œ๋‹ค. 

 

 

 

 

 

๐Ÿซ ๊ธธ์ฐพ๊ธฐ API๋Š” ์šฐ์„  ๋„ค์ด๋ฒ„ ํด๋ผ์šฐ๋“œ ํ”Œ๋žซํผ์—์„œ ์„œ๋น„์Šค ์‚ฌ์šฉ์„ ์œ„ํ•œ ๋“ฑ๋ก์ด ๋จผ์ € ๋˜์–ด์•ผ ํ•œ๋‹ค. 

 

https://console.ncloud.com  

AI - NAVER API ์•„๋ž˜์˜ maps๋ฅผ ์ด์šฉ ํ•  ๊ฒƒ์ž„.

 

 

**** ์„œ๋น„์Šค ๋“ฑ๋ก์€ ์ƒ๋žต

 

 

ํด๋ผ์ด์–ธํŠธ ์•„์ด๋””์™€ ํด๋ผ์ด์–ธํŠธ ์‹œํฌ๋ฆฟ ํ‚ค๋Š” API ์š”์ฒญ ์‹œ, header ๊ฐ’์— ์žˆ์–ด์•ผ ํ•˜๋Š” ๊ฐ’๋“ค์ด๋‹ค.

 

 

๐Ÿฟ ์ธ์ฆ์„ ์œ„ํ•œ ํ‚ค๊ฐ’์ด๋ฏ€๋กœ, ์œ ์ถœ๋˜์ง€ ์•Š๋„๋ก ์กฐ์‹ฌํ•ด์•ผ ํ•œ๋‹ค.

 

 

 

** ๋“ฑ๋ก์ด ๋˜์—ˆ๋‹ค๋ฉด ์‹ค์ œ API๋ฅผ ์š”์ฒญํ•ด์•ผ ํ•˜๋Š”๋ฐ 

 

์ฒ˜์Œ ์‹œ๋„ ํ•  ๋•Œ์— ํด๋ผ์ด์–ธํŠธ์—์„œ ์ง์ ‘ ์š”์ฒญํ•œ ๊ฐ’์„ ์“ฐ๋ ค ํ–ˆ์œผ๋‚˜, ํ˜„์žฌ๋Š” JS ์š”์ฒญ์„ NAVER ์ธก์—์„œ ๋ง‰์•„๋‘์—ˆ๋‹ค๊ณ  ํ•œ๋‹ค. CORS ์—๋Ÿฌ๊ฐ€ ๋ฆฌํ„ด๋  ๊ฒƒ์ž„...

 

https://developers.naver.com/forum/posts/7609

 

๊ฐœ๋ฐœ์ž ํฌ๋Ÿผ - NAVER Developers

 

developers.naver.com

 

๊ทธ๋ ‡๋‹ค๊ณ ํ•œ๋‹ค. 

 

๐Ÿฑ๐Ÿฑ๐Ÿฑ

๊ทธ๋ž˜์„œ client์—์„œ ๊ฐ€์ ธ์˜จ ์ขŒํ‘œ๊ฐ’์„ java backend์˜ API๋ฅผ ํ˜ธ์ถœ ํ•  ๋•Œ์— ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๋˜์ ธ์„œ 

๊ธธ์ฐพ๊ธฐ ๊ฒฐ๊ณผ ๊ฐ’์„ ๊ฐ€์ ธ์˜ค๋„๋ก ๊ตฌํ˜„ํ–ˆ๋‹ค.

 

์ถœํ‡ด๊ทผ ์‹œ๊ฐ„์ด ์–ผ๋งˆ๋‚˜ ๊ฑธ๋ฆด์ง€ ๊ถ๊ธˆํ•˜๋‹ค. 

 

 

 

์ถœ๋ฐœ์€ : 

  1. x: 126.844856
  2. y: 37.5407361
  3. _lat: 37.5407361
  4. _lng: 126.844856

๋„์ฐฉ์€ : 

  1. x: 126.8980711
  2. y: 37.5763214
  3. _lat: 37.5763214
  4. _lng: 126.8980711

 

์œ„์น˜ ๊ฒฝ๋กœ๋ฅผ ์•Œ์•˜์œผ๋‹ˆ, 

 

 

 

๐Ÿฅค ํด๋ผ์ด์–ธํŠธ์—์„œ ์ ๋‹นํžˆ ์ฐพ์•„์„œ back๋‹จ API๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด ๋œ๋‹ค. 

 

ํ˜ธ์ถœ์€ ์ƒ๋žตํ•˜๊ณ , 

 

 

 

 

 

๐Ÿฅซ springboot ์—์„œ webclient๋ฅผ ์ด์šฉํ•˜์—ฌ,  ๊ธธ์ฐพ๊ธฐ API๋ฅผ ํ…Œ์ŠคํŠธ ํ•ด๋ณธ๋‹ค.

 

PathTest.java

package com.org.ํŒจํ‚ค์ง€ํŒจํ‚ค์ง€

import io.netty.handler.ssl.SslContextBuilder;

import io.netty.handler.ssl.util.InsecureTrustManagerFactory;

import org.springframework.http.client.reactive.ReactorClientHttpConnector;

import org.springframework.web.reactive.function.client.WebClient;

import reactor.netty.http.client.HttpClient;

import javax.net.ssl.SSLException;

class PathTest {

  public static void main(String[] args) {

        //  ์š”์ฒญ ์ „์ฒด ์ฃผ์†Œ ์˜ˆ์‹œ, : https:/naveropenapi.apigw.ntruss.com/map-direction/v1/driving?start=126.8990034,27.1234&goal=126.8990034,27.224

        String uriPath = "https://naveropenapi.apigw.ntruss.com/map-direction/v1/driving";
      
      	String start = "126.844856,37.5407361";
        
     	String goal = "126.8980711,37.5763214";
        
        String option = "trafast";

        HttpClient httpClient = HttpClient.create().secure(t -> {
        
            try {
            
            	t.sslContext(SslContextBuilder.forClient().trustManager(InsecureTrustManagerFactory.INSTANCE).build());
            
            } catch (SSLException e) {
            
                System.out.println(e);
            
            }
        });

        WebClient client = WebClient.builder()
                .baseUrl(uriPath)
                .clientConnector(new ReactorClientHttpConnector(httpClient))
                .build();



        // X-NCP-APIGW-API-KEY-ID
        String clientId = "์„œ๋น„์Šค ๋“ฑ๋ก ์‹œ, ๋ฐœ๊ธ‰๋ฐ›์€ ์•„์ด๋””";



        // X-NCP-APIGW-API-KEY
        String clientSecret = "์„œ๋น„์Šค ๋“ฑ๋ก ์‹œ, ๋ฐœ๊ธ‰๋ฐ›์€ ํ‚ค";



        String response = client.get().uri(uriBuilder -> uriBuilder.path("")
                .queryParam("start", start)
                .queryParam("goal", goal)
                .queryParam("option",option)
                .build())
                .header("X-NCP-APIGW-API-KEY-ID",clientId)
                .header("X-NCP-APIGW-API-KEY",clientSecret)
                .retrieve() 			  // ์‘๋‹ต์„ ๋ฐ›๊ฒŒ ํ•˜๋˜,
                .bodyToMono(String.class) // ์‘๋‹ต ๊ฐ’์„ ํ•˜๋‚˜๋งŒ,
                .block(); 				  // ๋™๊ธฐ๋กœ ๋ฐ›์œผ๋ ค ํ•œ๋‹ค.



        System.out.println(" response : " + response);
    }
}

 

 

response๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. 

 

 {
 "code":0
 ,"message":"๊ธธ์ฐพ๊ธฐ๋ฅผ ์„ฑ๊ณตํ•˜์˜€์Šต๋‹ˆ๋‹ค."
 ,"currentDateTime":"2022-09-01T23:55:26"
 ,"route":{"trafast":[{"summary":{"start":{"location":[126.8448558
 ,37.5407358]}
 ,"goal":{"location":[126.8980710
 ,37.5763211]
 ,"dir":2}
 ,"distance":8139
 ,"duration":1031796
 ,"etaServiceType":1
 ,"departureTime":"2022-09-01T23:55:26"
 ,"bbox":[[126.8427019
 ,37.5407358]
 ,[126.8986484
 ,37.5849563]]
 ,"tollFare":0
 ,"taxiFare":8990
 ,"fuelPrice":1012}
 ,"path":[[126.8447656
 ,37.5408209]
 ,[126.8449243
 ,37.5409254]
 ,[126.8446244
 ,37.5411743]
 ,[126.8447639
 .........................................
 ,"section":[{"pointIndex":12
 ,"pointCount":125
 ,"distance":4475
 ,"name":"ํ™”๊ณก๋กœ"
 ,"congestion":2
 ,"speed":32}
 ,{"pointIndex":136
 ,"pointCount":38
 ,"distance":1770
 ,"name":"๊ฐ€์–‘๋Œ€๋กœ"
 ,"congestion":2
 ,"speed":33}
 ,{"pointIndex":173
 ,"pointCount":48
 ,"distance":1627
 ,"name":"์„ฑ์•”๋กœ"
 ,"congestion":2
 ,"speed":21}]
 ,"guide":[{"pointIndex":1
 ,"type":2
 ,"instructions":"์ขŒํšŒ์ „"
 ,"distance":18
 ,"duration":7992}
 ,{"pointIndex":2
 ,"type":3
 ,"instructions":"'๊นŒ์น˜์‚ฐ๋กœ4๊ธธ' ๋ฐฉ๋ฉด์œผ๋กœ ์šฐํšŒ์ „"
 ,"distance":38
 ,"duration":11210}
 
 ...........................
  ,"type":3
 ,"instructions":"์šฐํšŒ์ „"
 ,"distance":1565
 ,"duration":262132}
 ,{"pointIndex":221
 ,"type":88
 ,"instructions":"๋ชฉ์ ์ง€"
 ,"distance":71
 ,"duration":13895}]}]}
 }

 

๊ธˆ๋ฐฉ ๊ฐˆ ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™๋‹ค. 

 

์šฐํšŒ์ „ ๋ช‡ ๋ฒˆ ์ขŒํšŒ์ „ ๋ช‡ ๋ฒˆํ•˜๋ฉด ๋„์ฐฉํ•˜๋‹ˆ๊นŒ.

 

 

 

 

 

320x100
๋ฐ˜์‘ํ˜•
๋Œ“๊ธ€์ˆ˜0