个人知识库
1. 格式化json输出的日期格式
可以使用@JsonFormat注解以设置特定字段的格式:
在字段上面使用@JsonFormat(pattern=“yyyy-MM-dd HH:mm:ss”,timezone = “GMT+8”)
列如:
@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
private Date createdTime; @JsonFormat(pattern="yyyy-MM-dd
HH:mm:ss",timezone = "GMT+8") private Date updatedTime;
2.Springcloud微服务中多模块重复代码重构成公共模块的实现
参考博客:
Springcloud微服务中多模块重复代码重构成公共模块的实现
3.ReturnT(统一返回值)
源代码:
4.数据表表关系对应图解
5.java中如何判断字符串是否为json格式
6.HTTP请求和响应的压缩传输(湖南随手拍业务需求)
7.@Validated注解详解
8.java8 stream.skip().limit() 实现分页功能
.java8 stream.skip().limit() 实现分页功能
9. java8 使用HttpServletRequest对象来手动获取POST请求的请求体数据
需求描述:一般我们想要拿到前端请求的数据,可以在后端的Spring框架中,使用@RequestBody注解来接收POST请求的请求体参数。这个注解可以用在Controller层的方法参数上,将请求体中的JSON数据直接映射到对应的Java对象中。
@RestController
@RequestMapping("/api")
public class YourController {
@PostMapping("/yourEndpoint")
public ResponseEntity<String> yourEndpoint(@RequestBody YourDTO yourDTO) {
// 处理 yourDTO 对象中的数据
// yourDTO 包含了前端传来的请求体中的数据
// 返回响应给前端
return ResponseEntity.ok("Received the request body");
}
}
当然,如果你不想使用@RequestBody注解,你可以使用HttpServletRequest对象来手动获取POST请求的请求体数据。这种方式可以让你直接从请求中获取原始的JSON数据。
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
@RestController
@RequestMapping("/api")
public class YourController {
@PostMapping("/yourEndpoint")
public ResponseEntity<String> yourEndpoint(HttpServletRequest request) {
StringBuilder sb = new StringBuilder();
BufferedReader reader = null;
try {
reader = request.getReader();
String line;
while ((line = reader.readLine()) != null) {
sb.append(line);
}
} catch (IOException e) {
// 异常处理
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
// 异常处理
}
}
}
String requestBody = sb.toString();
// requestBody 就是请求体的原始 JSON 数据
// 处理 requestBody 中的数据
// 返回响应给前端
return ResponseEntity.ok("Received the request body");
}
}
通过这种方式拿到的是请求体的原始数据,是一个 JSON 格式的字符串。需要自行解析该字符串来提取其中的数据。
10. 数据注释//TODO的作用
11. FilenameUtils.getExtension的作用
作用: 获取图片或者文件的后缀