- [spring] 공공데이터 포탈 APIKEY 인코딩문제 2022.01.19
2022. 1. 19. 00:01
package com.onware.portal.holidayapitest.Controller;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URLEncoder;
import java.util.Map;
@Slf4j
@RestController
@NoArgsConstructor
public class Controller {
private WebClient webClient;
private String BASE_URL;
private String serviceKey;
@Autowired
Controller(WebClient webClient) {
this.webClient = webClient;
}
@GetMapping("/getholiday")
public ResponseEntity<String> getHoliday(@RequestParam Map<String, String> params) throws UnsupportedEncodingException, URISyntaxException {
serviceKey = "인코딩된키"
String solYear = params.get("solYear");
String solMonth = params.get("solMonth");
BASE_URL = "http://apis.data.go.kr/B090041/openapi/service/SpcdeInfoService/getRestDeInfo";
StringBuilder urlBuilder = new StringBuilder(BASE_URL);
urlBuilder.append("?" + URLEncoder.encode("serviceKey", "UTF-8") + "="+ serviceKey);
urlBuilder.append("&" + URLEncoder.encode("_type", "UTF-8") + "=" + URLEncoder.encode("json", "UTF-8"));
urlBuilder.append("&" + URLEncoder.encode("solYear", "UTF-8") + "=" + URLEncoder.encode(solYear, "UTF-8"));
urlBuilder.append("&" + URLEncoder.encode("solMonth", "UTF-8") + "=" + URLEncoder.encode(solMonth, "UTF-8"));
log.info(urlBuilder.toString());
Mono<String> request = webClient.get()
.uri(URI.create(urlBuilder.toString()))
.accept(MediaType.APPLICATION_JSON)
.retrieve()
.bodyToMono(String.class);
return new ResponseEntity<>(request.blockOptional().get(), HttpStatus.OK);
}
}