我写了下面的代码:
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
}
}
我期待输出为
{a: vlaue}
但是我收到了
{a: value, b: value}
请告诉我这段代码有什么问题。
我正在使用 jackson 版本 2.4.2
最佳答案
此行为的原因是 MapperFeature
DEFAULT_VIEW_INCLUSION
.
来自 Javadoc:
Default value is enabled, meaning that non-annotated properties are included in all views if there is no JsonView annotation
在 Jersey 中,您可以通过 JacksonJaxbJsonProvider
禁用此功能。对于其他 JAX-RS 框架,这应该以类似的方式工作。
@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);
...
}
}
关于java - @jsonview of jackson 不使用 jax-rs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26775106/