使Jackson子类型可扩展,而无需编辑Supertypes Java文件 [英] Make Jackson Subtypes extensible without editing the Supertypes java-file

查看:43
本文介绍了使Jackson子类型可扩展,而无需编辑Supertypes Java文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我公司中,我们有一个固定的JSON消息结构:

In my company we have a fixed JSON message structure:

{
    "headerVal1": ""
    "headerVal2": ""
    "customPayload": {
        "payloadType":""
    }
}

我想拥有一种库,该库使我不必关心公司定义的消息结构,而只是发送和接收有效载荷.

I would like to have some kind of library, which allows me, to not care for the company defined message structure, and instead just send and receive the payload.

我的想法是,将公司模板的结构定义为一个对象,并使用PayloadObject的子类型.

My idea was, to define the structure of the company template as one object, and use subtypes of a PayloadObject.

@JsonTypeInfo(
    use = JsonTypeInfo.Id.NAME,
    include = JsonTypeInfo.As.MINIMAL_CLASS,
    property = "payloadType",
    visible = false)
public abstract class PayloadObject {
}

现在,我可以创建PayloadObject的子类,并且只要属性payloadType具有字符串".SubTypeName",就可以在此结构中自动反序列化.

Now I can create subclasses of the PayloadObject, and it can be automatically deserialized in this structure, as long as the property payloadType has a string ".SubTypeName".

这是有问题的,因为我无法自定义它,甚至一开始都不会删除多余的..不幸的是,这不一定与公司现有的其他系统兼容,我们需要与之交互.

This is problematic, since I cannot customize it, not even remove the superflous . in the beginning. This is unfortunately not necessarily compatible with other, existing systems in the company, we need to interface with.

另一种方法是,添加一个@JsonSubTypes批注,在其中可以添加所有可能的子类型-在编写库时我不希望知道这些子类型.因此,此选项对我不起作用.

The alternative is, to add a @JsonSubTypes-annotation in which I can add all the possible subtypes - which I don't want to know when writing the library. So this option won't work for me.

我认为,对子类型进行@JsonType注释可能会有所帮助,但我仍然必须添加@JsonSubTypes,但这无济于事.

I thought, it might help to have the @JsonType-annoation with the subtypes, but I still have to add the @JsonSubTypes, which does not help.

有没有一种方法可以将子类型添加到基本类型而无需修改基本类型的Java文件?

Is there a way, to add subtypes to a basetype without modifying the basetypes java-file?


如果这有帮助:我们正在使用Java Spring.


If this helps: We are working with Java Spring.

推荐答案

ObjectMapper具有方法registerSubtypes(NamedType),该方法可用于添加要使用的子类型,而不必将其包含在注释中.

ObjectMapper has a method registerSubtypes(NamedType) which can be used to add subtypes for use, without having them in the annotations.

为此,我创建了一个新的注释(我可能已经重用了@JsonTypeName,但它可能是滥用的)

For this I created a new Annotation (I might have reused @JsonTypeName, but it might be abusive)

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface MyJsonSubtype
{
   public String jsonTypeName();
}

然后我给我写了一个方法

Then I wrote me a method

public static void registerMyJsonSubtypes(ObjectMapper om, Object... reflectionArgs) {
    Reflections reflections = new Reflections(reflectionArgs);
    Set<Class<?>> types = reflections.getTypesAnnotatedWith(MyJsonSubtype.class);
    for (Class type : types) {
        String name = ((MyJsonSubtype) type.getAnnotation(MyJsonSubtype.class)).jsonTypeName();
        om.registerSubtypes(new NamedType(type, name));
    }
}

它使用Reflections获取在搜索包中声明的所有带注释的类型,并将它们注册为ObjectMapper的子类型.

which uses Reflections to get all annotated types declared inside searched packages and registers them as subtypes for the ObjectMapper.

这仍然需要基类上的@JsonTypeInfo-注释将对象标记为可能可扩展,因此映射器知道要使用哪个属性来解析名称,但是我认为这是可以提供的. 我的主要注意力是这个问题,我不想在基类的注释中声明所有将来的子类型.

This still requires the @JsonTypeInfo-annotation on the base class to mark the object as potentially extensible, so the mapper knows, which property to use, to resolve the name, but I figure, this is is providable. My main attention was on the problem, that I don't want to declare all future subtypes in an annotation on the base class.

尽管我是Java初学者,所以如果不需要或可能/应该/必须改进它,请分享您的想法.

I am a Java beginner though, so please share your thoughts, if this is unnecessary or could/should/must be improved.

这篇关于使Jackson子类型可扩展,而无需编辑Supertypes Java文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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