当前位置: 首页 > 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…...

【GUI-Agent】阶跃星辰 GUI-MCP 解读---()---GUI-MCP 整体架构挚

前言 在使用 kubectl get $KIND -o yaml 查看 k8s 资源时&#xff0c;输出结果中包含大量由集群自动生成的元数据&#xff08;如 managedFields、resourceVersion、uid 等&#xff09;。这些信息在实际复用 yaml 清单时需要手动清理&#xff0c;增加了额外的工作量。 使用 kube…...

OpenSimpleLidar开源激光雷达:低成本DIY扫描测距仪完全指南

OpenSimpleLidar开源激光雷达&#xff1a;低成本DIY扫描测距仪完全指南 【免费下载链接】OpenSimpleLidar Open Source scanning laser rangefinder 项目地址: https://gitcode.com/gh_mirrors/op/OpenSimpleLidar OpenSimpleLidar是一款开源激光雷达项目&#xff0c;专…...

前端性能监控指标体系

前端性能监控指标体系&#xff1a;构建高效用户体验的关键 在当今快节奏的互联网时代&#xff0c;用户体验已成为决定产品成败的关键因素之一。前端性能直接影响用户留存率、转化率以及品牌形象&#xff0c;因此建立一套科学的前端性能监控指标体系至关重要。通过实时监控和分…...

Unity发布京东小游戏滴

从 UI 工程师到 AI 应用架构者 13 年前&#xff0c;我的工作是让按钮在 IE6 上对齐&#xff1b; 13 年后&#xff0c;我用 fetch-event-source 订阅大模型的“思维流”&#xff0c;用 OCR 解锁图片中的文字——前端&#xff0c;正在成为 AI 产品的第一道体验防线。 最近&#x…...

Claude顾问策略技术深度解析:Opus 4.6幕后指挥,Sonnet/Haiku高效执行

技术分析&#xff1a;Anthropic顾问策略架构设计与性能优化实现原理 前言&#xff1a;AI Agent架构的革命性突破 2026年3月&#xff0c;Anthropic正式发布Claude"顾问策略"&#xff08;Advisor Strategy&#xff09;&#xff0c;这一技术架构彻底改变了传统AI Agent…...

液压折弯机(全套)2012本科毕业设计

液压折弯机作为金属板材加工领域的核心设备&#xff0c;其全套系统设计直接决定了加工精度与效率。该设备通过液压系统驱动滑块实现垂直运动&#xff0c;配合模具对板材施加压力&#xff0c;使其按预设角度弯曲成型。其核心作用体现在三方面&#xff1a;一是精准控制弯曲角度&a…...

用 Laravel AI SDK 构建多智能体工作流计

1.安装环境准备 1.1.查看物理内存 [rootaiserver ~]# free -m 1.2.操作系统版本 [rootaiserver ~]# cat /etc/redhat-release 1.3.操作系统内存 [rootaiserver ~]# df -h /dev/shm/ 1.4.磁盘空间 [rootaiserver ~]# df -TH [rootaiserver ~]# df -h /tmp/ [rootaiserver ~]# d…...

AI编程时代,人类程序员还剩下什么?堂

故障表现 发现请求集群 demo 入口时卡住&#xff0c;并且对应 Pod 没有新的日志输出 rootce-demo-1:~# kubectl get pods -n deepflow-otel-spring-demo -o wide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NO…...

typecho按分类搜索文章

typecho根据分类搜索文章.jpg 之前我写的soso搜索增强插件其实已经能够根据分类进行搜索内容了&#xff0c;不过需要模板上进行配合&#xff0c;比如我们搜索分类id为2620下关于typecho的文章&#xff0c;需要传递分类id的参数给cat&#xff0c;让插件获取&#xff0c;比如这个…...

PCA9632/PCA9633四通道I²C PWM LED驱动器技术解析

1. PCA9632/PCA9633 四通道IC PWM LED驱动器深度技术解析1.1 芯片定位与工程价值PCA9632与PCA9633是NXP推出的低功耗、高精度IC接口LED驱动芯片&#xff0c;专为RGB/RGBW LED亮度控制场景设计。二者在电气特性和寄存器结构上高度兼容&#xff0c;PCA9632可作为PCA9633的直接硬件…...