@JsonView的使用

简介: @JsonView的使用

@JsonView的使用


前言

版本说明

platform-bom=Cairo-SR7


相关链接:



实战演练

@JsonView 使用步骤


  1. 使用接口来声明多个视图
  2. 在值对象的get方法山指定视图
  3. 在 Controller 方法上指定视图

User 类

package top.simba1949.common;
import com.fasterxml.jackson.annotation.JsonView;
import lombok.Setter;
import lombok.ToString;
/**
 * @Author Theodore
 * @Date 2019/12/2 15:32
 */
@Setter
@ToString
public class User {
    /**
     * 1. 使用接口来声明多个视图
     */
    public interface UserSimpleView{}
    public interface UserDetailView extends UserSimpleView{}
    private String username;
    private String password;
    /**
     * 2. 在值对象的get方法山指定视图
     */
    @JsonView(UserSimpleView.class)
    public String getUsername() {
        return username;
    }
    @JsonView(UserDetailView.class)
    public String getPassword() {
        return password;
    }
}


Controller

package top.simba1949.controller;
import com.fasterxml.jackson.annotation.JsonView;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.simba1949.common.User;
import java.util.ArrayList;
import java.util.List;
/**
 * @Author Theodore
 * @Date 2019/12/2 15:29
 */
@RestController
@RequestMapping("user")
public class UserController {
    /**
     *
     * @param username
     * @param pageable org.springframework.data.domain.Pageable 分页对象
     * @return
     * 返回结果如下 [{"username":null},{"username":null},{"username":null}]
     */
    @GetMapping("list1")
    @JsonView(User.UserSimpleView.class)
    public List<User> query(String username, Pageable pageable){
        List<User> users = new ArrayList<>();
        users.add(new User());
        users.add(new User());
        users.add(new User());
        return users;
    }
    /**
     *
     * @param username
     * @return
     *  返回结果如下 [{"username":null,"password":null},{"username":null,"password":null},{"username":null,"password":null}]
     */
    @GetMapping("list2")
    @JsonView(User.UserDetailView.class)
    public List<User> query(String username){
        List<User> users = new ArrayList<>();
        users.add(new User());
        users.add(new User());
        users.add(new User());
        return users;
    }
}



目录
相关文章
@JsonView的使用,entity中指定向前台返回哪些字段
使用步骤: 1.使用接口来声明多个视图      2.在值对象的get方法上指定视图   3.在Controller方法上指定视图
888 0
|
3月前
|
JSON PHP 数据格式
|
3月前
|
JSON JavaScript 前端开发
JavaScript 如何对 JSON 数据进行冒泡排序?
JavaScript 如何对 JSON 数据进行冒泡排序?
51 0
|
1月前
|
存储 JSON Apache
揭秘 Variant 数据类型:灵活应对半结构化数据,JSON查询提速超 8 倍,存储空间节省 65%
在最新发布的阿里云数据库 SelectDB 的内核 Apache Doris 2.1 新版本中,我们引入了全新的数据类型 Variant,对半结构化数据分析能力进行了全面增强。无需提前在表结构中定义具体的列,彻底改变了 Doris 过去基于 String、JSONB 等行存类型的存储和查询方式。
揭秘 Variant 数据类型:灵活应对半结构化数据,JSON查询提速超 8 倍,存储空间节省 65%
|
2月前
|
XML 机器学习/深度学习 JSON
在火狐浏览器调ajax获取json数据时,控制台提示“XML 解析错误:格式不佳”。
在火狐浏览器调ajax获取json数据时,控制台提示“XML 解析错误:格式不佳”。
29 0
在火狐浏览器调ajax获取json数据时,控制台提示“XML 解析错误:格式不佳”。
|
2天前
|
JSON 数据可视化 定位技术
python_将包含汉字的字典数据写入json(将datav的全省数据中的贵州区域数据取出来)
python_将包含汉字的字典数据写入json(将datav的全省数据中的贵州区域数据取出来)
7 0
|
15天前
|
存储 JSON JavaScript
「Python系列」Python JSON数据解析
在Python中解析JSON数据通常使用`json`模块。`json`模块提供了将JSON格式的数据转换为Python对象(如列表、字典等)以及将Python对象转换为JSON格式的数据的方法。
31 0
|
19天前
|
存储 JSON 数据挖掘
python逐行读取txt文本中的json数据,并进行处理
Python代码示例演示了如何读取txt文件中的JSON数据并处理。首先,逐行打开文件,然后使用`json.loads()`解析每一行。接着,处理JSON数据,如打印特定字段`name`。异常处理包括捕获`JSONDecodeError`和`KeyError`,确保数据有效性和字段完整性。将`data.txt`替换为实际文件路径运行示例。
14 2
|
1月前
|
JSON 数据格式
糊涂工具类(hutool)post请求设置body参数为json数据
糊涂工具类(hutool)post请求设置body参数为json数据
65 1