当前位置: 首页 > 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 接口测试:
新增:
在这里插入图片描述
在这里插入图片描述
修改:
在这里插入图片描述
在这里插入图片描述
查询:
在这里插入图片描述
删除:
在这里插入图片描述
在这里插入图片描述

相关文章:

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

请求参数传json对象&#xff0c;后端采用&#xff08;map&#xff09;接收的前提条件&#xff1a; 1.Spring Boot 的Controller接受参数采用&#xff1a;RequestBody 2.需要一个Json工具类&#xff0c;将json数据转成Map&#xff1b; 工具类&#xff1a;Json转Map import com…...

微软开测“Moment4”启动包:Win11 23H2要来了

近日&#xff0c; 有用户在Win11最新的7月累积更新中发现&#xff0c;更新文件中已经开始出现了对“Moment4”的引用。 具体来说&#xff0c;在7月累积更新中&#xff0c;微软加入了“Microsoft-Windows-UpdateTargeting-ClientOS-SV2Moment4-EKB”“Microsoft-Windows-23H2Ena…...

SpringCloud《Eureka、Ribbon、Feign、Hystrix、Zuul》作用简单介绍

概述 SpringCloud是一个全家桶&#xff0c;包含多个组件。 本文主要介绍几个重要组件&#xff0c;也就是Eureka、Ribbon、Feign、Hystrix、Zuul这几个组件。 一、业务场景介绍 业务流程&#xff0c;支付订单功能 订单服务改变为已支付订单服务调用库存服务&#xff0c;扣减…...

运维项目—K8S命令

文章目录 一、基本操作1、命名空间kubectl get ns 获取命名空间kubectl get ns default -o yaml 以yaml的格式查看某个nskubectl describe ns hoc-prod 查看某个ns详情1、命名空间与Podkubectl get pods --all-namespaces查看所有命名空间下的所有podkubectl get pod -A查看所有…...

java框架整合Springmvc+···+maven

框架整合: Springmvc Mybatis Shiro&#xff08;权限&#xff09; REST(服务) WebService(服务) JMS(消息) Lucene(搜搜引擎) Quartz(定时调度) Bootstrap Html5&#xff08;支持PC、IOS、Android&#xff09; 系统模块&#xff1a; 1. 用户管理&#xff1a; 用户信…...

答辩PPT怎么做?在线PPT软件哪个好?

又是一年毕业季&#xff0c;相信很多毕业生都开始准备论文答辩&#xff0c;有些同学正在为论文奋夜苦战&#xff0c;有些则是为论文答辩PPT而烦恼。做PPT要用什么软件好呢&#xff1f;这篇文章就来告诉你。 当下有很多PPT制作工具&#xff0c;其中自然也包括Office三件套。这些…...

Astro + Vercel 快速搭建自己的博客网站

Astro 和 Vercel 彼此相得益彰&#xff0c;前者提供出色的开发者体验&#xff0c;用于构建现代静态站点&#xff0c;而后者负责部署和托管代码。 两者结合我们就可以轻轻松松零成本搭建自己的博客网站。查看示例。 步骤 1&#xff0c;创建评论仓库 在部署博客之前&#xff…...

TensorFlow

什么是 TensorFlow TensorFlow是一个开源的机器学习框架&#xff0c;由Google于2015年推出。它被设计用来构建深度神经网络和其他机器学习模型&#xff0c;从而可以实现图像识别、语音识别、自然语言处理、推荐系统、搜索引擎、预测和控制等应用。TensorFlow是一个基于数据流图…...

【iOS RunLoop】

文章目录 前言-什么是RunLoop&#xff1f;默认情况下主线程的RunLoop原理 1. RunLoop对象RunLoop对象的获取 CFRunLoopRef源码部分&#xff08;引入线程相关&#xff09; 2. RunLoop和线程3. RunLoop相关的类RunLoop相关类的实现CFRunLoopModeRef五种运行模式CommonModes CFRun…...

阿里云平台注册及基础使用

首先进入阿里云官网&#xff1a; 阿里云-计算&#xff0c;为了无法计算的价值 点击右上角“登录/注册”&#xff0c;如果没有阿里云账号则需要注册。 注册界面&#xff1a; 注册完成后需要开通物联网平台公共实例&#xff1a; 注册成功后的登录&#xff1a; 同样点击右上角的…...

Mr. Cappuccino的第58杯咖啡——MacOS配置Maven和Java环境

MacOS配置Maven和Java环境 查看Mac使用的是哪个shell下载并准备Maven下载Maven配置前准备 下载并安装JDK下载JDK安装JDK 配置Maven和Java环境添加配置加载配置 验证环境 查看Mac使用的是哪个shell echo $SHELL如果使用的是bash&#xff0c;则使用以下命令 open ~/.bash_profi…...

linux Ubuntu 更新镜像源、安装sudo、nvtop

1.更换镜像源 vi ~/.pip/pip.conf在打开的文件中输入: pip.conf [global] index-url https://pypi.tuna.tsinghua.edu.cn/simple按下:wq保存并退出。 2.安装nvtop 如果输入指令apt install nvtop报错&#xff1a; E: Unable to locate package nvtop 需要更新一下apt&a…...

LUN映射出错导致写操作不互斥的服务器数据恢复案例

服务器数据恢复环境&#xff1a; 某公司的光纤SAN存储系统&#xff0c;6块硬盘组建一组RAID6&#xff0c;划分若干LUN&#xff0c;MAP到不同的SOLARIS操作系统服务器上。 服务器故障&分析&#xff1a; 由于业务增长需要新增应用&#xff0c;工作人员增加了一台IBM服务器&am…...

Android 仿京东头部滚动头像动态变化

UI出了一个新需求&#xff0c;仿京东头部滚动&#xff0c;头像需要动态变化&#xff0c;先来看下京东的是什么效果 我们知道什么效果以后&#xff0c;接下来就想想怎么实现吧&#xff0c;Android常规吸顶折叠布局是由CoordinatorLayoutAppBarLayoutCollapsingToolbarLayout组成…...

高频交易学习——上期SimNow开通

property 是 Python 中的一个装饰器&#xff08;decorator&#xff09;&#xff0c;用于定义类的属性。它可以将方法转换为相应的特性&#xff08;property&#xff09;&#xff0c;从而实现属性的访问和修改控制。 property 装饰器的作用是将一个方法变成一个只读属性&#x…...

电力巡检无人机助力迎峰度夏,保障夏季电力供应

夏季是电力需求量较高的时期&#xff0c;随着高温天气的来临&#xff0c;风扇、空调和冰箱等电器的使用量也大大增加&#xff0c;从而迎来夏季用电高峰期&#xff0c;电网用电负荷不断攀升。为了保障夏季电网供电稳定&#xff0c;供电公司会加强对电力设施设备的巡检&#xff0…...

UOS环境python3.7及pyqt5安装

解决方案尝试 先安装pyqt5依赖项: 1、更新python3.7 sudo add-apt-repository ppadeadsnakes/ppa sudo apt-get update sudo apt-get upgrade sudo apt-get autoremove sudo apt-get install python3.7 sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/…...

SEO优化:提升网站排名与流量的关键策略

导言&#xff1a; 在如今竞争激烈的互联网时代&#xff0c;网站的排名和流量对于企业的在线可见性和业务发展至关重要。搜索引擎优化&#xff08;SEO&#xff09;是一种关键的策略&#xff0c;旨在提高网站在搜索引擎结果页面上的排名&#xff0c;从而增加网站的曝光率和有针对…...

flutter-GridView使用

先看效果 代码实现 import package:app/common/util/k_log_util.dart; import package:app/gen/assets.gen.dart; import package:app/pages/widget/top_appbar.dart; import package:flutter/cupertino.dart; import package:flutter/material.dart; import package:flutter_…...

Unity Shader编辑器工具类ShaderUtil 常用函数和用法

Unity Shader编辑器工具类ShaderUtil 常用函数和用法 Unity的Shader编辑器工具类ShaderUtil提供了一系列函数&#xff0c;用于编译、导入和管理着色器。本文将介绍ShaderUtil类中的常用函数和用法。 编译和导入函数 CompileShader 函数签名&#xff1a;public static bool C…...

浏览器访问 AWS ECS 上部署的 Docker 容器(监听 80 端口)

✅ 一、ECS 服务配置 Dockerfile 确保监听 80 端口 EXPOSE 80 CMD ["nginx", "-g", "daemon off;"]或 EXPOSE 80 CMD ["python3", "-m", "http.server", "80"]任务定义&#xff08;Task Definition&…...

超短脉冲激光自聚焦效应

前言与目录 强激光引起自聚焦效应机理 超短脉冲激光在脆性材料内部加工时引起的自聚焦效应&#xff0c;这是一种非线性光学现象&#xff0c;主要涉及光学克尔效应和材料的非线性光学特性。 自聚焦效应可以产生局部的强光场&#xff0c;对材料产生非线性响应&#xff0c;可能…...

QMC5883L的驱动

简介 本篇文章的代码已经上传到了github上面&#xff0c;开源代码 作为一个电子罗盘模块&#xff0c;我们可以通过I2C从中获取偏航角yaw&#xff0c;相对于六轴陀螺仪的yaw&#xff0c;qmc5883l几乎不会零飘并且成本较低。 参考资料 QMC5883L磁场传感器驱动 QMC5883L磁力计…...

1688商品列表API与其他数据源的对接思路

将1688商品列表API与其他数据源对接时&#xff0c;需结合业务场景设计数据流转链路&#xff0c;重点关注数据格式兼容性、接口调用频率控制及数据一致性维护。以下是具体对接思路及关键技术点&#xff1a; 一、核心对接场景与目标 商品数据同步 场景&#xff1a;将1688商品信息…...

智能在线客服平台:数字化时代企业连接用户的 AI 中枢

随着互联网技术的飞速发展&#xff0c;消费者期望能够随时随地与企业进行交流。在线客服平台作为连接企业与客户的重要桥梁&#xff0c;不仅优化了客户体验&#xff0c;还提升了企业的服务效率和市场竞争力。本文将探讨在线客服平台的重要性、技术进展、实际应用&#xff0c;并…...

P3 QT项目----记事本(3.8)

3.8 记事本项目总结 项目源码 1.main.cpp #include "widget.h" #include <QApplication> int main(int argc, char *argv[]) {QApplication a(argc, argv);Widget w;w.show();return a.exec(); } 2.widget.cpp #include "widget.h" #include &q…...

unix/linux,sudo,其发展历程详细时间线、由来、历史背景

sudo 的诞生和演化,本身就是一部 Unix/Linux 系统管理哲学变迁的微缩史。来,让我们拨开时间的迷雾,一同探寻 sudo 那波澜壮阔(也颇为实用主义)的发展历程。 历史背景:su的时代与困境 ( 20 世纪 70 年代 - 80 年代初) 在 sudo 出现之前,Unix 系统管理员和需要特权操作的…...

Springboot社区养老保险系统小程序

一、前言 随着我国经济迅速发展&#xff0c;人们对手机的需求越来越大&#xff0c;各种手机软件也都在被广泛应用&#xff0c;但是对于手机进行数据信息管理&#xff0c;对于手机的各种软件也是备受用户的喜爱&#xff0c;社区养老保险系统小程序被用户普遍使用&#xff0c;为方…...

HDFS分布式存储 zookeeper

hadoop介绍 狭义上hadoop是指apache的一款开源软件 用java语言实现开源框架&#xff0c;允许使用简单的变成模型跨计算机对大型集群进行分布式处理&#xff08;1.海量的数据存储 2.海量数据的计算&#xff09;Hadoop核心组件 hdfs&#xff08;分布式文件存储系统&#xff09;&a…...

Caliper 配置文件解析:fisco-bcos.json

config.yaml 文件 config.yaml 是 Caliper 的主配置文件,通常包含以下内容: test:name: fisco-bcos-test # 测试名称description: Performance test of FISCO-BCOS # 测试描述workers:type: local # 工作进程类型number: 5 # 工作进程数量monitor:type: - docker- pro…...