欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 财经 > 金融 > 请求响应-05.请求-日期参数JSON参数

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

2025/4/19 14:43:22 来源:https://blog.csdn.net/qq_45055856/article/details/142291091  浏览:    关键词:请求响应-05.请求-日期参数JSON参数

一.日期参数

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

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 

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";}
}

 

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词