当前位置: 首页 > news >正文

【Spring Boot】请求参数传json对象,后端采用(map)CRUD案例(101)

请求参数传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>,将第二个参数传递为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>,将第二个参数传递为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类:

@Overridepublic 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">deleteFROM 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>

Postman 接口测试:
新增:
在这里插入图片描述
在这里插入图片描述
修改:
在这里插入图片描述
在这里插入图片描述
查询:
在这里插入图片描述
删除:
在这里插入图片描述
在这里插入图片描述

http://www.lryc.cn/news/108533.html

相关文章:

  • 微软开测“Moment4”启动包:Win11 23H2要来了
  • SpringCloud《Eureka、Ribbon、Feign、Hystrix、Zuul》作用简单介绍
  • 运维项目—K8S命令
  • java框架整合Springmvc+···+maven
  • 答辩PPT怎么做?在线PPT软件哪个好?
  • Astro + Vercel 快速搭建自己的博客网站
  • TensorFlow
  • 【iOS RunLoop】
  • 阿里云平台注册及基础使用
  • Mr. Cappuccino的第58杯咖啡——MacOS配置Maven和Java环境
  • linux Ubuntu 更新镜像源、安装sudo、nvtop
  • LUN映射出错导致写操作不互斥的服务器数据恢复案例
  • Android 仿京东头部滚动头像动态变化
  • 高频交易学习——上期SimNow开通
  • 电力巡检无人机助力迎峰度夏,保障夏季电力供应
  • UOS环境python3.7及pyqt5安装
  • SEO优化:提升网站排名与流量的关键策略
  • flutter-GridView使用
  • Unity Shader编辑器工具类ShaderUtil 常用函数和用法
  • 详解Spring中涉及的技术
  • 阿里云ssl免费数字证书快过期 如何更换
  • 利用OpenCV实现图像拼接
  • 【java安全】无Commons-Collections的Shiro550反序列化利用
  • CSS 滚动条
  • Linux: security: openssh: sshd 出现defunct的一种情况
  • Self-regulating Prompts: Foundational Model Adaptation without Forgetting
  • 平时工资不够用?推荐4种适合工作之余做的兼职副业!
  • 21.Netty源码之编码器
  • Linux 快速创建桌面图标
  • 数据结构—哈夫曼树及其应用