@jsonview的杰克逊没有与jax-rs合作 [英] @jsonview of jackson not working with jax-rs

查看:191
本文介绍了@jsonview的杰克逊没有与jax-rs合作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了以下代码:

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}

请告诉我这段代码有什么问题。

Please let me know what is wrong in this code.

我使用的是杰克逊版本2.4.2

i am using jackson version 2.4.2

推荐答案

这个的原因行为是 MapperFeature DEFAULT_VIEW_INCLUSION

The reason for this behavior is the MapperFeature DEFAULT_VIEW_INCLUSION.

来自Javadoc:

From the Javadoc:


默认值已启用,表示未注释的属性如果没有JsonView注释,则包含在所有视图中

Default value is enabled, meaning that non-annotated properties are included in all views if there is no JsonView annotation

在Jersey中,您可以通过 JacksonJaxbJsonProvider禁用此功能。这应该与其他JAX-RS框架类似。

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

    ...
  }
}

这篇关于@jsonview的杰克逊没有与jax-rs合作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆