오답노트
[Spring] Server to Server 예제 (네이버 API) 본문
import com.example.server.dto.Req;
import com.example.server.dto.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.prefs.BackingStoreException;
@Slf4j
@RestController
@RequestMapping("/api/server")
public class ServerApiController {
@GetMapping("/naver")
public String naver(){
// https://openapi.naver.com/v1/search/local.json?
// query=%EA%B0%88%EB%B9%84%EC%B0%9C
// &display=10
// &start=1
// &sort=random
String query = "갈비찜";
URI uri = UriComponentsBuilder.fromUriString("https://openapi.naver.com")
.path("/v1/search/local.json")
.queryParam("query",query)
.queryParam("display",10)
.queryParam("start",1)
.queryParam("sort","random")
.encode().build().toUri();
RestTemplate restTemplate = new RestTemplate();
RequestEntity<Void> req = RequestEntity.get(uri)
.header("X-Naver-Client-Id", "v9FlvhoPmxlEOeZ1pp_E")
.header("X-Naver-Client-Secret","KepiKCymAL")
.build();
ResponseEntity<String> result = restTemplate.exchange(req, String.class);
return result.getBody();
}
}
encode 메소드는 한글도 URL인코딩을 해준다..!
'Java > Spring' 카테고리의 다른 글
[Spring JPA] H2 In-Memory DB (0) | 2023.07.18 |
---|---|
[Spring] 코드 Test하기 (0) | 2023.07.17 |
[Spring] Server to Server (0) | 2023.07.14 |
[Spring] 비동기 처리 (Async) (0) | 2023.07.14 |
[Spring] Interceptor (0) | 2023.07.14 |