请求响应-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";
    }
}
相关推荐
c1s2d3n4cs9 小时前
Qt模仿nlohmann::json进行序列化和反序列化
开发语言·qt·json
一只很酸de橘子9 小时前
PostgreSQL 生成 JSON 字符串
postgresql·json
河阿里12 小时前
SpringBoot:项目启动速度深度优化
java·spring boot·后端
阿丰资源13 小时前
基于SpringBoot的企业客户管理系统(附源码)
java·spring boot·后端
两年半的个人练习生^_^13 小时前
SpringBoot 项目使用 Jasypt 实现配置文件敏感信息加密
java·spring boot·后端
YOU OU16 小时前
SpringBoot
java·spring boot·spring
凯瑟琳.奥古斯特16 小时前
SpringBoot快速入门指南
java·开发语言·spring boot·后端·spring
代码漫谈16 小时前
基于 Spring Boot 3.2.x 的 Actuator 监控指南:从健康检查到企业级监控体系
java·spring boot·actuator 监控
Kiyra16 小时前
Query Rewrite 不是越智能越好:RAG 检索的精确词保护与动态召回
redis·websocket·junit·单元测试·json
Msshu12317 小时前
XSP25全协议 100W PD快充诱骗芯片_串口读电压电流信息
hive·云计算·json·database·memcached