java - JSON 字段显示不应基于 View

标签 java json jackson

我的 JSON 中显示了一些字段,但根据我尝试使用的 View ,这些字段不应显示。我相信我正在正确编码使用 hte View ...

ObjectMapperProvider

@Component
@Provider
public class ObjectMapperProvider implements ContextResolver<ObjectMapper> {

    @Override
    public ObjectMapper getContext(Class<?> aClass) {
        ObjectMapper objectMapper = new ObjectMapper();

        // http://wiki.fasterxml.com/JacksonJsonViews#Handling_of_.22view-less.22_properties
        objectMapper.configure(SerializationConfig.Feature.DEFAULT_VIEW_INCLUSION, Boolean.FALSE);

        return objectMapper;
    }
}

生成 JSON 的代码:

ObjectMapper mapper = new ObjectMapper();
mapper.getSerializationConfig().withView(Views.ShutdownView.class);

//persist the queue to the database
if (getQueue().size() > 0) {
    try {
        getEntity().setQueue(mapper.writeValueAsBytes(getQueue()));
    } catch(IOException e) {
        logger.severe("Unable to JSONify: "+getEntity());
        e.printStackTrace();
    }
}

我看到的部分 JSON 包括:

                 "uuid":"c0fbb88f-893e-4375-91a0-1ebb315e7ad7",
                 "inProduction":false,
                 "updated":null,
                 "created":1372346197000,

当唯一应该显示的字段是uuid时:

@javax.persistence.Column(name = "uuid", nullable = false, insertable = true, updatable = true, length = 128, precision = 0)
@Basic
@JsonView({Views.ShutdownView.class})
public String getUuid() {
    return uuid;
}

public void setUuid(String uuid) {
    this.uuid = uuid;
}

private Boolean inProduction;

@javax.persistence.Column(name = "in_production", nullable = false, insertable = true, updatable = true, length = 0, precision = 0)
@Basic
public Boolean getInProduction() {
    return inProduction;
}

public void setInProduction(Boolean inProduction) {
    this.inProduction = inProduction;
}

private Timestamp updated;

@javax.persistence.Column(name = "updated", nullable = true, insertable = true, updatable = true, length = 19, precision = 0)
@Basic
public Timestamp getUpdated() {
    return updated;
}

public void setUpdated(Timestamp updated) {
    this.updated = updated;
}

private Timestamp created;

@javax.persistence.Column(name = "created", nullable = false, insertable = true, updatable = true, length = 19, precision = 0)
@Basic
public Timestamp getCreated() {
    return created;
}

public void setCreated(Timestamp created) {
    this.created = created;
}

最佳答案

在此之前,需要解决一些问题

  • 您发布了 ObjectMapperProvider 的代码,但在代码示例中您执行了 new ObjectMapper(),因此它不会包含您的 DEFAULT_VIEW_INCLUSION 设置

  • withView 方法返回一个新实例,它们不会修改所调用的实例。另外,在这种情况下您可能不想修改序列化配置。正确的代码是:mapper.writerWithView(Views.ShutdownView.class).writeValueAsBytes(getQueue())

关于java - JSON 字段显示不应基于 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17435087/

相关文章:

java - scala -> java getField().get()

jquery - json 到嵌套无序列表

java - 协调 jackson 操作的数据类型

spring - 使用 JacksonJsonProvider.setMapper 时无法访问 javax.ws.rs.ext.MessageBodyReader

java - 如何在 Java 中快速检索目录列表?

java - 将 ArrayList 转换为二维数组

java - 锁定写入 HashMap

json - 为 JSON 集类型定义 `Reads`

ruby-on-rails - 使用 map 方法格式化 JSON 输出 - Rails 3

java - Spring Boot 和 Jackson : Define which fields to serialize on external class