Spring MVC @JsonView使用详解

Posted Dreamer who

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring MVC @JsonView使用详解相关的知识,希望对你有一定的参考价值。

@JsonView jackson注解官方文档:

public @interface JsonView
Annotation used for indicating view(s) that the property that is defined by method or field annotated is part of.
An example annotation would be:
  @JsonView(BasicView.class)
which would specify that property annotated would be included when processing (serializing, deserializing) View identified by BasicView.class (or its sub-class). If multiple View class identifiers are included, property will be part of all of them.
Starting with 2.9, it is also possible to use this annotation on POJO classes to indicate the default view(s) for properties of the type, unless overridden by per-property annotation.
 

@JsonView 在spring mvc序列化中,主要起到POJO的属性过滤作用。当我们的POJO在不同的接口需要返回不同的字段或者不需要返回字段(敏感字段一般都要处理的,不返回或者加密返回等等处理)。从spring4.1之后,这个注解就被支持了。

@JsonView注解可以配置我们的视图类,所以,首先要定义一些视图类,而且这些视图类可以继承。我们的POJO类属性可以就可以使用@JsonView配置我们的视图类,然后在spring 的Controller类中的接口方法也使用注解@JsonView配置我们的视图类,只要是匹配POJO属性所配置的@JsonView所配置的视图类都可以被过滤序列化。

我们先看一下例子:

package com.sdcuike.springboot.controller;

import com.fasterxml.jackson.annotation.JsonView;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author sdcuike
 * @date 2019/10/12
 */
@RestController
@RequestMapping("/json-view")
public class JsonViewTestController 

    @GetMapping("/test")
    public Response<JsonViewObjectDTO> testNoJsonView() 
        Response<JsonViewObjectDTO> response = new Response<>();
        JsonViewObjectDTO dto = new JsonViewObjectDTO();
        response.setData(dto);
        dto.setId(1L);
        dto.setUserName("sdcuike");
        return response;
    

    @GetMapping("/test2")
    public Response<JsonViewObject2DTO> testJsonView() 
        Response<JsonViewObject2DTO> response = new Response<>();
        JsonViewObject2DTO dto = new JsonViewObject2DTO();
        response.setData(dto);
        dto.setId(1L);
        dto.setUserName("sdcuike");
        return response;
    

    @GetMapping("/test3")
    @JsonView(JsonViewProfile.Test.class)
    public Response<JsonViewObject2DTO> testHaveJsonView() 
        Response<JsonViewObject2DTO> response = new Response<>();
        JsonViewObject2DTO dto = new JsonViewObject2DTO();
        response.setData(dto);
        dto.setId(1L);
        dto.setUserName("sdcuike");
        return response;
    

    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public static class JsonViewObjectDTO 
        private Long id;
        private String userName;
    

    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public static class JsonViewObject2DTO 
        private Long id;
        @JsonView(JsonViewProfile.Test.class)
        private String userName;
    

    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public static class Response<T> 
        @JsonView(JsonViewProfile.All.class)
        private int code;

        @JsonView(JsonViewProfile.All.class)
        private String msg;

        @JsonView(JsonViewProfile.All.class)
        private T data;

    

    public static class JsonViewProfile 
        public interface All 
        

        public interface Test extends All 
        

    

执行结果:

GET http://127.0.0.1:8080/json-view/test

HTTP/1.1 200 
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Date: Sat, 12 Oct 2019 17:02:32 GMT


  "code": 0,
  "msg": null,
  "data":
    "id": 1,
    "userName": "sdcuike"
 

Response code: 200; Time: 22ms; Content length: 58 bytes
 

GET http://127.0.0.1:8080/json-view/test2

HTTP/1.1 200 
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Date: Sat, 12 Oct 2019 17:02:51 GMT


  "code": 0,
  "msg": null,
  "data":
    "id": 1,
    "userName": "sdcuike"
 

Response code: 200; Time: 27ms; Content length: 58 bytes
 

GET http://127.0.0.1:8080/json-view/test3

HTTP/1.1 200 
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Date: Sat, 12 Oct 2019 17:03:06 GMT


  "code": 0,
  "msg": null,
  "data":
    "userName": "sdcuike"
 

Response code: 200; Time: 22ms; Content length: 51 bytes
 

 

如果Controller类的接口方法没有@JsonView,返回的DTO即使有注解@JsonView,也不会过滤序列化输出(反序列化请自测),相当于注解不会起作用;如果Controller类的接口方法有@JsonView,那么DTO里面的属性必须包含Controller类的接口方法@JsonView配置的view类才会输出,尤其是DTO里嵌套其他类,嵌套类属性也必须由此注解。

 

 

 

以上是关于Spring MVC @JsonView使用详解的主要内容,如果未能解决你的问题,请参考以下文章

Spring @JsonView 如何使用请求参数或标头

Jackson 的@JsonView、@JsonFilter 和 Spring

Spring MVC返回的json如何去除根节点名称

spring boot 之@JsonView 简单介绍

@JsonView 不过滤属性(Spring 4.1.0.RC2,Jackson 2.3.2)

spring mvc 必须使用注解配置吗?可不可使用xml配置?求高手详解