我也是刚看到原来还可以这么玩,但是我还是习惯使用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. }

使用@JsonView注解控制返回的Json属性的更多相关文章

  1. @JsonView注解指定返回的model类中显示的字段

    1.User类 package com.imooc.model; import com.fasterxml.jackson.annotation.JsonView; /** * @author oy ...

  2. ASP.NET Web API 通过参数控制返回类型(JSON|XML)

    一个很实用的技巧,可以在访问web api服务的时候指定返回数据的格式类型,比如 json 或者 xml. 因为 web api 默认返回的是XML格式,但是现在json 比较流行,同时网上也有其他的 ...

  3. SpringMvc返回报文形式的控制-验证方法: JSON or HTML or XML

    首先,请求通过accept请求头声明了支持的返回格式 然后,框架根据该请求头和代码实现(注解)选择了对应的MessageConverter处理返回! 一.验证过程 1.返回html 1.1.请求组装 ...

  4. SpringMVC 返回的 json 中去除某些不必要的属性

    修改返回的Model,在对应的属性的get方法上使用 com.fasterxml.jackson.annotation.JsonIgnore 注解即可. 如 @JsonIgnore(true) pub ...

  5. 解决@ResponseBody注解返回的json中文乱码问题

    1. 简介 主要解决@ResponseBody注解返回的json中文乱码问题. 2.解决方案 2.1mvc加上注解(推荐此方法) 在mvc配置文件中假如下面配置(写在 <mvc:annotati ...

  6. 控制类名(className 属性)设置或返回class属性

    控制类名(className 属性) className 属性设置或返回元素的class 属性. 语法: object.className = classname 作用: 1.获取元素的class 属 ...

  7. jquery ajax 返回的json对象 新增属性值(干货)

    $.ajax({ type:"GEt'; url:"你的地址", data:{"你的字段","字段值"} success:funt ...

  8. jackSon注解– @JsonInclude 注解不返回null值字段

    @Data @JsonInclude(JsonInclude.Include.NON_NULL) public class OrderDTO { private String orderId; @Js ...

  9. 【shiro】2.spring整合shiro,注解控制shiro用户/角色/权限And/OR,没有权限跳转到固定页面

    这几天粗浅的把shiro整合到spring中,并且注解控制shiro用户/角色/权限And/OR 步骤: 1.首先maven搭建web项目 2.创建数据库 user/role/authority 其中 ...

随机推荐

  1. centos6.7不联网的情况下安装配置本地yum源

    1  cd / 2 mkdir -p /app/ios 3  cd /opt     mkdir ios 4   把下载好的centos-6.7-x86_64-bin-dvd1.iso  上传到 /o ...

  2. 降维方法PCA与SVD的联系与区别

    在遇到维度灾难的时候,作为数据处理者们最先想到的降维方法一定是SVD(奇异值分解)和PCA(主成分分析). 两者的原理在各种算法和机器学习的书籍中都有介绍,两者之间也有着某种千丝万缕的联系.本文在简单 ...

  3. MyBatis-注解方式整合SSM

    Spring.Spring MVC.MyBatis 整合 一.依赖 <?xml version="1.0" encoding="UTF-8"?> & ...

  4. Android开发入门经典实例

    开发实例概述 今天带大家做一个简单的Android App,这个App会显示创新工程实践老师们的照片和信息,不妨先看一看效果: 虽然这个App非常简单,但是涉及到了Android开发中的一些关键知识, ...

  5. swagger使用一新手篇

    本文转自:http://javatech.wang/index.php/archives/74/ 先简单介绍下项目环境: JDK1.7 Spring 3.2.2 swagger-springmvc 1 ...

  6. idea创建父子工程

    第一步:创建一个新的父工程father:file—–>new—->project ,注意要选maven,Create from archetype不要勾选.next填写GroupId .A ...

  7. 【leetcode-51,52】 N皇后,N皇后 II

     N皇后(hard) n 皇后问题研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并且使皇后彼此之间不能相互攻击. 上图为 8 皇后问题的一种解法. 给定一个整数 n,返回所有不同的 n 皇后问题 ...

  8. 标签中的onclick调用js方法传递多个参数的解决方案

    1.JS方法 <script type="text/javascript"> funcation cc(parameter1,parameter2,parameter3 ...

  9. PHP7 学习笔记(十二)gRPC

    GitHub:https://github.com/grpc/grpc/tree/master/src/php 环境:Linux + php7 1.安装grpc pecl install grpc 编 ...

  10. 分布式配置 SSH 免密登陆

    原地址忘记了,暂且记下 一.准备工作 1) 用客户端工具(ssh client或者putty)连接到linux服务器.在root用户下输入命令 vi /etc/hosts,用vi编辑hosts文件,如 ...