1

我正在尝试在Quarkus项目中向Jackson添加一个 mixin。ObjectMapper我有一些看起来像这样的代码:

@Provider
public class ObjectMapperContextResolver implements ContextResolver<ObjectMapper> {

    private final ObjectMapper mapper;

    public ObjectMapperContextResolver() {
        this.mapper = createObjectMapper();
    }

    @Override
    public ObjectMapper getContext(Class<?> type) {
        return mapper;
    }

    private ObjectMapper createObjectMapper() {
        ObjectMapper mapper = new ObjectMapper();
        mapper.addMixIn(MyModel.class, MyMixin.class);
        return mapper;
    }
}

这段代码在我的Thorntail项目中运行良好。出于某种原因,Quarkus 没有处理这个问题,并且对象映射器不受影响。我与 Quarkus CDI 有什么不同吗?

更新

显然我对实现有点困惑。我应该使用Json-B api。我想出了如何更改 Json-B 的配置并将其发布在下面。

4

3 回答 3

4

您可以提供一个而不是提供一个ObjectMapperJsonbConfig以便您可以自定义序列化/反序列化。

这是我最终使用的:

@Provider
public class JsonConfig implements ContextResolver<Jsonb> {

    @Override
    public Jsonb getContext(Class type) {
        JsonbConfig config = new JsonbConfig();
        config.withPropertyVisibilityStrategy(new IgnoreMethods());
        return JsonbBuilder.create(config);
    }
   }

  class IgnoreMethods implements PropertyVisibilityStrategy {

    @Override
    public boolean isVisible(Field field) {
        return true;
    }

    @Override
    public boolean isVisible(Method method) {
        return false;
    }
}

这允许您自定义您的JsonbConfig. 在这里,我的特别禁止访问序列化/反序列化方法。在带有 Panache 的 Quarkus 上,这可以防止isPersistent出现在您的 JSON 输出中。

于 2019-03-22T06:10:02.793 回答
2

除了@jsolum 的正确答案之外,这里还有一个工作提供者,它使用更快的xml-annotations 来检查字段和方法的可见性:

@Provider
public class JsonConfig implements ContextResolver<Jsonb> {

    @Override
    public Jsonb getContext(Class aClass) {
        JsonbConfig config = new JsonbConfig();
        config.withPropertyVisibilityStrategy(new PropertyVisibilityStrategy() {
            @Override
            public boolean isVisible(Field field) {
                JsonIgnore annotation = field.getAnnotation(JsonIgnore.class);
                return annotation == null || !annotation.value();
            }

            @Override
            public boolean isVisible(Method method) {
                JsonIgnore annotation = method.getAnnotation(JsonIgnore.class);
                return annotation == null || !annotation.value();
            }
        });
        return JsonbBuilder.create(config);
    }
}
于 2019-04-04T23:43:54.140 回答
0

JsonbConfig在 Quarkus 中可以自定义提供一个ApplicationScoped实例JsonbConfigCustomizer(考虑到@jsolum 的答案):

@ApplicationScoped
public class JsonbFormattingConfig implements JsonbConfigCustomizer {

    @Override
    public void customize(JsonbConfig jsonbConfig) {
        jsonbConfig.withPropertyVisibilityStrategy(new IgnoreMethods());
    }
}

class IgnoreMethods implements PropertyVisibilityStrategy {
    @Override
    public boolean isVisible(Field field) {
        return true;
    }

    @Override
    public boolean isVisible(Method method) {
        return false;
    }
}

来源:https ://quarkus.io/guides/rest-json#json-b

于 2021-05-04T21:07:27.303 回答