org.onetwo.common.apiclient.impl.DefaultApiClientResponseHandler Maven / Gradle / Ivy
package org.onetwo.common.apiclient.impl;
import java.beans.PropertyDescriptor;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import org.onetwo.common.apiclient.ApiClientMethod;
import org.onetwo.common.apiclient.ApiClientResponseHandler;
import org.onetwo.common.apiclient.RequestContextData;
import org.onetwo.common.apiclient.utils.ApiClientUtils;
import org.onetwo.common.reflect.ReflectUtils;
import org.onetwo.common.spring.SpringUtils;
import org.onetwo.common.utils.JFishProperty;
import org.onetwo.common.utils.JFishPropertyInfoImpl;
import org.onetwo.common.utils.LangUtils;
import org.slf4j.Logger;
import org.springframework.beans.BeanWrapper;
import org.springframework.http.ResponseEntity;
import org.springframework.util.Assert;
import org.springframework.web.client.RestClientException;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.collect.Lists;
/**
* @author wayshall
*
*/
public class DefaultApiClientResponseHandler implements ApiClientResponseHandler {
protected final Logger logger = ApiClientUtils.getApiclientlogger();
@Override
public Class> getActualResponseType(M invokeMethod) {
return invokeMethod.getMethodReturnType();
}
@Override
public Object handleResponse(M invokeMethod, ResponseEntity> responseEntity, RequestContextData context){
return handleResponse(invokeMethod, responseEntity, context.getResponseType());
}
public Object handleResponse(M invokeMethod, ResponseEntity> responseEntity, Class> actualResponseType){
Object resposne = responseEntity.getBody();
if(responseEntity.getStatusCode().is2xxSuccessful()){
if (List.class.isAssignableFrom(actualResponseType)) {
return convert2List(invokeMethod, responseEntity);
}
return resposne;
}
throw new RestClientException("error response: " + responseEntity.getStatusCodeValue());
}
@SuppressWarnings("unchecked")
protected Object convert2List(M invokeMethod, ResponseEntity> responseEntity) {
List> dataList = (List>) responseEntity.getBody();
if (LangUtils.isEmpty(dataList) || !Map.class.isAssignableFrom(dataList.get(0).getClass())) {
return responseEntity.getBody();
}
List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy