feat:新增JsonView博客
This commit is contained in:
@@ -22,16 +22,22 @@ export default defineConfig({
|
||||
{ text: 'Home', link: '/' },
|
||||
{ text: 'Web前端',
|
||||
items: [
|
||||
{ text: 'Vue3-Common', link: '/Web-Front/Vue3-Common' },
|
||||
{ text: 'JavaScript知识点整理', link: '/Web-Front/JavaScript-Guide' }
|
||||
{
|
||||
text: 'Vue',
|
||||
items: [
|
||||
{ text: 'Vue3-Common', link: '/Web-Front/Vue/Vue3-Common' },
|
||||
]
|
||||
},
|
||||
{
|
||||
text: '其他',
|
||||
items: [
|
||||
{ text: 'JavaScript知识点整理', link: '/Web-Front/Others/JavaScript-Guide' }
|
||||
]
|
||||
},
|
||||
]
|
||||
},
|
||||
{ text: 'Web后端',
|
||||
items: [
|
||||
{ text: 'MySQL知识点', link: '/Web-Backend/MySQL-Knowledge' },
|
||||
{ text: 'Flyway简单使用', link: '/Web-Backend/Flyway' },
|
||||
{ text: 'MyBatis简介和使用', link: '/Web-Backend/MyBatis' },
|
||||
{ text: 'RustFS简介和使用', link: '/Web-Backend/RustFS' },
|
||||
{
|
||||
text: 'SpringBoot',
|
||||
items: [
|
||||
@@ -49,7 +55,17 @@ export default defineConfig({
|
||||
{ text: 'FastAPI基础教程', link: '/Web-Backend/FastAPI/FastAPI-Guide' },
|
||||
{ text: '基于OAuth2的安全验证', link: '/Web-Backend/FastAPI/OAuth2' }
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
text: '其他',
|
||||
items: [
|
||||
{ text: 'MySQL知识点', link: '/Web-Backend/Others/MySQL-Knowledge' },
|
||||
{ text: 'Flyway简单使用', link: '/Web-Backend/Others/Flyway' },
|
||||
{ text: 'MyBatis简介和使用', link: '/Web-Backend/Others/MyBatis' },
|
||||
{ text: 'RustFS简介和使用', link: '/Web-Backend/Others/RustFS' },
|
||||
{ text: 'JsonView和MapStruct使用', link: '/Web-Backend/Others/JsonView-MapStruct' },
|
||||
]
|
||||
},
|
||||
]
|
||||
},
|
||||
{ text: 'DevOps',
|
||||
@@ -72,8 +88,18 @@ export default defineConfig({
|
||||
{
|
||||
text: 'Web前端',
|
||||
items: [
|
||||
{ text: 'Vue3-Common', link: '/Web-Front/Vue3-Common' },
|
||||
{ text: 'JavaScript知识点整理', link: '/Web-Front/JavaScript-Guide' }
|
||||
{
|
||||
text: 'Vue',
|
||||
items: [
|
||||
{ text: 'Vue3-Common', link: '/Web-Front/Vue/Vue3-Common' }
|
||||
]
|
||||
},
|
||||
{
|
||||
text: 'Others',
|
||||
items: [
|
||||
{ text: 'JavaScript知识点整理', link: '/Web-Front/Others/JavaScript-Guide' }
|
||||
]
|
||||
},
|
||||
]
|
||||
},
|
||||
],
|
||||
@@ -81,10 +107,6 @@ export default defineConfig({
|
||||
{
|
||||
text: 'Web后端',
|
||||
items: [
|
||||
{ text: 'MySQL知识点', link: '/Web-Backend/MySQL-Knowledge' },
|
||||
{ text: 'Flyway简单使用', link: '/Web-Backend/Flyway' },
|
||||
{ text: 'MyBatis简介和使用', link: '/Web-Backend/MyBatis' },
|
||||
{ text: 'RustFS简介和使用', link: '/Web-Backend/RustFS' },
|
||||
{ text: 'SpringBoot',
|
||||
items: [
|
||||
{ text: 'Spring IOC简介', link: '/Web-Backend/SpringBoot/SpringIOC' },
|
||||
@@ -100,6 +122,15 @@ export default defineConfig({
|
||||
{ text: 'FastAPI基础教程', link: '/Web-Backend/FastAPI/FastAPI-Guide' },
|
||||
{ text: '基于OAuth2的安全验证', link: '/Web-Backend/FastAPI/OAuth2' }
|
||||
]
|
||||
},
|
||||
{ text: '其他',
|
||||
items: [
|
||||
{ text: 'MySQL知识点', link: '/Web-Backend/Others/MySQL-Knowledge' },
|
||||
{ text: 'Flyway简单使用', link: '/Web-Backend/Others/Flyway' },
|
||||
{ text: 'MyBatis简介和使用', link: '/Web-Backend/Others/MyBatis' },
|
||||
{ text: 'RustFS简介和使用', link: '/Web-Backend/Others/RustFS' },
|
||||
{ text: 'JsonView和MapStruct使用', link: '/Web-Backend/Others/JsonView-MapStruct' },
|
||||
]
|
||||
}
|
||||
],
|
||||
},
|
||||
@@ -113,6 +144,16 @@ export default defineConfig({
|
||||
{ text: 'ElasticSearch部署和使用', link: '/DevOps/ElasticSearch' }
|
||||
]
|
||||
}
|
||||
],
|
||||
'/Flutter/': [
|
||||
{
|
||||
text: 'Flutter',
|
||||
items: [
|
||||
{ text: 'Dart基础教程', link: '/Flutter/DartGuide' },
|
||||
{ text: 'Flutter基础教程', link: '/Flutter/FlutterGuide' },
|
||||
{ text: '实战技巧', link: '/Flutter/Practice' }
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
|
||||
|
||||
194
docs/Web-Backend/Others/JsonView-MapStruct.md
Normal file
194
docs/Web-Backend/Others/JsonView-MapStruct.md
Normal file
@@ -0,0 +1,194 @@
|
||||
# 一、JsonView
|
||||
  JsonView 是 Jackson 提供的注解,用于控制对象序列化/反序列化时包含哪些字段。可以实现:
|
||||
- 不同接口返回不同字段
|
||||
- 敏感字段过滤
|
||||
- 前后端数据分离
|
||||
|
||||
## 1.1 添加依赖
|
||||
```xml
|
||||
<dependencies>
|
||||
<!-- Spring Boot Web -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
```
|
||||
|
||||
## 1.2 使用
|
||||
1. 定义视图接口
|
||||
```java
|
||||
// JsonView 视图定义
|
||||
public class Views {
|
||||
// 基础视图 - 所有接口都包含的字段
|
||||
public interface Basic {}
|
||||
|
||||
// 返回给前端时使用
|
||||
public interface Response extends Basic {}
|
||||
|
||||
// 接收前端数据时使用
|
||||
public interface Request extends Basic {}
|
||||
|
||||
// 管理视图 - 包含所有字段
|
||||
public interface AdminView extends ReadView {}
|
||||
}
|
||||
```
|
||||
|
||||
2. Dto对象使用JsonView
|
||||
```java
|
||||
@Data
|
||||
public class UserDTO {
|
||||
@JsonView(Views.Response.class)
|
||||
private Long id;
|
||||
|
||||
@JsonView(Views.Request.class)
|
||||
@NotBlank(message = "密码不能为空")
|
||||
@Size(min = 6, message = "密码至少6位")
|
||||
private String password;
|
||||
|
||||
@JsonView(Views.Basic.class)
|
||||
@NotBlank(message = "用户名不能为空")
|
||||
private String username;
|
||||
|
||||
@JsonView(Views.Basic.class)
|
||||
@Email(message = "邮箱格式不正确")
|
||||
private String email;
|
||||
|
||||
@JsonView(Views.Basic.class)
|
||||
private String nickname;
|
||||
|
||||
@JsonView(AdminView.class)
|
||||
private Boolean isActive;
|
||||
}
|
||||
```
|
||||
|
||||
3. Controller中使用
|
||||
```java
|
||||
@RestController
|
||||
@RequestMapping("/api/users")
|
||||
public class UserController {
|
||||
|
||||
@Autowired
|
||||
private UserService userService;
|
||||
|
||||
@PostMapping
|
||||
@JsonView(UserDTO.Response.class)
|
||||
public UserDTO createUser(@RequestBody @JsonView(UserDTO.Request.class) UserDTO userDTO) {
|
||||
return userService.createUser(userDTO);
|
||||
}
|
||||
|
||||
@PutMapping("/{id}")
|
||||
@JsonView(UserDTO.Response.class)
|
||||
public UserDTO updateUser(@PathVariable Long id,
|
||||
@RequestBody @JsonView(UserDTO.Request.class) UserDTO userDTO) {
|
||||
return userService.updateUser(id, userDTO);
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
@JsonView(UserDTO.Response.class)
|
||||
public UserDTO getUser(@PathVariable Long id) {
|
||||
return userService.getUserById(id);
|
||||
}
|
||||
|
||||
@GetMapping("/{id}/admin")
|
||||
@JsonView(UserDTO.AdminView.class)
|
||||
public UserDTO getAdminUser(@PathVariable Long id) {
|
||||
return userService.getAdminUserById(id);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
# 二、MapStruct
|
||||
  MapStruct 是一个 Java 注解处理器,用于生成类型安全的 Bean 映射代码:
|
||||
- 编译时生成映射代码,无运行时性能损失
|
||||
- 类型安全
|
||||
- 支持复杂映射
|
||||
|
||||
## 2.1 添加依赖
|
||||
```xml
|
||||
<properties>
|
||||
<org.mapstruct.version>1.5.5.Final</org.mapstruct.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.mapstruct</groupId>
|
||||
<artifactId>mapstruct</artifactId>
|
||||
<version>${org.mapstruct.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.11.0</version>
|
||||
<configuration>
|
||||
<annotationProcessorPaths>
|
||||
<path>
|
||||
<groupId>org.mapstruct</groupId>
|
||||
<artifactId>mapstruct-processor</artifactId>
|
||||
<version>${org.mapstruct.version}</version>
|
||||
</path>
|
||||
</annotationProcessorPaths>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
```
|
||||
|
||||
## 2.2 使用
|
||||
```java
|
||||
import org.mapstruct.*;
|
||||
|
||||
import java.util.List;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
|
||||
@Mapper
|
||||
public interface UserMapper {
|
||||
|
||||
@Mapping(target = "name", source = "userName") // 字段名不同
|
||||
@Mapping(target = "phoneNumber", source = "phone") // 字段名不同
|
||||
@Mapping(target = "description", source = "remark") // 字段名不同
|
||||
@Mapping(target = "statusDesc", ignore = true) // 需要特殊处理
|
||||
@Mapping(target = "createTime", ignore = true) // 需要格式化
|
||||
UserDTO toDTO(User user);
|
||||
|
||||
@Mapping(target = "userName", source = "name")
|
||||
@Mapping(target = "phone", source = "phoneNumber")
|
||||
@Mapping(target = "remark", source = "description")
|
||||
@Mapping(target = "password", ignore = true) // 密码不映射
|
||||
User toEntity(UserDTO dto);
|
||||
|
||||
// 列表映射
|
||||
List<UserDTO> toDTOList(List<User> users);
|
||||
|
||||
// 带默认值的映射
|
||||
@Mapping(target = "name", source = "userName")
|
||||
@Mapping(target = "phoneNumber", source = "phone")
|
||||
@Mapping(target = "description", source = "remark", defaultValue = "暂无描述")
|
||||
@Mapping(target = "statusDesc", expression = "java(convertStatus(user.getStatus()))")
|
||||
@Mapping(target = "createTime", expression = "java(formatTime(user.getCreateTime()))")
|
||||
UserDTO toDTOWithDefault(User user);
|
||||
|
||||
// 自定义转换方法
|
||||
default String convertStatus(Integer status) {
|
||||
if (status == null) return "未知";
|
||||
switch (status) {
|
||||
case 0: return "禁用";
|
||||
case 1: return "正常";
|
||||
case 2: return "锁定";
|
||||
default: return "未知";
|
||||
}
|
||||
}
|
||||
|
||||
// 时间格式化方法
|
||||
default String formatTime(LocalDateTime time) {
|
||||
if (time == null) return "";
|
||||
return time.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
  需要执行`mvn compile`后检查`target/generated-sources`文件夹下是否存在生成的转换代码。
|
||||
@@ -8,25 +8,26 @@ description: Web后端开发技术文档,包含数据库、框架、中间件
|
||||
|
||||
后端开发技术栈的学习笔记和总结。
|
||||
|
||||
## 主要内容
|
||||
|
||||
- [MySQL知识点](/Web-Backend/MySQL-Knowledge)
|
||||
- [Flyway简单使用](/Web-Backend/Flyway)
|
||||
- [MyBatis简介和使用](/Web-Backend/MyBatis)
|
||||
- [RustFS简介和使用](/Web-Backend/RustFS)
|
||||
|
||||
### 🍃 SpringBoot
|
||||
SpringBoot 框架学习系列:
|
||||
|
||||
- [Spring IOC简介](/SpringBoot/SpringIOC)
|
||||
- [SpringBoot启动流程](/SpringBoot/SpringBoot-Start-Process)
|
||||
- [Spring MVC简介](/SpringBoot/SpringMVC)
|
||||
- [SpringBoot Starter原理](/SpringBoot/SpringBootStarter)
|
||||
- [SpringBoot Bean简介](/SpringBoot/SpringBootBean)
|
||||
- [SpringBoot3原生镜像](/SpringBoot/SpringBoot3-GraalVM)
|
||||
- [Spring IOC简介](/Web-Backend/SpringBoot/SpringIOC)
|
||||
- [SpringBoot启动流程](/Web-Backend/SpringBoot/SpringBoot-Start-Process)
|
||||
- [Spring MVC简介](/Web-Backend/SpringBoot/SpringMVC)
|
||||
- [SpringBoot Starter原理](/Web-Backend/SpringBoot/SpringBootStarter)
|
||||
- [SpringBoot Bean简介](/Web-Backend/SpringBoot/SpringBootBean)
|
||||
- [SpringBoot3原生镜像](/Web-Backend/SpringBoot/SpringBoot3-GraalVM)
|
||||
|
||||
### 🐍 FastAPI
|
||||
Python FastAPI 框架学习:
|
||||
|
||||
- [FastAPI基础教程](/FastAPI/FastAPI-Guide)
|
||||
- [基于OAuth2的FastApi安全验证](/FastAPI/OAuth2)
|
||||
- [FastAPI基础教程](/Web-Backend/FastAPI/FastAPI-Guide)
|
||||
- [基于OAuth2的FastApi安全验证](/Web-Backend/FastAPI/OAuth2)
|
||||
|
||||
### 🔄 其他
|
||||
|
||||
- [MySQL知识点](/Web-Backend/Others/MySQL-Knowledge)
|
||||
- [Flyway简单使用](/Web-Backend/Others/Flyway)
|
||||
- [MyBatis简介和使用](/Web-Backend/Others/MyBatis)
|
||||
- [RustFS简介和使用](/Web-Backend/Others/RustFS)
|
||||
- [JsonView和MapStruct使用](/Web-Backend/SpringBoot/JsonView-MapStruct)
|
||||
@@ -8,7 +8,11 @@ description: Web前端开发技术文档
|
||||
|
||||
前端开发技术栈的学习笔记和总结。
|
||||
|
||||
## 主要内容
|
||||
### 🌿 Vue
|
||||
Vue 框架学习:
|
||||
|
||||
- [Vue3-Common](/Web-Front/Vue3-Common)
|
||||
|
||||
### 🔄 其他
|
||||
|
||||
- [JavaScript知识点整理](/Web-Front/JavaScript-Guide)
|
||||
Reference in New Issue
Block a user