티스토리 뷰

반응형

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);
        ...
    }
}
반응형
반응형
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/05   »
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
글 보관함