请求参数传json对象,后端采用(map)接收的前提条件:
1.Spring Boot 的Controller接受参数采用:@RequestBody
2.需要一个Json工具类,将json数据转成Map;
工具类:Json转Map
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.beanutils.PropertyUtils;
import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.*;
/**
* 。
* (1) 使用泛型方法:把json字符串转换为相应的JavaBean对象;
* 转换为普通JavaBean:readValue(json,Student.class);
* (2) List Map转换List 对象:如List,将第二个参数传递为Student;
* (3) List 对象转换List Map:
* [].class.然后使用Arrays.asList();方法把得到的数组转换为特定类型的List;
*
* @param jsonStr
* @param valueType
* @return
*
*/
public final class JsonUtils {
private static ObjectMapper objectMapper;
/**
* (1) 使用泛型方法:把json字符串转换为相应的JavaBean对象;
* 转换为普通JavaBean:readValue(json,Student.class);
*/
public static <T> T readValue(String jsonStr, Class<T> valueType) throws Exception {
if (objectMapper == null) {
objectMapper = new ObjectMapper();
}
return objectMapper.readValue(jsonStr, valueType);
}
/**
*(2).List Map转换List 对象:如List,将第二个参数传递为Student对象;
* map转换为bean
*/
public static Object mapToObject(Map<String, String> map, Class<?> beanClass) throws Exception {
if (map == null)
return null;
Object obj = beanClass.newInstance();
BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
for (PropertyDescriptor property : propertyDescriptors) {
Method setter = property.getWriteMethod();
if (setter != null) {
setter.invoke(obj, map.get(property.getName()));
}
}
return obj;
}
/**
*(3).List 对象转换List Map:
* [].class.然后使用Arrays.asList();方法把得到的数组转换为特定类型的List;
* bean转换为map
*/
public static <T> List<Map<String, Object>> listConvert(List<T> list){
List<Map<String, Object>> list_map = new ArrayList<Map<String, Object>>();
if (CollectionUtils.isNotEmpty(list)) {
list.forEach(item ->{
Map<String, Object> map = null;
try {
map = PropertyUtils.describe(item);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
} catch (InvocationTargetException e) {
throw new RuntimeException(e);
} catch (NoSuchMethodException e) {
throw new RuntimeException(e);
}
list_map.add(map);
});
}
return list_map;
}
}
Controller类:@RequestBody
备注:为了便于测试:Controller类只写了一个接口(实际开发可不要这样写噢)
*
* 请求参数传递json数据:json对象(map)
*
*/
@PostMapping(value = "/addTest")
@AuthInterceptor("mg:get:addTest")
public Result addTest(@RequestBody String param) {
try {
Map<String, Object> paramMap = JsonUtils.readValue(param, Map.class);
return xxxListService.addTest(paramMap);
} catch (Exception e) {
log.error("Controller addTest is error===:" + e.getMessage(), e);
return Result.failure("测试成功");
}
}
Service类:
Result addTest(Map<String, Object> paramMap);
ServiceImpl类:
@Override
public Result addTest(Map<String, Object> paramMap) {
List<Map<String, Object>> res = new ArrayList<>();
String interfaceType = String.valueOf(paramMap.get("interfaceType"));
if(interfaceType.equals("add")){
xxxListMapper.addTest(paramMap);
}
else if(interfaceType.equals("del")){
xxxListMapper.delTest(paramMap);
}
else if(interfaceType.equals("modify")){
xxxListMapper.modifyTest(paramMap);
}
else if(interfaceType.equals("sel")){
res = xxxListMapper.selTest(paramMap);
}
return Result.success().result(res);
}
Mapper类:
//新增
void addTest(Map<String, Object> paramMap);
//删除
void delTest(Map<String, Object> paramMap);
//修改
void modifyTest(Map<String, Object> paramMap);
//查询
List<Map<String, Object>> selTest(Map<String, Object> paramMap);
Mapper.xml类
<!-- 新增 -->
<insert id="addTest" parameterType="map">
INSERT IGNORE INTO xxx_other_list_dic
(dicNameFirst,dicValueFirst,dicNameSecond,dicValueSecond,dicType,isEnable)
VALUES
(#{dicNameFirst},#{dicValueFirst},#{dicNameSecond},#{dicValueSecond},#{dicType},#{isEnable})
</insert>
<!-- 删除 -->
<select id="delTest" parameterType="map">
delete
FROM xxx_other_list_dic where
<if test = "null != seqId and '' != seqId">
seqId = #{seqId}
</if>
</select>
<!-- 修改 -->
<update id="modifyTest" parameterType="map">
update xxx_other_list_dic
<set>
<if test = "null != sortId and '' != sortId">
sortId = #{sortId},
</if>
<if test = "null != isEnable and '' != isEnable">
isEnable = #{isEnable}
</if>
</set>
where
<if test = "null != seqId and '' != seqId">
seqId = #{seqId}
</if>
</update>
<!-- 查询 -->
<select id="selTest" parameterType="map" resultType="map">
SELECT *
FROM xxx_other_list_dic where 1 = 1
<if test="null != dicNameFirst and '' != dicNameFirst">
and dicNameFirst = #{dicNameFirst}
</if>
<if test="null != dicValueFirst and '' != dicValueFirst">
and dicValueFirst = #{dicValueFirst}
</if>
<if test="null != dicNameSecond and '' != dicNameSecond">
and dicNameSecond = #{dicNameSecond}
</if>
<if test="null != dicValueSecond and '' != dicValueSecond">
and dicValueSecond = #{dicValueSecond}
</if>
<if test="null != dicType and '' != dicType">
and dicType = #{dicType}
</if>
<if test="null != isEnable and '' != isEnable">
and isEnable = #{isEnable}
</if>
order by sortId
</select>