com.jd.httpservice.agent.PojoPropertiesConverter Maven / Gradle / Ivy
package com.jd.httpservice.agent;
import java.beans.PropertyDescriptor;
import java.util.LinkedList;
import java.util.List;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;
import org.springframework.core.convert.TypeDescriptor;
import com.jd.httpservice.NamedParamMap;
import com.jd.httpservice.PropertiesConverter;
import com.jd.httpservice.RequestParam;
/**
* 对 POJO 属性的转换器;
*
* @author haiq
*
*/
public class PojoPropertiesConverter implements PropertiesConverter {
private List propNames = new LinkedList();
private RequestParamResolver paramResolver;
private Class> argType;
public PojoPropertiesConverter(Class> argType) {
this.argType = argType;
resolveParamProperties();
}
private void resolveParamProperties() {
BeanWrapperImpl beanWrapper = new BeanWrapperImpl(argType);
List> reqParamDefs = new LinkedList>();
PropertyDescriptor[] propDescs = beanWrapper.getPropertyDescriptors();
TypeDescriptor propTypeDesc;
for (PropertyDescriptor propDesc : propDescs) {
propTypeDesc = beanWrapper.getPropertyTypeDescriptor(propDesc.getName());
RequestParam reqParamAnno = propTypeDesc.getAnnotation(RequestParam.class);
if (reqParamAnno == null) {
// 忽略未标注 RequestParam 的属性;
continue;
}
RequestParamDefinition reqParamDef = RequestParamDefinition.resolveDefinition(reqParamAnno);
ArgDefEntry defEntry = new ArgDefEntry(reqParamDefs.size(), propTypeDesc.getType(),
reqParamDef);
reqParamDefs.add(defEntry);
propNames.add(propDesc.getName());
}
paramResolver = RequestParamResolvers.createParamResolver(reqParamDefs);
}
@Override
public NamedParamMap toProperties(Object arg) {
if (propNames.size() == 0) {
return new NamedParamMap();
}
BeanWrapper beanWrapper = new BeanWrapperImpl(arg);
Object[] propValues = new Object[propNames.size()];
int i = 0;
for (String propName : propNames) {
propValues[i] = beanWrapper.getPropertyValue(propName);
i++;
}
NamedParamMap params = paramResolver.resolve(propValues);
return params;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy