본문 바로가기
Java/Spring with Error

[해결 방법] java.lang.IllegalArgumentException

by HJ0216 2023. 8. 21.

👉 기본 환경

- Language: Java

- DB: MySQL

- IDE: IntelliJ

 

 

⌨️ 코드

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponents;
import org.springframework.web.util.UriComponentsBuilder;
 
@RestController
public class ApiController {
 
    @GetMapping(value = "/getMidFcst")
    public Object midFcstInfoAPI() {
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
 
        String midFcstInfo_url = "https://apis.data.go.kr/1360000/MidFcstInfoService/{fcst}";
 
        UriComponents uri = UriComponentsBuilder
                .fromHttpUrl(midFcstInfo_url)
                .queryParam("fcst""getMidFcst")
                .queryParam("serviceKey""")
                .queryParam("numOfRows"5)
                .queryParam("pageNo"1)
                .queryParam("dataType""XML")
                .queryParam("stnId""")
                .queryParam("tmFc""")
                .build();
 
        ResponseEntity<String> result = restTemplate.exchange(uri.toUriString(), HttpMethod.GET, new HttpEntity<String>(headers), String.class);
 
        return result;
    }
 
}
 
 
 

😮 http://localhost:8080/getMidFcst 호출

 

 

🖨️오류

java.lang.IllegalArgumentException: Not enough variable values available to expand 'fcst'

 

 

📡 원인

{fcsg}는 queryParam으로 처리함

 

 

📰 해결 방법

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponents;
import org.springframework.web.util.UriComponentsBuilder;
 
@RestController
public class ApiController {
 
    @GetMapping(value = "/getMidFcst")
    public Object midFcstInfoAPI() {
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
 
        String midFcstInfo_url = "https://apis.data.go.kr/1360000/MidFcstInfoService/{fcst}";
 
        UriComponents uri = UriComponentsBuilder
                .fromHttpUrl(midFcstInfo_url)
                .queryParam("serviceKey""")
                .queryParam("numOfRows"5)
                .queryParam("pageNo"1)
                .queryParam("dataType""XML")
                .queryParam("stnId""")
                .queryParam("tmFc""")
                .buildAndExpand("getMidFcst");
 
        ResponseEntity<String> result = restTemplate.exchange(uri.toUriString(), HttpMethod.GET, new HttpEntity<String>(headers), String.class);
 
        return result;
    }
 
}
 
 
 

buildAndExpand()

    - fcst는 queryParam(A=B 형식)으로 처리하는 것이 아닌 변수에 fcst 변수에 대입하는 방법으로 처리해야함