使用@JsonView注解控制返回的Json属性
我也是刚看到原来还可以这么玩,但是我还是习惯使用Dto,我总感觉这样做的话实体类耦合程度有点高。还是记录以下,万一今后用到了呢
⒈在实体类中使用接口来声明该实体类的多个视图。
⒉在实体类的属性get方法上指定该属性在那个视图中呈现。
1 package cn.coreqi.security.entities; 2 3 import com.fasterxml.jackson.annotation.JsonView; 4 5 public class User { 6 public interface UserSimpleView{}; //简单视图 7 public interface UserDetailView extends UserSimpleView{}; //高级视图 8 9 private Long id; 10 private String username; 11 private String password; 12 private Integer enabled; 13 14 public User() { 15 } 16 17 public User(Long id, String username, String password, Integer enabled) { 18 this.id = id; 19 this.username = username; 20 this.password = password; 21 this.enabled = enabled; 22 } 23 24 @JsonView(UserSimpleView.class) 25 public Long getId() { 26 return id; 27 } 28 29 public void setId(Long id) { 30 this.id = id; 31 } 32 33 @JsonView(UserSimpleView.class) 34 public String getUsername() { 35 return username; 36 } 37 38 public void setUsername(String username) { 39 this.username = username; 40 } 41 42 @JsonView(UserDetailView.class) 43 public String getPassword() { 44 return password; 45 } 46 47 public void setPassword(String password) { 48 this.password = password; 49 } 50 @JsonView(UserSimpleView.class) 51 public Integer getEnabled() { 52 return enabled; 53 } 54 55 public void setEnabled(Integer enabled) { 56 this.enabled = enabled; 57 } 58 59 @Override 60 public String toString() { 61 return "User{" + 62 "id=" + id + 63 ", username='" + username + '\'' + 64 ", password='" + password + '\'' + 65 ", enabled=" + enabled + 66 '}'; 67 } 68 }
⒊在控制器的Action方法上使用@JsonView注解声明该Action返回的实体类视图。
1 package cn.coreqi.security.controller; 2 3 import cn.coreqi.security.entities.User; 4 import com.fasterxml.jackson.annotation.JsonView; 5 import org.springframework.web.bind.annotation.GetMapping; 6 import org.springframework.web.bind.annotation.PathVariable; 7 import org.springframework.web.bind.annotation.RestController; 8 import java.util.ArrayList; 9 import java.util.List; 10 11 @RestController 12 public class UserController { 13 @GetMapping("/users") 14 @JsonView(User.UserSimpleView.class) 15 public List<User> query(){ 16 List<User> users = new ArrayList<>(); 17 users.add(new User(1L,"fanqi","fanqi",1)); 18 users.add(new User(2L,"fanqi","fanqi",1)); 19 users.add(new User(3L,"fanqi","fanqi",1)); 20 return users; 21 } 22 23 @GetMapping("/user/{id:\\d+}") //使用正则指定Id为数字 24 @JsonView(User.UserDetailView.class) 25 public User getInfo(@PathVariable String id){ 26 return new User(1L,"fanqi","fanqi",1); 27 } 28 }
作者:奇
出处:https://www.cnblogs.com/fanqisoft/p/10599480.html
版权:本作品采用「本文版权归作者和博客园共有,欢迎转载,但必须给出原文链接,并保留此段声明,否则保留追究法律责任的权利。」许可协议进行许可。
分类:
Spring Boot
, Spring MVC
如果文章内容对您有所帮助,欢迎赞赏.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!