[英]How can I generate a JSON Schema from a Java class with generics when I am only given a string?
I am working on a project where I need to generate JSON schemas for all of the objects that are returned from my API. 我正在一个项目中,我需要为从我的API返回的所有对象生成JSON模式。
I am given a String such as: 我得到一个字符串,例如:
"com.example.CustomListing<MyClass>"
My goal is to create a schema for CustomListing
and insert a $ref
to MyClass
in all the places it is used in CustomListing
. 我的目标是为
CustomListing
创建一个架构,并在MyClass
在CustomListing
使用的所有位置插入$ref
。
Jackson offers a way to create schemas from generics here but I need to have Class object for that generic type. Jackson 在这里提供了一种从泛型创建模式的方法,但是我需要为该泛型类型提供Class对象。
I can create a generic Class object using a method here but it requires me to know the type ahead of time, which I do not. 我可以在此处使用方法创建通用的Class对象,但它需要我提前知道类型,而我不是。
How can I get a Class<CustomListing<MyClass>>
when I am only given the String "com.example.CustomListing<MyClass>"
? 仅给我字符串
"com.example.CustomListing<MyClass>"
时,如何获得Class<CustomListing<MyClass>>
?
EDIT: I am creating these schemas in a JavaDoc doclet, so I am working at compile time, hence the limitations. 编辑:我正在JavaDoc doclet中创建这些架构,所以我在编译时工作,因此存在局限性。
I think I have found a direction to go for the solution. 我想我已经找到了解决方案的方向。 I am using
ObjectMapper
from Jackson which allows me to configure modules to process any given type. 我正在使用Jackson的
ObjectMapper
,它允许我配置模块以处理任何给定的类型。 I believe I can register some to deal with a generic (such as <T>
) and it would know that in this case <T>
would be referencing MyClass
instead and insert the $ref
. 我相信我可以注册一些来处理通用名称(例如
<T>
),并且它会知道在这种情况下<T>
会引用MyClass
并插入$ref
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.