请求响应-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";
    }
}
相关推荐
kangkang-1 小时前
PC端基于SpringBoot架构控制无人机(二):MavLink协议
java·spring boot·后端·无人机
麦兜*2 小时前
Spring Boot秒级冷启动方案:阿里云FC落地实战(含成本对比)
java·spring boot·后端·spring·spring cloud·系统架构·maven
魂兮归乡3 小时前
B2、进度汇报(— 25/06/16)
经验分享·spring boot·ai·vue·团队开发·课程设计·web app
Q_Q5110082854 小时前
python的保险业务管理与数据分析系统
开发语言·spring boot·python·django·flask·node.js·php
风象南5 小时前
SpringBoot应用开机自启动与进程守护配置
java·spring boot·后端
顽疲5 小时前
从零用java实现 小红书 springboot vue uniapp(13)模仿抖音视频切换
java·vue.js·spring boot
nightunderblackcat6 小时前
新手向:实现ATM模拟系统
java·开发语言·spring boot·spring cloud·tomcat·maven·intellij-idea
超级小忍6 小时前
Spring Boot 与 Docker 的完美结合:容器化你的应用
spring boot·后端·docker
麦兜*7 小时前
Spring Boot 企业级动态权限全栈深度解决方案,设计思路,代码分析
java·spring boot·后端·spring·spring cloud·性能优化·springcloud
蹦跑的蜗牛8 小时前
Spring Boot 整合 RabbitMQ
spring boot·rabbitmq