简体   繁体   English

当我只得到一个字符串时,如何从带有泛型的Java类生成JSON模式?

[英]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创建一个架构,并在MyClassCustomListing使用的所有位置插入$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.

相关问题 我如何从mongodb中生成json模式在Java中的结果? - How i generate json schema from mongodb result in java? 如何为Java类字段生成准确的泛型表达式? - How do I generate an accurate generics expression for a Java class field? 如何将用户从GUI提供的输入存储到Java中的字符串数组? 我正在使用Java swing创建GUI - How can I store inputs given by the user from GUI to a string array in java? I am using java swing to create the GUI 如何生成具有所需字段或默认值的 JSON 模式? - How can i generate JSON schema with a required filed or default values? 如何从以字符串形式给出的 java 代码解析包? - How can I parse the package from java code given as a String? 如何使用泛型实例化Java中的类实例? - How can I instantiate a class instance in java with generics? Java:如何设计此类以使其具有可选的泛型ParameterType? - Java: How can I design this class for having optional generics ParameterType? 如何在Java中使用“默认”类属性解码json字符串? - How can I decode a json string with 'default' class properties in java? BCrypt:如何从我正在散列的字符串中生成盐? - BCrypt: how can I generate a salt from the string I am hashing? 当我侦听RethinkDB的更新时,能否以JSON字符串的形式获取数据? - Can I get data as JSON string when I am listening updates from RethinkDB?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM