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

article/2025/10/28 1:36:00

请求参数传json对象,后端采用(pojo)接收的前提条件:

1.pom.xml文件加入坐标依赖:jackson-databind
2.Spring Boot 的启动类加注解:@EnableWebMvc
3.Spring Boot 的Controller接受参数采用:@RequestBody
4.postman入参采用json格式

1.pom.xml文件加入坐标:


<dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId><version>2.9.8</version>
</dependency>

2.Spring Boot 启动类:加注解:@EnableWebMvc


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;@SpringBootApplication
@EnableWebMvc
@EnableFeignClients(basePackages = {"com.itheima.platform.common.feign.services.home","com.itheima.bcg.service.fegin","com.itheima.platform.common.feign.services.paperTask","com.itheima.platform.common.feign.services.problem","com.itheima.platform.common.feign.services.auth","com.itheima.platform.common.feign.services.bpmn"})
public class Application {public static void main(String[] args) {SpringApplication.run(Application.class, args);}@Beanpublic RestTemplate restTemplate() {//复杂构造函数的使用SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();requestFactory.setConnectTimeout(300000);// 设置超时requestFactory.setReadTimeout(300000);RestTemplate restTemplate = new RestTemplate();restTemplate.setRequestFactory(requestFactory);return restTemplate;}@Beanpublic ClientHttpRequestFactory simpleClientHttpRequestFactory() {SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();factory.setConnectTimeout(150000);factory.setReadTimeout(50000);return factory;}
}

POJO类:


import lombok.Data;@Data
public class Config {private int seqId;private int sortId;private String interfaceType;private String dicNameFirst;private int dicValueFirst;private String dicNameSecond;private int dicValueSecond;private String dicType;private String isEnable;
}

3.Controller接受参数采用:@RequestBody
Controller类:
备注:为了便于测试:Controller类只写了一个接口(实际开发可不要这样写噢)

/** 请求参数传json对象(POJO)**/@PostMapping(value = "/addTest")@AuthInterceptor("mg:get:addTest")public Result addTest(@RequestBody Config config) {try {return xxxListService.addTest(config);} catch (Exception e) {log.error("Controller addTest is error===:" + e.getMessage(), e);return Result.failure("测试成功");}}

Service类:

	Result addTest(Config config);

ServiceImpl类:

@Overridepublic Result addTest(Config config) {List<Map<String, Object>> res = new ArrayList<>();String interfaceType = config.getInterfaceType();if(interfaceType.equals("add")){xxxListMapper.addTest(config);}else if(interfaceType.equals("del")){xxxListMapper.delTest(config);}else if(interfaceType.equals("modify")){xxxListMapper.modifyTest(config);}else if(interfaceType.equals("sel")){res = xxxListMapper.selTest(config);}return Result.success().result(res);}

Mapper类:

	//新增void addTest(Config config);//删除void delTest(Config config);//修改void modifyTest(Config config);//查询List<Map<String, Object>> selTest(Config config);

Mapper.xml类

<!-- 新增 --><insert id="addTest">INSERT IGNORE INTO xxx_other_list_dic(dicNameFirst,dicValueFirst,dicNameSecond,dicValueSecond,dicType,isEnable)VALUES(#{dicNameFirst},#{dicValueFirst},#{dicNameSecond},#{dicValueSecond},#{dicType},#{isEnable})</insert><!-- 删除 --><select id="delTest">deleteFROM xxx_other_list_dic where<if test = "null != seqId and '' != seqId">seqId = #{seqId}</if></select><!-- 修改 --><update id="modifyTest">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" 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>

4.postman入参采用json格式
postman 接口测试:
新增:
在这里插入图片描述
在这里插入图片描述
修改:

在这里插入图片描述
在这里插入图片描述
查询:
在这里插入图片描述
删除:
在这里插入图片描述
在这里插入图片描述


http://chatgpt.dhexx.cn/article/xdyvwU9w.shtml

相关文章

8.返回JSON数据(Jackson):【@Controller + @ResponseBody 等价于【@RestController】的使用方法 jackson乱码的xml配置

文章目录 Controller类&#xff0c;返回JSON数据(Jackson)【一个不乱码的对象】1. 使用Jackson&#xff0c;需要额外导入pom依赖&#xff1a;jackson-databind2. 配置web.xml&#xff1a;注册DispatcherServlet、自带的过滤器3. 配置springmvc-servlet.xml 【含jackson乱码的解…

SpringBoot默认使用Jackson,它与ObjectMapper的前世今生JSON工具(格式化、JSONObject转对象)

Jackson与ObjectMapper 1、Jackson可以轻松地将Java对象转换成json对象和xml文档&#xff0c;同样也可以将json、xml转换成Java对象&#xff1b; 2、ObjectMapper类是Jackson库的主要类。它称为ObjectMapper的原因是因为它将JSON映射为Java对象&#xff08;序列化&#xff09…

Json☀️ 二、使用 JsonUtility 创建并解析 Json

文章目录 &#x1f7e5; 要生成的 Json&#x1f7e7; 创建 Json 方法&#x1f7e8; 解析 Json 方法 在我们项目中&#xff0c;可能经常用到解析 Json&#xff0c; 但有时也需要存档的工作。那该怎样生成Json呢&#xff1f; 下面我们就以上节 Json 例子为例&#xff0c;来讲解如…

Jackson API指南(*)

一.Jackson 概述 与 依赖 1.市面上用于在 Java 中解析 Json 的第三方库&#xff0c;随便一搜不下几十种&#xff0c;其中的佼佼者有 Google 的 Gson&#xff0c; Alibaba 的 Fastjson以及本文的 jackson。 2.我们在学习一门技术之前&#xff0c;首选要了解这门技术的优劣性&am…

Jackson 工具类使用及配置指南

目录 前言Jackson使用工具类Jackson配置属性Jackson解析JSON数据Jackson序列化Java对象 前言 Json数据格式这两年发展的很快&#xff0c;其声称相对XML格式有很对好处&#xff1a; 容易阅读&#xff1b;解析速度快&#xff1b;占用空间更少。 不过&#xff0c;JSON 和 XML…

docker 命令(2) 容器数据持久化

1. docker ps 查看所有运行状态的容器. 2.docker ps -a 查看全部状态的容器列表。 3. docker rmi image名字 删除一个镜像。 4.容器持久化&#xff1a; 这样的话容器即使删了 但是数据依然在 主机的目录未指定的话 默认就会给你建一个 docker run --name mysql_pro -p 330…

jprofiler 连接 docker中的jvm

jprofiler 官网&#xff1a; Java Profiler - JProfiler 1: docker-compose 指定端口&#xff0c; docker 为 -p 指定端口 2&#xff1a;官网下载 liunx版本及windows版本&#xff1a; 我用的是12.0.4 windows本地 win10安装 liunx 版本 上传是liun服务器 3&#xff1a; 在…

Java中Jackson库操作json的基本操作

这段工作中总会遇到使用Java处理JSON的情况&#xff0c;大部分都使用的是开源工具Jackson实现的。因此总结一下发上来&#xff0c;希望对看到的人有所帮助。 上一篇文档中&#xff0c;我已经讲过Java如何使用Jackson来对Json进行序列化&#xff0c;这边我再稍微回顾一下。 Ja…

java操作k8s api示例:通过java完成对kubenetes原生资源对象(pod、node、namespace、servcie、deployment)和自定义资源对象CRD的增删改查或事件监听

本文目标 基于官方kubernetes-client/java类库&#xff0c;实现通过java完成对kubenetes原生资源对象&#xff08;pod、node、namespace、servcie、deployment&#xff09;和自定义资源对象&#xff08;如&#xff1a;cluster&#xff09;的增删改查或事件监听&#xff08;wat…

Jackson注解 @JsonCreator

当json在反序列化时&#xff0c;默认选择类的无参构造函数创建类对象&#xff0c;当没有无参构造函数时会报错&#xff0c;JsonCreator作用就是指定反序列化时用的无参构造函数。构造方法的参数前面需要加上JsonProperty,否则会报错。 JsonCreatorpublic Person(JsonProperty(&…

Spring中读取本地json文件,并交给Spring容器管理

我们经常在项目开发中遇到项目数据初始化的问题&#xff0c;例如一些超管&#xff0c;管理员账号&#xff1b;或者地图包&#xff0c;电话号码包&#xff0c;之类的东西。放到到一个json文件里面&#xff08;大的数据字典包可以放到搜索引擎里面&#xff0c;改情况本文不做讨论…

导入jackson-databind依赖后tomcat报错Cannot resolve com.fasterxml.jackson.core:jackson-databind

1》解决步骤&#xff1a; 项目启动前先打开tomcat里面的conf里面的catalina.properties文件夹 如&#xff1a;apache-tomcat-8.5.83\conf\catalina.properties 后面在里面找到如下&#xff1a;红线处 往下找到如下&#xff1a; 将上面的 红波浪线内容添加到后面&#xff1a…

通过Kettle工具解析Json接口数据并且保存到数据库中的详细操作

最近接到一个业务需求&#xff0c;要把一个Json接口数据获取下来并且保存到数据库中&#xff0c;考虑到应用代码实现功能需要耗费一定时间和精力&#xff0c;一旦需要修改&#xff0c;就得重启项目等。于是就选择利用Kettle工具来实现这个业务功能&#xff0c;将其从项目源码中…

树莓派安装wiringpi显示不存在解决方法

环境&#xff1a;树莓派4B 使用sudo apt-get install wiringpi 指令安装wiringpi包时&#xff0c;出现下面的提示&#xff1a; Reading package lists... Done Building dependency tree... Done Reading state information... Done Package wiringpi is not available, but …

[ARM+Linux] 基于wiringPi库的串口通信

wiringOP-master/examples/serialTest.c中&#xff0c;wiringPi库中自带的串口程序&#xff1a; #include <stdio.h> #include <string.h> #include <errno.h>#include <wiringPi.h> #include <wiringSerial.h>int main () {int fd ;int count …

树莓派 Raspberry Pi —— wiringPi库安装

树莓派的GPIO可以像单片机&#xff08;51单片机&#xff0c;Arduino&#xff0c;STM32等&#xff09;一样进行IO控制&#xff08;输出高、低电平&#xff0c;IIC&#xff0c;SPI&#xff0c;串口通信&#xff0c;PWM输出等&#xff09;&#xff0c;在此使用常用的WiringPi库来进…

树莓派 wiringPi 库

wiringPi是一个很棒的树莓派IO控制库&#xff0c;使用C语言开发&#xff0c;提供了丰富的接口&#xff1a;GPIO控制&#xff0c;中断&#xff0c;多线程&#xff0c;等等 检查树莓派是否已安装 wiringPi&#xff0c;在树莓派终端输入&#xff1a; gpio -v // 会在终端中输出…

树莓派安装wiringPi

在学习微雪的2-CH CAN FD HAT时&#xff0c;根据官网步骤在树莓派安装wiringPi sudo apt-get install wiringpi #对于树莓派4B可能需要进行升级&#xff1a; wget https://project-downloads.drogon.net/wiringpi-latest.deb&#xff08;此链接安装可能出错&#xff0c;如果出…

解决wiringPi库与64位树莓派之间不兼容的问题

目录 一.问题现象&#xff1a; 二.解决方案&#xff08;网站&#xff09;可以直接点这下载 一.问题现象&#xff1a; 今天在练习wiringPi库的使用时候&#xff0c;在最后编译的时候出现了这个问题 主要问题是这个skipping incompatible&#xff01; skipping incompatible. …

树莓派安装WiringPi以及找不到wiringPi.h文件解决方法(图文教程)

目录 安装WiringPi 失败的过程&#xff1a; 选择的方法&#xff1a; 安装步骤&#xff1a; 找不到wiringPi.h文件解决方法 失败过程&#xff1a; 解决方法&#xff1a; 安装WiringPi 失败的过程&#xff1a; 通过分别使用sudo apt-get install wiringPi 和 wget https…