[Spring] UriComponentsBuilder / RestTemplate GET with parameters / RestTemplate 사용 시 Get 메서드로 파라미터 보내는 법
2022. 1. 13. 22:32
반응형
[이전 글]
[Spring] RestTemplate / spring 에서 http 통신하는 법 / API 호출
🐝 Rest 서비스를 호출하는 방법 - RestTemplate Spring 3부터 지원, REST API 호출 이후 응답을 받을 때까지 기다리는 동기 방식 - AsyncRestTemplate Spring 4에 추가된 비동기 RestTemplate - WebClient Spring..
frogand.tistory.com
🌱 UriComponentsBuilder
org.springframework.web.util.UriComponentsBuilder를 사용하면 명확한 URI 문자열을 생성할 수 있다.
final String uri = "https://frogand.tistory.com";
final String uriWithParameter = UriComponentsBuilder.fromHttpUrl(uri)
.path("/category/programming/ECMAScript6"
.queryParam("page", 2)
.toString();
// https://frogand.tistory.com/category/programming/ECMAScript6?page=2
예제) parameter가 있는 url로 HTTP GET Method 호출
public static List<DataDto> selectDataByMemberId(String memberId) {
if (StringUtils.isEmpty(memberId)) {
return null;
}
final String selectDataUrl = "http://localhost:8080" + "/api/Data";
String httpUrl = UriComponentsBuilder.fromHttpUrl(selectDataUrl)
.queryParam("memberId", memberId)
.toUriString();
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<List<DataDto>> userRecommendResponse =
restTemplate.exchange(
httpUrl,
HttpMethod.GET,
null,
new ParameterizedTypeReference<List<DataDto>>() {
});
return userRecommendResponse.getBody();
}
출처
https://stackoverflow.com/questions/8297215/spring-resttemplate-get-with-parameters
Spring RestTemplate GET with parameters
I have to make a REST call that includes custom headers and query parameters. I set my HttpEntity with just the headers (no body), and I use the RestTemplate.exchange() method as follows: HttpHead...
stackoverflow.com
728x90
반응형
'web > Spring' 카테고리의 다른 글
[Spring MVC] Thymeleaf 타임리프 기본 기능- 텍스트 text, utext (0) | 2022.01.18 |
---|---|
[Spring MVC] Thymeleaf 타임리프 기본 기능 (0) | 2022.01.17 |
[Spring] @RequestBody, @RequestParam, @ModelAttribute의 차이 (0) | 2022.01.12 |
[Spring JPA] Spring JPA에서 Cubrid(DBMS) 연결하는 법 (0) | 2022.01.04 |
[Spring Boot] Flyway를 이용한 데이터 마이그레이션 / Spring DB Migration Tool flyway (0) | 2021.12.03 |
Written by ner.o
개발자 네로의 개발 일기,
자바를 좋아합니다 !
댓글 개