티스토리 뷰
프로그래밍/Back-End
Spring Boot에서 응답 본문이 무효인 경우 GET 요청에 RestTemplate 클라이언트를 사용하는 방법은 무엇입니까?
worniworni 2022. 1. 20. 20:19반응형
Java 및 Spring Boot 프로젝트에서 RestTemplate 클라이언트를 사용하고 있으며 서버에서 응답 본문을 수신하면 다음 코드가 있습니다.
private RestTemplate oauth2RestTemplate;
private ParameterizedTypeReference<List<Employee>> parameterizedTypeReference;
....
ResponseEntity<List<Employee>> rtGetResponse = oauth2RestTemplate.exchange(uriComponents.toUri(), HttpMethod.GET, httpEntity, parameterizedTypeReference);
따라서 이 경우 직원 목록을 받게 됩니다.
이제 이 메서드를 사용하고 싶지만 서버의 응답 본문은 void이고 exchange 메서드를 사용하고 싶지만 응답이 void이기 때문에 parameterizedTypeReference 대신 무엇을 사용해야 할지 모르겠습니다. 그래서 나는 응답 본문이 없고 예외를 잡기만을 원합니다.
그런 일을 해도 괜찮습니까?
try {
oauth2RestTemplate.exchange(uriComponents.toUri(), HttpMethod.GET, httpEntity, null);
} catch (HttpStatusCodeException e) {
switch (e.getStatusCode()) {
case BAD_REQUEST:
throw new BadRequestException(e);
case NOT_FOUND:
throw new NotFoundException(e);
...
}
}
Void클래스 를 사용할 수 있습니다
try {
oauth2RestTemplate.exchange(uriComponents.toUri(), HttpMethod.GET, httpEntity, Void.class);
} catch (HttpStatusCodeException e) {
switch (e.getStatusCode()) {
case BAD_REQUEST:
throw new BadRequestException(e);
case NOT_FOUND:
throw new NotFoundException(e);
...
}
}
반응형
'프로그래밍 > Back-End' 카테고리의 다른 글
DI 프레임워크를 사용하여 동적으로 빈을 생성하는 방법 (0) | 2022.01.27 |
---|---|
Spring expression 언어: SpEL에서 맵 조회를 위한 키로 변수 사용 (0) | 2022.01.27 |
네이티브 이미지에 대한 Spring Boot 빌드 이미지 실패 (0) | 2022.01.20 |
resize2fs 에러 : Operation not permitted While trying to add group centos (0) | 2022.01.20 |
Failed to introspect Class [org.springframework.boot.autoconfigure.orm.jpa..HibernateJpaConfiguration (0) | 2022.01.20 |
반응형
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 커널드라이버
- 예제
- JavaScript
- Frontend
- ChatGPT
- ios
- Java
- 주식
- Spring
- reactjs
- JSX
- 부동산
- HTML
- SWiFT
- Python
- react
- golang
- 생각
- WinAPI
- 트렌드
- 투자
- Windows
- CSS
- 유튜브
- go
- Linux
- frida
- 스핀락
- 파이썬
- Backend
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함