com.nepxion.discovery.common.util.RestUtil Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of discovery-common Show documentation
Show all versions of discovery-common Show documentation
Nepxion Discovery is a solution for Spring Cloud with blue green, gray, weight, limitation, circuit breaker, degrade, isolation, monitor, tracing, dye, failover, async agent
package com.nepxion.discovery.common.util;
/**
* Title: Nepxion Discovery
* Description: Nepxion Discovery
* Copyright: Copyright (c) 2017-2050
* Company: Nepxion
* @author Haojun Ren
* @version 1.0
*/
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.client.ResponseErrorHandler;
import org.springframework.web.client.RestTemplate;
import com.fasterxml.jackson.core.type.TypeReference;
import com.nepxion.discovery.common.handler.RestErrorHandler;
public class RestUtil {
public static T fromJson(RestTemplate restTemplate, String result, TypeReference typeReference) {
try {
return JsonUtil.fromJson(result, typeReference);
} catch (Exception e) {
String cause = getCause(restTemplate);
if (StringUtils.isNotEmpty(cause)) {
throw new IllegalArgumentException(cause);
}
throw e;
}
}
public static String getCause(RestTemplate restTemplate) {
ResponseErrorHandler responseErrorHandler = restTemplate.getErrorHandler();
if (responseErrorHandler instanceof RestErrorHandler) {
RestErrorHandler errorHandler = (RestErrorHandler) responseErrorHandler;
return errorHandler.getCause();
}
return null;
}
}