请求响应-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";
    }
}
相关推荐
Q_Q5110082852 小时前
python的软件工程与项目管理课程组学习系统
spring boot·python·django·flask·node.js·php·软件工程
2301_793086873 小时前
SpringCloud 02 服务治理 Nacos
java·spring boot·spring cloud
MacroZheng4 小时前
还在用WebSocket实现即时通讯?试试MQTT吧,真香!
java·spring boot·后端
midsummer_woo5 小时前
基于springboot的IT技术交流和分享平台的设计与实现(源码+论文)
java·spring boot·后端
别惹CC6 小时前
Spring AI 进阶之路01:三步将 AI 整合进 Spring Boot
人工智能·spring boot·spring
柯南二号7 小时前
【Java后端】Spring Boot 集成 MyBatis-Plus 全攻略
java·spring boot·mybatis
javachen__8 小时前
SpringBoot整合P6Spy实现全链路SQL监控
spring boot·后端·sql
IT毕设实战小研15 小时前
基于Spring Boot 4s店车辆管理系统 租车管理系统 停车位管理系统 智慧车辆管理系统
java·开发语言·spring boot·后端·spring·毕业设计·课程设计
一只爱撸猫的程序猿16 小时前
使用Spring AI配合MCP(Model Context Protocol)构建一个"智能代码审查助手"
spring boot·aigc·ai编程
甄超锋16 小时前
Java ArrayList的介绍及用法
java·windows·spring boot·python·spring·spring cloud·tomcat