@JsonView 在简单的测试中不起作用

Dan*_*ani 2 java jackson

我不能用@JsonView 执行这个简单的例子。我究竟做错了什么?

${jackson-2-version} = 2.6.5

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>${jackson-2-version}</version>
</dependency>

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-annotations</artifactId>
    <version>${jackson-2-version}</version>
</dependency>

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>${jackson-2-version}</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)

完整的测试类。

package staticTest;

import com.fasterxml.jackson.annotation.JsonView;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import org.junit.Test;

/**
 * Created by Daniel on 01/04/2016.
 */
public class Jackson2Tests {

    @Test
    public void JsonViewTest(){

        try {

            System.out.println(getMapper().writeValueAsString(new DemoClass()));
        } catch (JsonProcessingException e) {

            e.printStackTrace();
        }
    }

    private ObjectMapper getMapper(){

        ObjectMapper objectMapper = new ObjectMapper();

        objectMapper.writerWithView(ToShowIn.App.class);
        objectMapper.readerWithView(ToShowIn.App.class);

        return objectMapper;
    }

    public class ToShowIn {

        public class App{}
        public class Manager{}

    }

    class DemoClass{

        @JsonView(ToShowIn.App.class)
        private String propertyOne = "one";
        private int propertyTwo = 2;
        private boolean propertyThree = true;
        private DemoChild propertyFour = new DemoChild();

        public String getPropertyOne() {
            return propertyOne;
        }

        public void setPropertyOne(String propertyOne) {
            this.propertyOne = propertyOne;
        }

        public int getPropertyTwo() {
            return propertyTwo;
        }

        public void setPropertyTwo(int propertyTwo) {
            this.propertyTwo = propertyTwo;
        }

        public boolean isPropertyThree() {
            return propertyThree;
        }

        public void setPropertyThree(boolean propertyThree) {
            this.propertyThree = propertyThree;
        }

        public DemoChild getPropertyFour() {
            return propertyFour;
        }

        public void setPropertyFour(DemoChild propertyFour) {
            this.propertyFour = propertyFour;
        }

        class DemoChild{

            private String childOne = "1";
            private int childTwo = 2;
            private boolean childThree = true;

            public String getChildOne() {
                return childOne;
            }

            public void setChildOne(String childOne) {
                this.childOne = childOne;
            }

            public int getChildTwo() {
                return childTwo;
            }

            public void setChildTwo(int childTwo) {
                this.childTwo = childTwo;
            }

            public boolean isChildThree() {
                return childThree;
            }

            public void setChildThree(boolean childThree) {
                this.childThree = childThree;
            }
        }

    }

}
Run Code Online (Sandbox Code Playgroud)

导致:

{"propertyOne":"one","propertyTwo":2,"propertyThree":true,"propertyFour":{"childOne":"1","childTwo":2,"childThree":true}}

解决方案

首先,正如@varren 在下面所说的,.writerWithView()它不是一个二传手。

要使 ObjectMapper 中的一个视图持久化,请使用.setConfig().

objectMapper.setConfig(objectMapper.getSerializationConfig().withView(ToShowIn.App.class));

最后,也是最重要的一点,objectMapper.disable(MapperFeature.DEFAULT_VIEW_INCLUSION);让 JsonView 像 @varren 贡献的 jackson 文档所说的那样工作。

var*_*ren 5

writerWithView并且readerWithView不是设置器,它们是ObjectWriterObjectReader构建器方法,因此您必须使用:

getMapper().writerWithView(ToShowIn.App.class).writeValueAsString(new DemoClass()))
Run Code Online (Sandbox Code Playgroud)

而且您还必须禁用MapperFeature.DEFAULT_VIEW_INCLUSIONobjectMapper。

private ObjectMapper getMapper(){
    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.disable(MapperFeature.DEFAULT_VIEW_INCLUSION);
    return objectMapper;
}
Run Code Online (Sandbox Code Playgroud)

以下是文档中的信息:http : //wiki.fasterxml.com/JacksonJsonViews

处理“无视图”属性

默认情况下,所有没有显式视图定义的属性都包含在序列化中。但是从 Jackson 1.5 开始,您可以通过以下方式更改此默认值:

objectMapper.configure(SerializationConfig.Feature.DEFAULT_VIEW_INCLUSION, false); 其中 false 表示在启用视图时不包含此类属性。此属性的默认值为“true”。