serialization - 我可以在 RestEasy 中指定用于方法结果转换的 jackson @JsonView 吗?

标签 serialization jackson resteasy wildfly

我正在使用基于 @JsonView 的序列化模型.我通常用 ContextResolver 配置 jackson像这样:

@Override
public ObjectMapper getContext(Class<?> aClass) {
    // enable a view by default, else Views are not processed
    Class view = Object.class;
    if (aClass.getPackage().getName().startsWith("my.company.entity")) {
        view = getViewNameForClass(aClass);
    }
    objectMapper.setSerializationConfig(
         objectMapper.getSerializationConfig().withView(view));
    return objectMapper;
}

如果我序列化单个实体,这可以正常工作。但是,对于某些用例,我想使用与单个实体相同的 View 序列化我的实体列表。在这种情况下,aClassArrayList ,所以通常的逻辑没有多大帮助。

所以我正在寻找一种方法来告诉 jackson 使用哪个 View 。理想情况下,我会写:
@GET @Produces("application/json; charset=UTF-8")
@JsonView(JSONEntity.class)
public List<T> getAll(@Context UriInfo uriInfo) {
    return getAll(uriInfo.getQueryParameters());
}

并在 View 下序列化 JSONEntity .这可以用 RestEasy 实现吗?如果没有,我怎么能效仿呢?

编辑:我知道我可以自己进行序列化:
public String getAll(@Context UriInfo info, @Context Providers factory) {
    List<T> entities = getAll(info.getQueryParameters());
    ObjectMapper mapper = factory.getContextResolver(
         ObjectMapper.class, MediaType.APPLICATION
    ).getContext(entityClass);
    return mapper.writeValueAsString(entities);
}

然而,这充其量是笨拙的,并且破坏了让框架处理这个样板的整个想法。

最佳答案

事实证明,可以简单地用 @JsonView 注释一个特定的端点。 (就像在我的问题中一样) jackson 将使用此 View 。谁能想到。

您甚至可以以通用方式( more context in my other question )执行此操作,但这将我与 RestEasy 联系起来:

@Override
public void writeTo(Object value, Class<?> type, Type genericType, 
        Annotation[] annotations,  MediaType mediaType, 
        MultivaluedMap<String, Object> httpHd, 
        OutputStream out) throws IOException {
    Class view = getViewFromType(type, genericType);
    ObjectMapper mapper = locateMapper(type, mediaType);

    Annotation[] myAnn = Arrays.copyOf(annotations, annotations.length + 1);
    myAnn[annotations.length] = new JsonViewQualifier(view);

    super.writeTo(value, type, genericType, myAnn, mediaType, httpHd, out);
}

private Class getViewFromType(Class<?> type, Type genericType) {
    // unwrap collections
    Class target = org.jboss.resteasy.util.Types.getCollectionBaseType(
            type, genericType);
    target = target != null ? target : type;
    try {
        // use my mix-in as view class
        return Class.forName("example.jackson.JSON" + target.getSimpleName());
    } catch (ClassNotFoundException e) {
        LOGGER.info("No view found for {}", target.getSimpleName());
    }
    return Object.class;
}

关于serialization - 我可以在 RestEasy 中指定用于方法结果转换的 jackson @JsonView 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20634275/

相关文章:

java - Jackson 未知字段的 json 数组列表

java - 为什么在没有默认构造函数的情况下将 JSON 编码的字符串转换为 Java 类实例会失败?

java.lang.RuntimeException : Unable to instantiate @Form class. 没有无参数构造函数

scala - 从 Scala 中的字符串读取案例类对象(类似于 Haskell 的 "read"类型类)

.net - DataContractJsonSerializer 何时包含类型信息?

json - 使用 json4s 序列化 AnyVal 的序列

java - 如何在 JAX-RS 应用程序中从基于 web.xml 的授权切换到通过注释进行授权

ios - Swift中类的Xml序列化/反序列化

Java jackson : Deserialize JSON with dynamic Names into JavaClass

java - 为什么 JacksonJsonProvider 会忽略 MediaType 的参数?