0. RestTemplate란?
- REST API client 입장에서 활용 할 수 있는 java library
- RestTemplate communicates HTTP server using RESTful principals. RestTemplate provides different methods to communicate that will accept URI template, URI variables, response type and request object as arguments. It uses HTTP methods such as GET, POST, HEAD, PUT, DELETE etc. It also handles HTTP connections.
1. RestTemRestTemplate 실습
- 공공데이터 Open API를 이용하여 데이터를 가져오기 (https://www.data.go.kr/)
- 도로명주소 우편번호 조회서비스 API 이용 (문서에서 요청메시지/응답메시지 명세 참고)
1) GetZipCodeService.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
package com.zipcode.model;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class GetZipCodeVo {
private String ServiceKey;
private String searchSe;
private String srchwrd;
private String countPerPage;
private String currentPage;
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
|
2) GetZipCodeService.java
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
40
41
42
43
44
45
46
47
|
package com.zipcode.service;
import java.net.URLDecoder;
import java.nio.charset.Charset;
import org.springframework.stereotype.Service;
@Service
public class GetZipCodeService {
public Object getZipCode(String searchSe, String srchwrd, String countPerPage, String currentPage) throws UnsupportedEncodingException{
String url = "http://openapi.epost.go.kr/postal/retrieveNewAdressAreaCdService/retrieveNewAdressAreaCdService/getNewAddressListAreaCd";
String serviceKey = "iXL2FwauXak7M7U6w8dPxaAEADZhBrlOsCNyroXSd14dkKaQqjcyeiPNMB1xnu1bsGIpaoHWd2cH93e7PzOL0g%3D%3D";
// UnsupportedEncodingException 예외 처리를 해주지 않으면 여기에서 ERROR
RestTemplate restTemplate = new RestTemplate();
HttpHeaders httpHeaders = new HttpHeaders();
// Response Header to UTF-8
httpHeaders.setContentType(new MediaType("application", "json", Charset.forName("UTF-8")));
UriComponents uri = UriComponentsBuilder.fromHttpUrl(url).queryParam("serviceKey", decodeServiceKey)
.queryParam("searchSe", searchSe)
.queryParam("srchwrd", srchwrd)
.queryParam("countPerPage", countPerPage)
.queryParam("currentPage", currentPage)
.build(false); // 자동 Encoding 막기
//Object response = restTemplate.exchange(uri.toUriString(), HttpMethod.GET, new HttpEntity<String>(httpHeaders), String.class);
Object response = restTemplate.getForObject(uri.toUriString(), String.class);
return response;
}
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
|
3) GetZipCodeController.java
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
40
41
42
43
|
package com.zipcode.controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import com.zipcode.service.GetZipCodeService;
@RestController
public class GetZipCodeController {
private static final Logger logger = LoggerFactory.getLogger(GetZipCodeController.class);
@Autowired
GetZipCodeService getZipCodeService;
public Object getZipCode(@RequestBody GetZipCodeVo vo) throws UnsupportedEncodingException{
String searchSe = vo.getSearchSe();
String srchwrd = vo.getSearchSe();
String countPerPage = vo.getCountPerPage();
String currentPage = vo.getCurrentPage();
// UnsupportedEncodingException 예외처리 해주지 않으면 여기에서 ERROR
Object response = getZipCodeService.getZipCode(searchSe, srchwrd, countPerPage, currentPage);
return response;
}
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
|
- 결과
요청
{
"searchSe": "dong",
"srchwrd": "개포동1205-1",
"countPerPage": "10",
"currentPage": "1"
}
응답
{"NewAddressListResponse":{"cmmMsgHeader":{"requestMsgId":"","responseMsgId":"","responseTime":"20190908:224951838","successYN":"Y","returnCode":"00","errMsg":"","totalCount":"","countPerPage":"","totalPage":"","currentPage":""}}}
아무리 끙끙대도 newAddressListAreaCd가 왜 안나오는지 모르겠다 =_=....
2. 더 자세히...
자세한 document.. 공홈보다 더 자세한듯
- https://attacomsian.com/blog/http-requests-resttemplate-spring-boot
'Java > Spring Boot' 카테고리의 다른 글
02-Lombok (0) | 2020.05.20 |
---|---|
01-IntelliJ 설치 및 환경설정 (gradle, git) (0) | 2020.05.20 |
SpringBoot-010-CORS (Cross Origin Resource Sharing) (0) | 2019.09.05 |
SpringBoot-008-lombok 이용하기 (0) | 2019.09.01 |
SpringBoot-007-JPA 이용하기 (0) | 2019.08.26 |