demand
The front and back ends separate the project, and the front end is encapsulated by post requests into an Application/JSON data request back end. The backend uses RestTemplate to call third party platforms in application/ X-www-form-urlencoded format by imitating form forms with signatures.
The object needs to be converted to MultiValueMap by reflection
Because RestTemplate can only call the form submission with MultiValueMap, the Fastjson I’m using can’t directly convert the instance object to the key-value MultiValueMap. Reflection object to MultiValueMap as follows:
* @param: [obj] * @return: java.util.Map<java.lang.String, java.lang.reflect.Field> * @Author: * @Date: 2021-03-12 */ public static MultiValueMap<String, Object> getMultiValueMap(Object obj) { MultiValueMap<String, Object> multiValueMap = new LinkedMultiValueMap<>(); Class<? > clazz = obj.getClass(); Field[] fields = clazz.getDeclaredFields(); try { for (Field field : fields) { if (needFilterField(field)) { continue; } // this method is used to set the access permission. // If the accessible flag is set to true, reflection objects are used without checking Java language permission controls (private, etc.); // If set to false, reflection objects check for Java language permission controls when used. // It is important to note that setting it to true raises security concerns. field.setAccessible(true); multiValueMap.add(field.getName(), field.get(obj)); } getParentMap(clazz, multiValueMap, obj); } catch (Exception e) { e.printStackTrace(); } return multiValueMap; } /** * needFilterField * @return */ private static Boolean needFilterField(field field) {// Filter static attribute if (Modifier.isStatic(field.getModifiers())) { return true; } if (Modifier. IsTransient (field.getModifiers())) {return true; } return false; }Copy the code
If it’s a property that requires a parent class you need to recursively call the parent class by reflection
@param clazz @param multiValueMap */ private static void getParentMap(Class<? > clazz, MultiValueMap<String, Object> multiValueMap, Object obj) { Class<? > superClazz = clazz.getSuperclass(); if (superClazz ! = null) { Field[] superFields = superClazz.getDeclaredFields(); try { for (Field field : superFields) { if (needFilterField(field)) { continue; } field.setAccessible(true); multiValueMap.add(field.getName(), field.get(obj)); } } catch (IllegalAccessException e) { e.printStackTrace(); } getParentMap(clazz, multiValueMap, obj); }}Copy the code
The incoming object will then be converted to the incoming object in key-value mode
The MultiValueMap object after the transfer
RestTemplate call
The code is as follows:
@Autowired private RestTemplate restTemplate; /*** * @description :RetResponse encapsulates its own unified return object * @param: [returnStr] * @return: cn.thinkjoy.springboot.logicunified.dto.response.RetResponse * @Author: leichunhong * @Date: 2021-02-23 */ @Override public RetResponse getRetResponse(String url, MultiValueMap<String, Object> multiValueMap) { RetResponse retResponse=new RetResponse(); try{ HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED); HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(multiValueMap, httpHeaders); ResponseEntity<String> response = restTemplate.postForEntity(getPath(url), requestEntity, String.class); retResponse = JSONObject.parseObject(response, RetResponse.class); return retResponse; }catch (Exception e){ logger.error(e.getMessage(), e); retResponse.setRet(-1); Retresponse.setmsg (" HTTP request error "); return retResponse; }}Copy the code