I have written following code:
class A{
    public static class Public { }
}
// Entity class
public class B{
    @JsonView({A.Public.class}) 
    int a;
    int b;    
}
public class C{
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @JsonView({A.Public.class}) 
    public Bed getData(){
        // return object of B
    }
}
I am expecting output as
{a: vlaue}
but i am recieving
{a: value, b: value}
Please let me know what is wrong in this code.
i am using jackson version 2.4.2
The reason for this behavior is the MapperFeature DEFAULT_VIEW_INCLUSION.
From the Javadoc:
Default value is enabled, meaning that non-annotated properties are included in all views if there is no JsonView annotation
In Jersey you can disable this feature via the JacksonJaxbJsonProvider. This should work in a similar way for other JAX-RS frameworks.
@ApplicationPath("/api")
public class MyApplication extends ResourceConfig {
  public MyApplication() {
    ...
    JacksonJaxbJsonProvider provider = new JacksonJaxbJsonProvider();
    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.disable(MapperFeature.DEFAULT_VIEW_INCLUSION);    
    provider.setMapper(objectMapper);
    register(provider);
    ...
  }
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With