spring boot / java 11 / react client
๐ฅจ ๊ฐ์ธ ํ๋ก์ ํธ์์ naver ๊ธธ์ฐพ๊ธฐ api๋ฅผ ์ฌ์ฉํ ๊ณผ์ ์ ์ ์ด๋๋ คํ๋ค.
๐ฅช ๊ณต์ ๋ฌธ์๋ฅผ ๋ณด๋ ๊ฒ์ด ๊ฐ์ฅ ์ ํํ๋ฏ๋ก ๊ณต์๋ฌธ์๋ฅผ ์ฐธ๊ณ ํ๋๊ฒ ์ ์ผ ๋ฒ ์คํธ์ด๋ค.
๐ถ ์ค์ ๋ถํฐ API ์์ฒญ๊น์ง, API ์์ฒญ์์ response data๋ฅผ ๋ฐ์์ค๊ธฐ๊น์ง ์ ๋ฆฌํ๋ ค ํ๋ค.
๐ซ ๊ธธ์ฐพ๊ธฐ API๋ ์ฐ์ ๋ค์ด๋ฒ ํด๋ผ์ฐ๋ ํ๋ซํผ์์ ์๋น์ค ์ฌ์ฉ์ ์ํ ๋ฑ๋ก์ด ๋จผ์ ๋์ด์ผ ํ๋ค.

**** ์๋น์ค ๋ฑ๋ก์ ์๋ต

๐ฟ ์ธ์ฆ์ ์ํ ํค๊ฐ์ด๋ฏ๋ก, ์ ์ถ๋์ง ์๋๋ก ์กฐ์ฌํด์ผ ํ๋ค.
** ๋ฑ๋ก์ด ๋์๋ค๋ฉด ์ค์ API๋ฅผ ์์ฒญํด์ผ ํ๋๋ฐ
์ฒ์ ์๋ ํ ๋์ ํด๋ผ์ด์ธํธ์์ ์ง์ ์์ฒญํ ๊ฐ์ ์ฐ๋ ค ํ์ผ๋, ํ์ฌ๋ JS ์์ฒญ์ NAVER ์ธก์์ ๋ง์๋์๋ค๊ณ ํ๋ค. CORS ์๋ฌ๊ฐ ๋ฆฌํด๋ ๊ฒ์...
https://developers.naver.com/forum/posts/7609
๊ฐ๋ฐ์ ํฌ๋ผ - NAVER Developers
developers.naver.com

๊ทธ๋ ๋ค๊ณ ํ๋ค.
๐ฑ๐ฑ๐ฑ
๊ทธ๋์ client์์ ๊ฐ์ ธ์จ ์ขํ๊ฐ์ java backend์ API๋ฅผ ํธ์ถ ํ ๋์ ํ๋ผ๋ฏธํฐ๋ก ๋์ ธ์
๊ธธ์ฐพ๊ธฐ ๊ฒฐ๊ณผ ๊ฐ์ ๊ฐ์ ธ์ค๋๋ก ๊ตฌํํ๋ค.
์ถํด๊ทผ ์๊ฐ์ด ์ผ๋ง๋ ๊ฑธ๋ฆด์ง ๊ถ๊ธํ๋ค.

์ถ๋ฐ์ :
- x: 126.844856
- y: 37.5407361
- _lat: 37.5407361
- _lng: 126.844856
๋์ฐฉ์ :
- x: 126.8980711
- y: 37.5763214
- _lat: 37.5763214
- _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}]}]}
}
๊ธ๋ฐฉ ๊ฐ ์ ์์ ๊ฒ ๊ฐ๋ค.
์ฐํ์ ๋ช ๋ฒ ์ขํ์ ๋ช ๋ฒํ๋ฉด ๋์ฐฉํ๋๊น.
'JAVA > SPRING' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Spring] @Valid , @Validated, BindingResult (0) | 2023.06.29 |
---|---|
[Spring] Spring / Spring boot ํน์ง ๊ฐ๋จ ์ ๋ฆฌ. (0) | 2023.01.11 |
springBoot / mapstruct (0) | 2022.06.08 |
spring boot / H2 DB ์ธํ (0) | 2022.05.29 |
Hikari CP / Spring boot ์ค์ (0) | 2022.05.23 |
spring boot / java 11 / react client
๐ฅจ ๊ฐ์ธ ํ๋ก์ ํธ์์ naver ๊ธธ์ฐพ๊ธฐ api๋ฅผ ์ฌ์ฉํ ๊ณผ์ ์ ์ ์ด๋๋ คํ๋ค.
๐ฅช ๊ณต์ ๋ฌธ์๋ฅผ ๋ณด๋ ๊ฒ์ด ๊ฐ์ฅ ์ ํํ๋ฏ๋ก ๊ณต์๋ฌธ์๋ฅผ ์ฐธ๊ณ ํ๋๊ฒ ์ ์ผ ๋ฒ ์คํธ์ด๋ค.
๐ถ ์ค์ ๋ถํฐ API ์์ฒญ๊น์ง, API ์์ฒญ์์ response data๋ฅผ ๋ฐ์์ค๊ธฐ๊น์ง ์ ๋ฆฌํ๋ ค ํ๋ค.
๐ซ ๊ธธ์ฐพ๊ธฐ API๋ ์ฐ์ ๋ค์ด๋ฒ ํด๋ผ์ฐ๋ ํ๋ซํผ์์ ์๋น์ค ์ฌ์ฉ์ ์ํ ๋ฑ๋ก์ด ๋จผ์ ๋์ด์ผ ํ๋ค.

**** ์๋น์ค ๋ฑ๋ก์ ์๋ต

๐ฟ ์ธ์ฆ์ ์ํ ํค๊ฐ์ด๋ฏ๋ก, ์ ์ถ๋์ง ์๋๋ก ์กฐ์ฌํด์ผ ํ๋ค.
** ๋ฑ๋ก์ด ๋์๋ค๋ฉด ์ค์ API๋ฅผ ์์ฒญํด์ผ ํ๋๋ฐ
์ฒ์ ์๋ ํ ๋์ ํด๋ผ์ด์ธํธ์์ ์ง์ ์์ฒญํ ๊ฐ์ ์ฐ๋ ค ํ์ผ๋, ํ์ฌ๋ JS ์์ฒญ์ NAVER ์ธก์์ ๋ง์๋์๋ค๊ณ ํ๋ค. CORS ์๋ฌ๊ฐ ๋ฆฌํด๋ ๊ฒ์...
https://developers.naver.com/forum/posts/7609
๊ฐ๋ฐ์ ํฌ๋ผ - NAVER Developers
developers.naver.com

๊ทธ๋ ๋ค๊ณ ํ๋ค.
๐ฑ๐ฑ๐ฑ
๊ทธ๋์ client์์ ๊ฐ์ ธ์จ ์ขํ๊ฐ์ java backend์ API๋ฅผ ํธ์ถ ํ ๋์ ํ๋ผ๋ฏธํฐ๋ก ๋์ ธ์
๊ธธ์ฐพ๊ธฐ ๊ฒฐ๊ณผ ๊ฐ์ ๊ฐ์ ธ์ค๋๋ก ๊ตฌํํ๋ค.
์ถํด๊ทผ ์๊ฐ์ด ์ผ๋ง๋ ๊ฑธ๋ฆด์ง ๊ถ๊ธํ๋ค.

์ถ๋ฐ์ :
- x: 126.844856
- y: 37.5407361
- _lat: 37.5407361
- _lng: 126.844856
๋์ฐฉ์ :
- x: 126.8980711
- y: 37.5763214
- _lat: 37.5763214
- _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}]}]}
}
๊ธ๋ฐฉ ๊ฐ ์ ์์ ๊ฒ ๊ฐ๋ค.
์ฐํ์ ๋ช ๋ฒ ์ขํ์ ๋ช ๋ฒํ๋ฉด ๋์ฐฉํ๋๊น.
'JAVA > SPRING' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Spring] @Valid , @Validated, BindingResult (0) | 2023.06.29 |
---|---|
[Spring] Spring / Spring boot ํน์ง ๊ฐ๋จ ์ ๋ฆฌ. (0) | 2023.01.11 |
springBoot / mapstruct (0) | 2022.06.08 |
spring boot / H2 DB ์ธํ (0) | 2022.05.29 |
Hikari CP / Spring boot ์ค์ (0) | 2022.05.23 |