Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@JsonView doesn't work in a simple test

Tags:

java

jackson

I can't perform this simple example with @JsonView. What am I doing wrong?

${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>

The full test class.

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;
            }
        }

    }

}

Result in:

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

SOLUTION

First, as @varren says below, .writerWithView() is not a setter.

To make one view persistent in the ObjectMapper work with .setConfig().

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

Finally, and the most important, objectMapper.disable(MapperFeature.DEFAULT_VIEW_INCLUSION);makes JsonView works just like jackson documentation contributed by @varren says.

like image 852
Dani Avatar asked Dec 06 '25 14:12

Dani


1 Answers

writerWithView and readerWithView are not setters, they are ObjectWriter and ObjectReader builder methods, so you have to use:

getMapper().writerWithView(ToShowIn.App.class).writeValueAsString(new DemoClass()))

And you also have to disable MapperFeature.DEFAULT_VIEW_INCLUSION for your objectMapper.

private ObjectMapper getMapper(){
    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.disable(MapperFeature.DEFAULT_VIEW_INCLUSION);
    return objectMapper;
}

Here is info from documentation: http://wiki.fasterxml.com/JacksonJsonViews

Handling of "view-less" properties

By default all properties without explicit view definition are included in serialization. But starting with Jackson 1.5 you can change this default by:

objectMapper.configure(SerializationConfig.Feature.DEFAULT_VIEW_INCLUSION, false); where false means that such properties are NOT included when enabling with a view. Default for this property is 'true'.

like image 185
varren Avatar answered Dec 08 '25 04:12

varren



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!