import com.alibaba.fastjson.JSON; import org.apache.commons.lang.StringUtils; import org.springframework.core.ParameterizedTypeReference; import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; import org.springframework.http.MediaType; import org.springframework.stereotype.Component; import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; import org.springframework.web.client.RestTemplate; import javax.annotation.Resource; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; import java.math.BigDecimal; import java.math.BigInteger; import java.text.SimpleDateFormat; import java.util.*; @Component public class RestTempLateUtil { @Resource private RestTemplate restTemplate; public <T> T postForObject(String url, Map map,Class<T> tClass){ MultiValueMap<String,Object> multiValueMap = new LinkedMultiValueMap(); String str = null; Set<Map.Entry> set = map.entrySet(); Iterator<Map.Entry> iterator1 = set.iterator(); multiValueMap = getStringObjectMultiValueMap(multiValueMap, iterator1); HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED); HttpEntity<MultiValueMap> httpEntity = new HttpEntity<>(multiValueMap,httpHeaders); return mapToObject(restTemplate.exchange(url, HttpMethod.POST, httpEntity, new ParameterizedTypeReference<T>() {}).getBody(),tClass); //return (T)restTemplate.postForObject(url,httpEntity,tClass); } public <T> T postForObject(String url, Object object,Class<T> tClass){ MultiValueMap<String,Object> multiValueMap = new LinkedMultiValueMap(); multiValueMap = objectToMap(multiValueMap,object); HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED); HttpEntity<MultiValueMap<String,Object>> httpEntity = new HttpEntity<>(multiValueMap,httpHeaders); return mapToObject(restTemplate.exchange(url, HttpMethod.POST, httpEntity, new ParameterizedTypeReference<T>() {}).getBody(),tClass); //return (T)restTemplate.postForObject(url,httpEntity,tClass); } public <T> T postForObjectJ(String url, Object object,Class<T> tClass){ /*MultiValueMap<String,Object> multiValueMap = new LinkedMultiValueMap(); multiValueMap = objectToMap(multiValueMap,object);*/ HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.setContentType(MediaType.APPLICATION_JSON_UTF8); HttpEntity<Object> httpEntity = new HttpEntity<>(object,httpHeaders); return mapToObject(restTemplate.exchange(url, HttpMethod.POST, httpEntity, new ParameterizedTypeReference<T>() {}).getBody(),tClass); //return (T)restTemplate.postForObject(url,httpEntity,tClass); } public <T> T postForObject(String url,Class<T> tClass,Object... objects){ MultiValueMap<String,Object> multiValueMap = new LinkedMultiValueMap(); HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED); HttpEntity<MultiValueMap> httpEntity = new HttpEntity<>(multiValueMap,httpHeaders); return mapToObject(restTemplate.exchange(url, HttpMethod.POST, httpEntity, new ParameterizedTypeReference<T>() {},objects).getBody(),tClass); //return (T)restTemplate.postForObject(url,httpEntity,tClass,objects); } public <T> List<T> postForList(String url, Map map,Class<T> tClass){ MultiValueMap<String,Object> multiValueMap = new LinkedMultiValueMap(); String str = null; Set<Map.Entry> set = map.entrySet(); Iterator<Map.Entry> iterator1 = set.iterator(); multiValueMap = getStringObjectMultiValueMap(multiValueMap, iterator1); HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED); HttpEntity<MultiValueMap> httpEntity = new HttpEntity<>(multiValueMap,httpHeaders); return mapToObjectList(restTemplate.exchange(url, HttpMethod.POST, httpEntity, new ParameterizedTypeReference<List<T>>() {}).getBody(),tClass); //return (T)restTemplate.postForObject(url,httpEntity,tClass); } public <T> List<T> postForList(String url, Object object,Class<T> tClass){ MultiValueMap<String,Object> multiValueMap = new LinkedMultiValueMap(); multiValueMap = objectToMap(multiValueMap,object); HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED); HttpEntity<MultiValueMap<String,Object>> httpEntity = new HttpEntity<>(multiValueMap,httpHeaders); return mapToObjectList(restTemplate.exchange(url, HttpMethod.POST, httpEntity, new ParameterizedTypeReference<List<T>>() {}).getBody(),tClass); //return (T)restTemplate.postForObject(url,httpEntity,tClass); } public <T> List<T> postForListJ(String url, Object object,Class<T> tClass){ /*MultiValueMap<String,Object> multiValueMap = new LinkedMultiValueMap(); multiValueMap = objectToMap(multiValueMap,object);*/ HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.setContentType(MediaType.APPLICATION_JSON_UTF8); HttpEntity<String> httpEntity = new HttpEntity<>(JSON.toJSONString(object),httpHeaders); return mapToObjectList(restTemplate.exchange(url, HttpMethod.POST, httpEntity, new ParameterizedTypeReference<List<T>>() {}).getBody(),tClass); //return (T)restTemplate.postForObject(url,httpEntity,tClass); } public <T> List<T> postForListJ(String url, Object object,Class<T> tClass,Object... objects){ /*MultiValueMap<String,Object> multiValueMap = new LinkedMultiValueMap(); multiValueMap = objectToMap(multiValueMap,object);*/ HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.setContentType(MediaType.APPLICATION_JSON_UTF8); HttpEntity<String> httpEntity = new HttpEntity<>(JSON.toJSONString(object),httpHeaders); return mapToObjectList(restTemplate.exchange(url, HttpMethod.POST, httpEntity, new ParameterizedTypeReference<List<T>>() {},objects).getBody(),tClass); //return (T)restTemplate.postForObject(url,httpEntity,tClass); } public <T> List<T> postForList(String url,Class<T> tClass,Object... objects){ MultiValueMap<String,Object> multiValueMap = new LinkedMultiValueMap(); HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED); HttpEntity<MultiValueMap> httpEntity = new HttpEntity<>(multiValueMap,httpHeaders); return mapToObjectList(restTemplate.exchange(url, HttpMethod.POST, httpEntity, new ParameterizedTypeReference<List<T>>() {},objects).getBody(),tClass); //return (T)restTemplate.postForObject(url,httpEntity,tClass); } private MultiValueMap<String, Object> getStringObjectMultiValueMap(MultiValueMap<String, Object> multiValueMap, Iterator<Map.Entry> iterator1) { while (iterator1.hasNext()){ Map.Entry entry = iterator1.next(); Object object = entry.getValue(); String name = (String) entry.getKey(); if(object.getClass().isArray()){ Object o[] = (Object[])object; for(int i = 0; i < o.length; i++){ multiValueMap.add(name,anyToString(o[i])); } } else if(object instanceof Collection){ Collection collection = (Collection)object; Iterator iterator = collection.iterator(); while (iterator.hasNext()){ multiValueMap.add(name,anyToString(iterator.next())); } }else { String value = anyToString(object); if(value==null){ multiValueMap.add(name,object); }else { multiValueMap.add(name,value); } } } return multiValueMap; } public static MultiValueMap objectToMap(MultiValueMap multiValueMap,Object object){ Class Clazz = object.getClass(); Field fields[] = Clazz.getDeclaredFields(); List<String> list = new ArrayList<>(); for(Field field:fields){ list.add(field.getName()); } fields = Clazz.getFields(); for(Field field:fields){ list.add(field.getName()); } for(String string:list){ try { Method method = Clazz.getMethod("get"+string.substring(0,1).toUpperCase()+string.substring(1,string.length())); Object object1 = method.invoke(object); if(object1 != null){ if(object1.getClass().isPrimitive()){ multiValueMap.add(string,anyToString(object1)); }else if(object1 instanceof Number){ multiValueMap.add(string,anyToString(object1)); }else if(object1 instanceof Boolean){ multiValueMap.add(string,anyToString(object1)); }else if(object1 instanceof String){ multiValueMap.add(string,anyToString(object1)); }else if(object1 instanceof Date){ multiValueMap.add(string,anyToString(object1)); }else if(object1 instanceof Character) { multiValueMap.add(string, anyToString(object1)); } } }catch (Exception e){ //e.printStackTrace(); } } return multiValueMap; } public static String anyToString(Object object){ if(object.getClass().isPrimitive()){ return String.valueOf(object); }else if(object instanceof Number){ return object.toString(); }else if (object instanceof String){ return (String)object; }else if(object instanceof Character){ return object.toString(); }else if(object instanceof Boolean){ return object.toString(); }else if(object instanceof Date){ Date date = (Date)object; return date.toString(); } else { return null; } } public static<T> List<T> mapToObjectList(List list,Class<T> clazz){ if(list==null||list.size()==0){ return null; } List<T> list1 = new ArrayList<>(); for(Object object:list){ if(object instanceof Map){ list1.add(mapToObject((Map)object,clazz)); } } return list1; } public static <T> T objectToT(Object object,Class<T> tClass){ try { if(tClass==int.class){ if(object.getClass()==int.class){ return (T)object; }else if(object.getClass()==Integer.class){ return (T)Integer.valueOf((int)object); }else { return (T)((Object)Integer.parseInt(String.valueOf(object))); } }else if(tClass==Integer.class){ if(object.getClass()==int.class){ return (T)Integer.valueOf((int)object); }else if(object.getClass()==Integer.class){ return (T)object; }else { return (T)((Object)Integer.valueOf(String.valueOf(object))); } }else if(tClass==short.class){ if(object.getClass()==short.class){ return (T)object; }else if(object.getClass()==Short.class){ return (T)((Object)((Short)object).shortValue()); }else { return (T)((Object)Short.parseShort(String.valueOf(object))); } }else if(tClass==Short.class){ if(object.getClass()==short.class){ return (T)Short.valueOf((short)object); }else if(object.getClass()==Short.class){ return (T)object; }else { return (T)((Object)Short.valueOf(String.valueOf(object))); } }else if(tClass==char.class){ if(object.getClass()==char.class){ return (T)object; }else if(object.getClass()==Character.class){ return (T)((Object)((Character)object).charValue()); }else { return (T)(Object)String.valueOf(object).charAt(0); } }else if(tClass==Character.class){ if(object.getClass()==char.class){ return (T)object; }else if(object.getClass()==Character.class){ return (T)object; }else { return (T)(Object)String.valueOf(object).charAt(0); } }else if(tClass==byte.class){ if(object.getClass()==byte.class){ return (T)object; }else if(object.getClass()==Byte.class){ return (T)((Object)((Byte)object).byteValue()); }else { return (T)((Object)Byte.parseByte(String.valueOf(object))); } }else if(tClass==Byte.class){ if(object.getClass()==byte.class){ return (T)Byte.valueOf((byte)object); }else if(object.getClass()==Byte.class){ return (T)object; }else { return (T)((Object)Byte.valueOf(String.valueOf(object))); } }else if(tClass==boolean.class){ if(object.getClass()==boolean.class){ return (T)object; }else if(object.getClass()==Boolean.class){ return (T)((Object)((Boolean)object).booleanValue()); }else { return (T)((Object)Boolean.parseBoolean(String.valueOf(object))); } }else if(tClass==Boolean.class){ if(object.getClass()==boolean.class){ return (T)Boolean.valueOf((boolean)object); }else if(object.getClass()==Boolean.class){ return (T)object; }else { return (T)((Object)Boolean.valueOf(String.valueOf(object))); } }else if(tClass==float.class){ if(object.getClass()==float.class){ return (T)object; }else if(object.getClass()==Float.class){ return (T)((Object)((Float)object).floatValue()); }else { return (T)((Object)Float.parseFloat(String.valueOf(object))); } }else if(tClass==Float.class){ if(object.getClass()==float.class){ return (T)Float.valueOf((float)object); }else if(object.getClass()==Float.class){ return (T)object; }else { return (T)((Object)Float.valueOf(String.valueOf(object))); } }else if(tClass==double.class){ if(object.getClass()==double.class){ return (T)object; }else if(object.getClass()==Double.class){ return (T)((Object)((Double)object).doubleValue()); }else { return (T)((Object)Double.parseDouble(String.valueOf(object))); } }else if(tClass==Double.class){ if(object.getClass()==double.class){ return (T)Double.valueOf((double)object); }else if(object.getClass()==Double.class){ return (T)object; }else { return (T)((Object)Double.valueOf(String.valueOf(object))); } }else if(tClass==long.class){ if(object.getClass()==long.class){ return (T)object; }else if(object.getClass()==Long.class){ return (T)((Object)((Long)object).longValue()); }else { return (T)((Object)Long.parseLong(String.valueOf(object))); } }else if(tClass==Long.class){ if(object.getClass()==long.class){ return (T)Long.valueOf((long)object); }else if(object.getClass()==Long.class){ return (T)object; }else { return (T)((Object)Long.valueOf(String.valueOf(object))); } }else if(tClass==BigInteger.class){ if(object.getClass()==BigInteger.class){ return (T)object; }else { return (T)new BigInteger(String.valueOf(object)); } }else if(tClass==BigDecimal.class){ if(object.getClass()==BigDecimal.class){ return (T)object; }else { return (T)new BigDecimal(String.valueOf(object)); } }else if(tClass==String.class){ if(object.getClass()==String.class){ return (T)object; }else { return (T)String.valueOf(object); } }else if(tClass==Date.class){ if(object.getClass()==Date.class){ return (T)object; }else { SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ"); return (T)simpleDateFormat.parse(String.valueOf(object)); } }else { return null; } } catch (Exception e){ return null; } } public static<T> T mapToObject(Object object,Class<T> clazz){ if(!(object instanceof Map)){ Object o = objectToT(object,clazz); if(object == null){ return (T)object; }else { return (T)o; } } Map map = (Map)object; Set<Map.Entry> entrySet = map.entrySet(); Iterator<Map.Entry> iterator = entrySet.iterator(); Map.Entry entry; Method methods[] = clazz.getMethods(); T dx = null; try { dx = clazz.newInstance(); }catch (Exception e){ e.printStackTrace(); return null; } while (iterator.hasNext()){ entry = iterator.next(); Object key = entry.getKey(); Object value = entry.getValue(); if(value==null){ continue; } String sKey = (String) key; String methodName = "set"+sKey.substring(0,1).toUpperCase()+sKey.substring(1,sKey.length()); ws:for(Method method:methods){ if(method.getName().equals(methodName)){ Class c[] = method.getParameterTypes(); if(c.length==1){ if(c[0]==int.class||c[0]==Integer.class){ try { method.invoke(dx,Integer.parseInt(String.valueOf(value))); break ws; }catch (Exception e){ e.printStackTrace(); } } else if(c[0]==byte.class||c[0]==Byte.class){ try { method.invoke(dx,Byte.parseByte(String.valueOf(value))); break ws; }catch (Exception e){ e.printStackTrace(); } } else if(c[0]==char.class||c[0]==Character.class){ try { method.invoke(dx,value); break ws; }catch (Exception e){ e.printStackTrace(); } } else if(c[0]==boolean.class||c[0]==Boolean.class){ try { method.invoke(dx,Boolean.parseBoolean(String.valueOf(value))); break ws; }catch (Exception e){ e.printStackTrace(); } } else if(c[0]==short.class||c[0]==Short.class){ try { method.invoke(dx,Short.parseShort(String.valueOf(value))); break ws; }catch (Exception e){ e.printStackTrace(); } } else if(c[0]==long.class||c[0]==Long.class){ try { method.invoke(dx,Long.parseLong(String.valueOf(value))); break ws; }catch (Exception e){ e.printStackTrace(); } } else if(c[0]==float.class||c[0]==Float.class){ try { method.invoke(dx,Float.parseFloat(String.valueOf(value))); break ws; }catch (Exception e){ e.printStackTrace(); } } else if(c[0]==double.class||c[0]==Double.class){ try { method.invoke(dx,Double.parseDouble(String.valueOf(value))); break ws; }catch (Exception e){ e.printStackTrace(); } } else if(c[0]==String.class){ try { method.invoke(dx,String.valueOf(value)); break ws; }catch (Exception e){ e.printStackTrace(); } }else if(c[0]==BigInteger.class){ try { method.invoke(dx,BigInteger.valueOf(Long.parseLong(String.valueOf(value)))); break ws; }catch (Exception e){ e.printStackTrace(); } }else if(c[0]==BigDecimal.class){ try { method.invoke(dx,new BigDecimal(String.valueOf(value))); break ws; }catch (Exception e){ e.printStackTrace(); } }else if(c[0]==Date.class){ try { if(value instanceof Long){ method.invoke(dx,new Date((Long)value)); break ws; }else if(value.getClass()==long.class){ method.invoke(dx,new Date((long)value)); break ws; }else { if(StringUtils.isNumeric((String)value)){ method.invoke(dx,new Date(Long.valueOf((String) value))); break ws; }else { //SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ"); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); simpleDateFormat.setLenient(false); method.invoke(dx,simpleDateFormat.parse(dateToDate((String) value))); break ws; } } }catch (Exception e){ e.printStackTrace(); } } //对于数组的转换 else if(c[0]==int[].class||c[0]==Integer[].class){ try { if(c[0]==int[].class){ int[] objects = (int[])value; int[] ints = new int[objects.length]; for(int i = 0; i < ints.length; i++){ ints[i] = Integer.parseInt(String.valueOf(objects[i])); } method.invoke(dx,(Object)ints); } if(c[0]==Integer[].class){ Integer[] objects = (Integer[])value; Integer[] ints = new Integer[objects.length]; for(int i = 0; i < ints.length; i++){ ints[i] = Integer.parseInt(String.valueOf(objects[i])); } method.invoke(dx,(Object)ints); } break ws; }catch (Exception e){ e.printStackTrace(); } } else if(c[0]==byte[].class||c[0]==Byte[].class){ try { if(c[0]==byte[].class){ byte[] objects = (byte[])value; byte[] bytes = new byte[objects.length]; for(int i = 0; i < bytes.length; i++){ bytes[i] = Byte.parseByte(String.valueOf(objects[i])); } method.invoke(dx,(Object)bytes); } if(c[0]==Byte[].class){ Byte[] objects = (Byte[])value; Byte[] bytes = new Byte[objects.length]; for(int i = 0; i < bytes.length; i++){ bytes[i] = Byte.parseByte(String.valueOf(objects[i])); } method.invoke(dx,(Object)bytes); } break ws; }catch (Exception e){ e.printStackTrace(); } } else if(c[0]==char[].class||c[0]==Character[].class){ try { if(c[0]==char[].class){ char[] objects = (char[])value; char[] chars = new char[objects.length]; for(int i = 0; i < chars.length; i++){ chars[i] = (char)objects[i]; } method.invoke(dx,(Object)chars); } if(c[0]==Character[].class){ Character[] objects = (Character[])value; Character[] characters = new Character[objects.length]; for(int i = 0; i < characters.length; i++){ characters[i] = (Character)objects[i]; } method.invoke(dx,(Object)characters); } break ws; }catch (Exception e){ e.printStackTrace(); } } else if(c[0]==boolean[].class||c[0]==Boolean[].class){ try { if(c[0]==boolean[].class){ boolean[] objects = (boolean[])value; boolean[] booleans = new boolean[objects.length]; for(int i = 0; i < booleans.length; i++){ booleans[i] = Boolean.parseBoolean(String.valueOf(objects[i])); } method.invoke(dx,(Object)booleans); } if(c[0]==Boolean[].class){ Boolean[] objects = (Boolean[])value; Boolean[] booleans = new Boolean[objects.length]; for(int i = 0; i < booleans.length; i++){ booleans[i] = Boolean.parseBoolean(String.valueOf(objects[i])); } method.invoke(dx,(Object)booleans); } break ws; }catch (Exception e){ e.printStackTrace(); } } else if(c[0]==short[].class||c[0]==Short[].class){ try { if(c[0]==short[].class){ short[] objects = (short[])value; short[] shorts = new short[objects.length]; for(int i = 0; i < shorts.length; i++){ shorts[i] = Short.parseShort(String.valueOf(objects[i])); } method.invoke(dx,(Object)shorts); } if(c[0]==Short[].class) { Short[] objects = (Short[])value; Short[] shorts = new Short[objects.length]; for (int i = 0; i < shorts.length; i++) { shorts[i] = Short.parseShort(String.valueOf(objects[i])); } method.invoke(dx, (Object)shorts); } break ws; }catch (Exception e){ e.printStackTrace(); } } else if(c[0]==long[].class||c[0]==Long[].class){ try { if(c[0]==long[].class){ long[] objects = (long[])value; long[] longs = new long[objects.length]; for(int i = 0; i < longs.length; i++){ longs[i] = Long.parseLong(String.valueOf(objects[i])); } method.invoke(dx,(Object)longs); } if(c[0]==Long[].class) { Long[] objects = (Long[])value; Long[] longs = new Long[objects.length]; for (int i = 0; i < longs.length; i++) { longs[i] = Long.parseLong(String.valueOf(objects[i])); } method.invoke(dx, (Object) longs); } break ws; }catch (Exception e){ e.printStackTrace(); } } else if(c[0]==float[].class||c[0]==Float[].class){ try { if(c[0]==float[].class){ float[] objects = (float[])value; float[] floats = new float[objects.length]; for(int i = 0; i < floats.length; i++){ floats[i] = Float.parseFloat(String.valueOf(objects[i])); } method.invoke(dx,(Object)floats); } if(c[0]==Float[].class) { Float[] objects = (Float[])value; Float[] floats = new Float[objects.length]; for (int i = 0; i < floats.length; i++) { floats[i] = Float.parseFloat(String.valueOf(objects[i])); } method.invoke(dx, (Object)floats); } break ws; }catch (Exception e){ e.printStackTrace(); } } else if(c[0]==double[].class||c[0]==Double[].class){ try { if(c[0]==double[].class){ double[] objects = (double[])value; double[] doubles = new double[objects.length]; for(int i = 0; i < doubles.length; i++){ doubles[i] = Double.parseDouble(String.valueOf(objects[i])); } method.invoke(dx,(Object)doubles); } if(c[0]==Double[].class) { Double[] objects = (Double[])value; Double[] doubles = new Double[objects.length]; for (int i = 0; i < doubles.length; i++) { doubles[i] = Double.parseDouble(String.valueOf(objects[i])); } method.invoke(dx, (Object)doubles); } break ws; }catch (Exception e){ e.printStackTrace(); } } else if(c[0]==String[].class){ try { Object[] objects = (Object[])value; String[] strings = new String[objects.length]; for(int i = 0; i < strings.length; i++){ strings[i] = String.valueOf(objects[i]); } method.invoke(dx,(Object)strings); break ws; }catch (Exception e){ e.printStackTrace(); } }else if(c[0]==BigInteger[].class){ try { Object[] objects = (Object[])value; BigInteger[] bigIntegers = new BigInteger[objects.length]; for(int i = 0; i < bigIntegers.length; i++){ bigIntegers[i] = BigInteger.valueOf(Long.parseLong(String.valueOf(objects[i]))); } method.invoke(dx,(Object)bigIntegers); break ws; }catch (Exception e){ e.printStackTrace(); } }else if(c[0]==BigDecimal[].class){ try { Object[] objects = (Object[])value; BigDecimal[] bigDecimals = new BigDecimal[objects.length]; for(int i = 0; i < bigDecimals.length; i++){ bigDecimals[i] = new BigDecimal(String.valueOf(objects[i])); } method.invoke(dx,(Object)bigDecimals); break ws; }catch (Exception e){ e.printStackTrace(); } }else if(c[0]==Date[].class){ try { Object[] objects = (Object[])value; Date[] dates = new Date[objects.length]; for(int i = 0; i < dates.length; i++) { dates[i] = (Date) objects[i]; } method.invoke(dx,(Object)dates); break ws; }catch (Exception e){ e.printStackTrace(); } }else if(c[0].isArray()){ Object[] objects = (Object[]) value; for(int i = 0; i < objects.length; i++){ try { objects[i] = mapToObject(objects[i],Class.forName(c[0].toString().substring(8,c[0].toString().length()-1))); }catch (Exception e){ e.printStackTrace(); } } try { method.invoke(dx,(Object)objects); }catch (Exception e){ e.printStackTrace(); } }else if(Collection.class.isAssignableFrom(c[0])){ String typeString = null; try { Field field = clazz.getDeclaredField(sKey); if(field != null){ ParameterizedType parameterizedType = (ParameterizedType)field.getGenericType(); Type type = parameterizedType.getActualTypeArguments()[0]; typeString = type.getTypeName(); } }catch (Exception e){ e.printStackTrace(); } if(typeString==null){ if(value instanceof Collection){ try { method.invoke(dx,value); }catch (Exception e){ e.printStackTrace(); } }else { try { List list = new ArrayList(); list.add(value); method.invoke(dx,list); }catch (Exception e){ e.printStackTrace(); } } }else { try { Collection collection = (Collection)value; Collection collection1 = new ArrayList(); Class clazz1 = Class.forName(typeString); Iterator iterator1 = collection.iterator(); while (iterator1.hasNext()){ Object o = iterator1.next(); if(o instanceof Map&&!Map.class.isAssignableFrom(clazz1)){ collection1.add(mapToObject(o,clazz1)); }else { collection1.add(o); } } method.invoke(dx,collection1); }catch (Exception e){ if(value instanceof Collection){ try { method.invoke(dx,value); }catch (Exception e1){ e.printStackTrace(); } }else { try { List list = new ArrayList(); list.add(value); method.invoke(dx,list); }catch (Exception e1){ e.printStackTrace(); } } } } /* List list = (List) value; for(int i = 0; i < list.size(); i++){ if(list.get(i) instanceof Map){ try { Field field = clazz.getDeclaredField(sKey); if(field != null){ ParameterizedType parameterizedType = (ParameterizedType)field.getGenericType(); Type type = parameterizedType.getActualTypeArguments()[0]; if(type != null){ Object o = mapToObject(list.get(i),Class.forName(type.getTypeName())); } } }catch (Exception e){ e.printStackTrace(); } //c[0].getComponentType() } }*/ } else { if(value instanceof Map){ try { method.invoke(dx,mapToObject(value,c[0])); break ws; }catch (Exception e){ e.printStackTrace(); } } } } } } } return dx; } public static void main(String[] args) { Map map = new HashMap(); map.put("key","fsdfsdfsdf"); map.put("code","156461"); String str = mapToString(map); System.out.println(str); } public static String mapToString(Map map){ StringBuffer stringBuffer = new StringBuffer(); Set<Map.Entry> set = map.entrySet(); Iterator<Map.Entry> iterator = set.iterator(); int i = 0; while (iterator.hasNext()){ Map.Entry entry = iterator.next(); Object object = entry.getValue(); if(object.getClass().isArray()){ Object[] objects = (Object[]) object; for(int k = 0; k < objects.length; k++){ object = objects[k]; if( object.getClass().isPrimitive()){ stringBufferAdd(stringBuffer,entry.getKey(),object); } if(object instanceof Number){ stringBufferAdd(stringBuffer,entry.getKey(),object); } if(object instanceof Character){ stringBufferAdd(stringBuffer,entry.getKey(),object); } if(object instanceof String){ stringBufferAdd(stringBuffer,entry.getKey(),object); } } }else { if( object.getClass().isPrimitive()){ stringBufferAdd(stringBuffer,entry.getKey(),object); } if(object instanceof Number){ stringBufferAdd(stringBuffer,entry.getKey(),object); } if(object instanceof Character){ stringBufferAdd(stringBuffer,entry.getKey(),object); } if(object instanceof String){ stringBufferAdd(stringBuffer,entry.getKey(),object); } } } stringBuffer.delete(0,1); return stringBuffer.toString(); } public static String dateToDate(String date){ char chars[] = date.toCharArray(); List<String> strings = new ArrayList<>(); for(int i = 0; i < chars.length; i++){ StringBuffer stringBuffer = new StringBuffer(); while (i<chars.length&&chars[i]>47&&chars[i]<58){ stringBuffer.append(chars[i]); i++; } if(stringBuffer.length()>0){ strings.add(stringBuffer.toString()); } } if(strings.size()>5){ return strings.get(0)+"-"+strings.get(1)+"-"+strings.get(2)+" "+strings.get(3)+":"+strings.get(4)+":"+strings.get(5); } if(strings.size()>2){ return strings.get(0)+"-"+strings.get(1)+"-"+strings.get(2)+" 00:00:00"; } return null; } public static StringBuffer stringBufferAdd(StringBuffer stringBuffer,Object object1,Object object2){ stringBuffer.append('&'); stringBuffer.append(object1); stringBuffer.append('='); stringBuffer.append(object2); return stringBuffer; } }
文章来源: 对springRestTempLate的处理练习