Methos of Making External API Calls
RestTemplate restTemplate = new RestTemplate(); HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.setContentType(MediaType.APPLICATION_JSON); HttpEntity<SpecificClientsSSEDto> entity = new HttpEntity<>(sseDto, httpHeaders); Object obj = restTemplate.postForObject("<http://localhost:8086/sse/send>", entity, Object.class);@Configuration public class FeignConfig { // 요청 및 응답 로깅 설정 @Bean Logger.Level feignLoggerLevel() { return Logger.Level.FULL; // 로깅 레벨 설정 (NONE, BASIC, HEADERS, FULL) } // 헤더 설정을 위한 인터셉터 설정 @Bean public RequestInterceptor requestInterceptor() { return requestTemplate -> { requestTemplate.header("Authorization", "Bearer YOUR_ACCESS_TOKEN"); // 다른 헤더 설정 가능 }; } // 에러 핸들링을 위한 ErrorDecoder 설정 @Bean public ErrorDecoder errorDecoder() { return new CustomErrorDecoder(); } // 사용자 정의 ErrorDecoder 구현 public class CustomErrorDecoder implements ErrorDecoder { @Override public Exception decode(String methodKey, Response response) { // 여기서 에러 응답을 분석하고 사용자 정의 예외를 생성하여 반환 // 예외 처리 로직 작성 return new CustomFeignException("Feign Error: " + response.status()); } } // 사용자 정의 예외 클래스 public class CustomFeignException extends RuntimeException { public CustomFeignException(String message) { super(message); } } }
@Configuration public class FeignConfig { @Bean Logger.Level feignLoggerLevel() { return Logger.Level.FULL; } @Bean public RestTemplate restTemplate() { return new RestTemplate(); } }@FeignClient(name="tbTpsCm037-feign", url="<http://localhost:8082>", configuration = FeignConfig.class) public interface TbTpsCm037FeignClientApi { @GetMapping("/common/api/code/v1/common/select/list") void test(); }@EnableFeignClients (basePackages = "org.okestro.tps.api.infrastructure.config.feign")// spring will read @FeignClient annotation @Configuration @RequiredArgsConstructor public class TbTpsCm037FeignClientConfig { }
Last updated