빙응의 공부 블로그
[오류 수정기]공공데이터 -SERVICE_KEY_IS_NOT_REGISTERED_ERROR 본문
📝문제사항
xml 파싱에 문제가 생겼다... 분명 DTO 잘쓰고 했는데
똑같이 들어가서 찾아보니 xml 파싱 문제가 아니라 키 문제였다.
키도 제대로 넣고 무슨 문제지? 싶었는데
https://graycha.tistory.com/166
공공데이터 API - SERVICE_KEY_IS_NOT_REGISTERED_ERROR 해결 방법
지난 3월 미세도라는 지역별 측정소 미세먼지 정보를 실시간으로 수집하여 지도에 마킹하는 프로젝트를 오픈하였다. 오픈 후, 사용 중이던 API 서비스가 중단되니 신규 서비스로 변경하라는 안
graycha.tistory.com
여기서 나온 것처럼 중복으로 인코딩되는게 문제라 하더라?
즉, WebClient 사용 중에도 인코딩이 되며 인코딩되면서 키가 안맞게 되는거다.
또한 인코딩 안된 키로 해도 다른 키가 나와 안맞게 나오기 때문에 생기는 문제라고 한다.
📝 인코딩 문제 고치기
public FetchJobListingsDTO.Items fetchJobListings(String region, String empType) {
log.info("API 호출: region={}, empType={}", region, empType);
try {
FetchJobListingsDTO responseData = webClient.get()
.uri(uriBuilder -> uriBuilder
.queryParam("serviceKey", secretKey)
.queryParam("pageNo", PAGE_NUMBER)
.queryParam("numOfRows", NUM_OF_ROWS)
.build())
.retrieve()
.bodyToMono(FetchJobListingsDTO.class)
.block();
if (responseData != null && responseData.getBody() != null) {
return filterService.filterJobListings(responseData, region, empType).getBody().getItems();
} else {
log.error("데이터를 가져오는 데 실패했습니다.");
throw new RuntimeException("데이터를 가져오는 데 실패했습니다.");
}
} catch (Exception e) {
log.error("API 호출 중 예외 발생:", e);
throw new RuntimeException("API 호출 중 예외 발생:", e);
}
}
해당 코드는 기존에 쓰던 코드이다.
예외처리는 나중에 바꿀거니까 신경 쓰지말고
FetchJobListingsDTO responseData = webClient.get()
.uri(uriBuilder -> uriBuilder
.queryParam("serviceKey", secretKey)
.queryParam("pageNo", PAGE_NUMBER)
.queryParam("numOfRows", NUM_OF_ROWS)
.build())
.retrieve()
.bodyToMono(FetchJobListingsDTO.class)
.block();
OpenAPI를 호출하는 이부분을 보면 된다.
아무 설정없이 webclient를 사용하면 uri이 인코딩되어 나온다고 한다. 그렇기에 인코딩을 꺼줘야 한다.
@Bean
public WebClient FetchJobListings_WebClient() {
// WebClient를 생성하고 DefaultUriBuilderFactory에 EncodingMode를 NONE으로 설정
UriBuilderFactory factory = new DefaultUriBuilderFactory();
((DefaultUriBuilderFactory) factory).setEncodingMode(DefaultUriBuilderFactory.EncodingMode.NONE);
return WebClient.builder()
.uriBuilderFactory(factory)
.build();
}
위의 코드처럼 UriBuilderFactory를 정의하고 Encoding 옵션을 꺼주면된다.
@Value("${fetchJob.key}")
private String secretKey;
public FetchJobListingsDTO.Items fetchJobListings(String region, String empType) {
String url = "https://apis.data.go.kr/B552583/job/job_list_env?serviceKey=" + secretKey + "&pageNo=1&numOfRows=400";
log.info("API 호출: region={}, empType={}", region, empType);
try {
FetchJobListingsDTO responseData = webClient.get()
.uri(url)
.retrieve()
.bodyToMono(FetchJobListingsDTO.class)
.block();
if (responseData != null && responseData.getBody() != null) {
return filterService.filterJobListings(responseData, region, empType).getBody().getItems();
} else {
log.error("데이터를 가져오는 데 실패했습니다.");
throw new RuntimeException("데이터를 가져오는 데 실패했습니다.");
}
} catch (Exception e) {
log.error("API 호출 중 예외 발생:", e);
throw new RuntimeException("API 호출 중 예외 발생:", e);
}
}
그래서 그냥 이렇게 바꿨다.
더 체계적으로 하려면 예외처리와 WebclientConfig에 uri을 구성해야 하지만 오류는 고쳤으니까!
'트러블슈팅' 카테고리의 다른 글
[오류 수정기]Content type 'application/octet-stream' not supported (0) | 2024.08.16 |
---|