@RequestMapping(value = "/test", method = RequestMethod.POST)
public String test(HttpServletRequest request) {
MyDto myDto = Tools.populate(request, MyDto.class);
}
public class Tools {
/**
* Servlet业务中实体工具
* 自动匹配参数赋值到实体bean中
*/
public static <T> T populate(HttpServletRequest request, Class<?> clazz) {
Object bean = null;
try {
// 通过空构造器实例化
bean = clazz.newInstance();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
// 获取当前类的所有方法
Method[] ms = clazz.getDeclaredMethods();
String mName;
String field;
String fieldType;
Object value;
for (Method m : ms) {
mName = m.getName();