简体   繁体   English

将 Java 数据 Class 转换为前端的字段数组/元数据(类似于 Swagger 数据模型/模式)?

[英]Convert Java Data Class to Array of Fields/Metadata for frontend (Similar to Swagger Data Model/Schema)?

I currently have a bunch of Java Data Defintions that I have to duplicate the definition as a JSON array for use in a front-end table display.我目前有一堆 Java 数据定义,我必须将定义复制为 JSON 数组以用于前端表格显示。

Is there a standard way to generate the field, clazz, and metadata definitions from the Java Class during build (or dynamically on request)?是否有一种标准方法可以在构建期间(或根据要求动态生成)从 Java Class 生成字段、clazz 和元数据定义? Ideally with the option to define some other metadata via annotations?理想情况下可以选择通过注释定义一些其他元数据吗?

I am thinking it could be done via getFields and Annotations, but I was hoping there was some resource available that could help me learn what is the common way to do something like this (or if this should even be done)?我认为这可以通过 getFields 和注释来完成,但我希望有一些可用的资源可以帮助我了解做这样的事情的常用方法是什么(或者是否应该这样做)?

eg) (With example desired Annotations)例如)(以示例所需的注释)

@GenerateClassMeta
class MyObject {
    @Title("Id")
    int id;
    
    @Title("My String")
    String myStr;
   
    @Title("My List")
    List<String> list;
     
    @EmbeddedMeta(title="My Child",field="child")
    Child child;
   
}
class Child{
    @Title("Enabled")
    boolean enabled;
    
    @IgnoreMeta
    String myOtherString;        
}

And the generated JSON array would be something like生成的 JSON 数组类似于

   [
       {
           "title":"Id",
           "field":"id",
           "clazz":"java.lang.Integer",
           "searchPath":"id"
       },
       {
           "title":"My String",
           "field":"myString",
           "clazz":"java.lang.String",
           "searchPath":"myStr"
       },
       {
           "title":"My List",
           "field":"list",
           "clazz":"java.util.list",
           "searchPath":"list"
        },
        {
           "title":"My Child: Enabled",
           "field":"enabled",
           "clazz":"java.lang.Boolean",
           "searchPath":"child.enabled"
        }
   ]

Currently i have to manually write the JSON definition for displaying in the front-end, but was hoping to change it so the front-end can fetch the generated metadata and format it as needed for various displays.目前我必须手动编写 JSON 定义以在前端显示,但希望对其进行更改,以便前端可以获取生成的元数据并根据各种显示的需要对其进行格式化。

If it makes a difference it is for a Spring-Boot server/React frontend.如果它有所作为,它是针对 Spring-Boot 服务器/React 前端的。 The metadata is used to define (among other things) the columns and entity search paths for a datatable.元数据用于定义(除其他事项外)数据表的列和实体搜索路径。

Any help would be appreciated.任何帮助,将不胜感激。

Have a look at JSON Schema.看看 JSON 架构。 There are a number of implementations that convert code to JSON Schema: https://json-schema.org/implementations.html有许多将代码转换为 JSON Schema 的实现: https://json-schema.org/implementations.html

Of course the JSON structure is different from your example, but JSON Schema is widely used (for instance in OpenAPI).当然 JSON 结构与您的示例不同,但 JSON Schema 被广泛使用(例如在 OpenAPI 中)。

Side note: not sure what your frontend intends to do with the data, but in case you're planning to display a dynamic form using this data, there also are a number of implementations that do just that: https://json-schema.org/implementations.html#web-ui-generation旁注:不确定您的前端打算如何处理数据,但如果您计划使用此数据显示动态表单,也有许多实现可以做到这一点: https://json-schema .org/implementations.html#web-ui-generation

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM