请求响应-05.请求-日期参数&JSON参数

一.日期参数

当浏览器发起的请求参数类型是日期参数时,我们通常使用LocalDateTime对象来接收,前面使用@DateTimeFormat注解来完成日期的格式转换(日期时间格式有多种,需要哪种就设置为哪种:如yyyy-MM-dd HH:mm:ss)

java 复制代码
package com.gjw.controller;
/**
 * 目标:掌握原始方式和springboot方式对于简单参数的请求响应
 *      掌握springboot方式对于实体参数的请求响应
 */

import com.gjw.pojo.User;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

@RestController
public class RequestController {
    // 4.请求-时间日期参数
    @RequestMapping("/dateTimeParam")
    public String dateTimeParam(@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime updateTime){
        System.out.println(updateTime);
        return "OK";
    }
}

二.JSON参数

当浏览器的请求类型是JSON参数时,需要JSON数据键名与形参对象属性名相同,需要使用@RequestBody作为标识,因为JSON格式的数据要放在请求体当中携带到服务端中,因此必须发起的是POST请求,因此请求类型应为POST,且请求体应选择RAW,类型为JSON。

JSON中所有的key都要用""引起来

必须保证JSON参数的键名与对象的属性名相同,且JSON格式的数据要封装在一个实体对象当中,前面必须加上注解@RequestBody

java 复制代码
package com.gjw.controller;
/**
 * 目标:掌握原始方式和springboot方式对于简单参数的请求响应
 *      掌握springboot方式对于实体参数的请求响应
 */

import com.gjw.pojo.User;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

@RestController
public class RequestController {
    // 5.JSON格式的参数
    @RequestMapping("/jsonParam")
    public String jsonParam(@RequestBody User user){
        System.out.println(user);
        return "OK";
    }
}
相关推荐
前网易架构师-高司机15 小时前
带标注的羽毛球运动员,裁判,羽毛球识别数据集,识别率84.4%,2879张图,支持yolo,coco json,voc xml,文末有模型训练代码
xml·yolo·json·数据集·羽毛球·裁判
leisoo809716 小时前
股票数据本地化存储实战:JSON、数据库与列式存储的方案对比
jvm·数据库·json
切糕师学AI17 小时前
从压缩到查询:PostgreSQL中JSON数据的存储与处理实践
数据库·postgresql·json
Flittly17 小时前
【雕虫大技】Agent 动态 Skill 供应链安全加固(二):执行隔离沙箱实战
java·spring boot·spring
沐言人生18 小时前
HelloAgentR工程手记1/100天——项目工作流搭建
spring boot·agent·vibecoding
陌上丨18 小时前
Spring Boot 应用类型推断机制:一次从源码到原理的深度探索
java·spring boot·后端
云烟成雨TD19 小时前
Micrometer 系列【33】Spring Boot Micrometer Metrics 自动配置模块解析
spring boot·云原生·micrometer
doubt。20 小时前
大模型prompt工程Zero-Shot与Few-Shot以及json格式
人工智能·深度学习·机器学习·语言模型·json·prompt
用户31268748772021 小时前
接口太慢?Spring Boot 缓存体系 @Cacheable 全链路拆解
spring boot
snow@li21 小时前
SpringBoot:AOP日志切面全景梳理/原理+流程+实战+避坑
java·开发语言·spring boot