我也是刚看到原来还可以这么玩,但是我还是习惯使用Dto,我总感觉这样做的话实体类耦合程度有点高。还是记录以下,万一今后用到了呢

⒈在实体类中使用接口来声明该实体类的多个视图。

⒉在实体类的属性get方法上指定该属性在那个视图中呈现。

 package cn.coreqi.security.entities;

 import com.fasterxml.jackson.annotation.JsonView;

 public class User {
public interface UserSimpleView{};  //简单视图
public interface UserDetailView extends UserSimpleView{};  //高级视图 private Long id;
private String username;
private String password;
private Integer enabled; public User() {
} public User(Long id, String username, String password, Integer enabled) {
this.id = id;
this.username = username;
this.password = password;
this.enabled = enabled;
} @JsonView(UserSimpleView.class)
public Long getId() {
return id;
} public void setId(Long id) {
this.id = id;
} @JsonView(UserSimpleView.class)
public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
} @JsonView(UserDetailView.class)
public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
}
@JsonView(UserSimpleView.class)
public Integer getEnabled() {
return enabled;
} public void setEnabled(Integer enabled) {
this.enabled = enabled;
} @Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
", enabled=" + enabled +
'}';
}
}

⒊在控制器的Action方法上使用@JsonView注解声明该Action返回的实体类视图。

 package cn.coreqi.security.controller;

 import cn.coreqi.security.entities.User;
import com.fasterxml.jackson.annotation.JsonView;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.List; @RestController
public class UserController {
@GetMapping("/users")
@JsonView(User.UserSimpleView.class)
public List<User> query(){
List<User> users = new ArrayList<>();
users.add(new User(1L,"fanqi","fanqi",1));
users.add(new User(2L,"fanqi","fanqi",1));
users.add(new User(3L,"fanqi","fanqi",1));
return users;
} @GetMapping("/user/{id:\\d+}") //使用正则指定Id为数字
@JsonView(User.UserDetailView.class)
public User getInfo(@PathVariable String id){
return new User(1L,"fanqi","fanqi",1);
}
}

使用@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. Django REST Framework extensions

    GitHub:https://github.com/chibisov/drf-extensions 官方文档:http://chibisov.github.io/drf-extensions/docs ...

  2. JavaSE_坚持读源码_Class对象_Java1.7

    Java程序在运行时,Java运行时系统一直对所有的对象进行所谓的运行时类型标识.这项信息纪录了每个对象所属的类.虚拟机通常使用运行时类型信息选准正确方法去执行,用来保存这些类型信息的类是Class类 ...

  3. Linux-CentOS 查看(监控)服务器网卡流量

    1.使用系统自带的命令: watch more /proc/net/dev 2. 使用nload命令,需要自行安装 1)# yum install flex byacc libpcap ncurses ...

  4. GridView固定行宽,自动换行,鼠标放在Table的Tr上变色

    //固定行宽table-layout:fixed;//自动换行word-break :break-all;word-wrap:break-wordGridView.Attributes.Add(&qu ...

  5. Python——安装requests第三方库

    使用pip安装 在cmd下cd到这个目录下C:\Python27\Scripts,然后执行pip install requests 在cmd 命令行执行 E:   进入e盘 cd  Python\pr ...

  6. redis安全问题【原】

    前提 假设redis安装在 IP 地址为 192.168.0.123 的linux服务器 . 我的本机Win10操作系统 IP地址为 192.168.0.45 , 有一套java客户端代码可调用lin ...

  7. HDU 1088(文本处理 **)

    题意是对一段文本进行处理,如果读到 <br>,则换行:如果读到 <hr>,若当前行无字符,则输出 80 个 ’-‘ 并换行,否则在下一行输出 80 个 ’-‘ 再换行:如果一行 ...

  8. 028、限制容器对CPU的使用(2019-01-23 周三)

    参考https://www.cnblogs.com/CloudMan6/p/7003199.html   默认情况下,所有容器都可以平等的使用host cpu资源,没有限制   docker 可以通过 ...

  9. C++ 出现异常“.... \debug_heap.cpp Line:980 Expression:__acrt_first_block==header"

    本人是在写dll项目中出现了这个问题,经过一天的研究,尝试了三个步骤1.在配置属性->常规->MFC的使用中,将在静态库中使用MFC改为在共享DLL中使用MFC.但是还会出错2.原因是dl ...

  10. shutdown函数

    #include <sys/socket.h> int shutdown(int sockfd, int howto); 返回:若成功则为0,若出错则为- (1)该函数的行为依赖于howt ...