Code generation management method and system of RESTful API client and storage medium

By determining the language code version of the RESTful API client and generating codes in multiple programming languages, the problems of low code generation efficiency and poor versatility in the existing technology are solved, efficient management and reuse of multi-language codes are achieved, and development efficiency and code quality are improved.

CN120631366APending Publication Date: 2025-09-12CHINA RAILWAY SIYUAN SURVEY & DESIGN GRP CO LTD +1
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202510631772.3
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-05-16
Publication Date
2025-09-12

AI Technical Summary

Technical Problem

The existing technology has low efficiency in generating RESTful API client code, cannot be reused, can only generate a single programming language, has poor versatility, and lacks flexibility and code management support.

Method used

By determining whether the language code of the business service is the latest version, directly download or introduce code dependencies; generate target language code for multiple programming languages ​​and automatically push it to the language pack management repository, supporting multi-language and multi-platform code generation.

Benefits of technology

It improves code generation efficiency, reduces developer workload, supports multi-language code generation, and improves code maintainability and development efficiency.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120631366A_ABST
    Figure CN120631366A_ABST
Patent Text Reader

Abstract

The invention provides a code generation management method and system of a RESTful API client and a storage medium. The method comprises the following steps: acquiring or creating a business service, and judging whether a language code of the business service is a latest version or not; if yes, the user directly downloads the language code into the corresponding item or introduces the corresponding code dependency; otherwise, the target language code of the RESTful API client is regenerated, and the method comprises the steps that an Open API protocol is obtained according to the content of the business service, and the Open API protocol is analyzed and converted into an object entity; the object entity is input into the established code template, and meanwhile target language codes of multiple programming languages are generated; a plurality of target language codes are automatically pushed to corresponding language package management warehouses, and the user downloads the target language codes into corresponding items from the language package management warehouses or introduces corresponding code dependence. The technical problems that at present, code generation efficiency is low, multiple programming languages cannot be generated at the same time, and universality is poor are solved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the technical field of code generation and management, and in particular to a code generation and management method, system, and storage medium for a RESTful API client. Background Art

[0002] RESTful API (Representational State Transfer Application Programming Interface) is a software architecture style based on the HTTP protocol. The core idea is to abstract network resources into a series of URLs (uniform resource locators). Clients access and operate these URLs through the HTTP protocol. It makes full use of the rich semantics and functions of HTTP to design flexible and scalable web services to implement operations such as resource acquisition, creation, update and deletion.

[0003] At present, business codes are usually generated by using the business service model corresponding to the target business service. Although this method can quickly generate API services and improve API development and management efficiency, the code generation process of this code generation tool is one-time. When different callers use the same code, the process needs to be repeated, and the previous process cannot be reused, resulting in low code generation efficiency. At the same time, this code generation tool does not provide perfect support for code management and is inconvenient to use. Developers need to manually copy the code to the project for use, which not only increases the workload of developers and code reviewers, but also the generated code is not flexible enough. It can only generate one language code at a time and cannot generate multiple programming language codes at the same time. It has poor versatility and lacks service and version management. Summary of the Invention

[0004] Based on this, it is necessary to provide a code generation management method, system and storage medium for a RESTful API client to address the above technical problems, which solve the current technical problems of low code generation efficiency, inability to generate multiple programming languages ​​at the same time, and poor versatility.

[0005] In a first aspect, the present invention provides a code generation management method for a RESTful API client, the method comprising:

[0006] Obtain or create a business service and determine whether the language code of the business service is the latest version; if so, the user directly downloads the language code to the corresponding project or introduces the corresponding code dependency; otherwise, regenerate the target language code of the RESTful API client;

[0007] The target language code for generating the RESTful API client includes:

[0008] Obtain the OpenAPI protocol based on the content of the business service, parse the OpenAPI protocol and convert it into an object entity; input the object entity into the established code template, and generate the target language code in multiple programming languages ​​at the same time; and automatically push the multiple target language codes to the corresponding language package management repository, and the user then downloads the target language code from the language package management repository to the corresponding project or introduces the corresponding code dependency.

[0009] Furthermore, the content of the business service includes: service identification, name, description, interface document URL, and interface path prefix.

[0010] Furthermore, the method for determining whether the language code of the business service is the latest version includes:

[0011] The content of the business service and the corresponding business input information in the database are obtained to determine whether the object entities of the two are consistent. If so, it means that the language code is the latest version; otherwise, it means that the language code is not the latest version.

[0012] Furthermore, the object entity includes: request type, request path, request parameters, request header, request body, and response body.

[0013] Furthermore, the generation of the target language code includes:

[0014] Different code sub-templates are constructed using different programming languages, the object entities are simultaneously input into different code sub-templates, rendering is completed through the Velocity template engine, and a plurality of target language codes corresponding to different programming languages ​​are generated.

[0015] Furthermore, the code sub-model is established in the following way: based on the coding habits of different languages, the class template of the request parameters and response parameters of the target language is defined through the Velocity template syntax, the request service and interface method are defined, and the code sub-model is generated in the form of folder organization, program package, and class method variable naming.

[0016] Furthermore, the target language code is maintained through service granularity design.

[0017] Furthermore, the automatic push includes: uploading the generated multiple target language codes to multiple language package management repositories.

[0018] In a second aspect, the present invention provides a code generation management system for a RESTful API client, the system comprising:

[0019] The service management module is used to obtain or create business services and transmit the content of business services to the OpenAPI protocol acquisition and parsing module;

[0020] A version management module is used to receive the content of the business service of the OpenAPI protocol acquisition and parsing module, determine whether the language code of the business service is the latest version, and feed back the result to the OpenAPI protocol acquisition and parsing module;

[0021] An OpenAPI protocol acquisition and parsing module is used to receive the content of the business service, obtain the OpenAPI protocol based on the content of the business service, parse the OpenAPI protocol and convert it into an object entity, and send the object entity to the multilingual code generation module;

[0022] A multi-language code generation module is used to establish a code template, input the received object entity into the code template, and simultaneously generate the target language code in multiple programming languages ​​and send it to the code push module;

[0023] The code push module is used to automatically push the target language codes to the corresponding language package management warehouse.

[0024] In a third aspect, the present invention provides a computer-readable storage medium having a computer program / instruction stored thereon, which implements the steps of any of the above methods when executed by a processor.

[0025] In general, the present invention provides a code generation management method, system, and storage medium for a RESTful API client, which can achieve the following beneficial effects compared to the prior art:

[0026] On the first aspect, the present invention measures whether it is necessary to regenerate the target language code of the RESTful API client by judging whether the language code of the business service is the latest version; when the language code is the latest version, the language code can be directly downloaded to the corresponding project or the corresponding code dependency can be introduced, so that when different callers use the same code, there is no need to regenerate the language code, but the previous language code can be directly reused, which not only greatly improves the code generation efficiency; but also developers can directly download the language code to the corresponding project or introduce the corresponding code dependency, avoiding the developer manually copying the code into the project, improving the work efficiency of the development team and the maintainability of the code, while reducing the workload.

[0027] Secondly, the present invention inputs the object entity into the established code template and generates codes in multiple programming languages ​​at the same time. It not only supports the code generation needs of multiple languages ​​and multiple platforms, but can also be expanded to more programming languages ​​according to needs. It has strong versatility and can significantly improve the efficiency and quality of RESTfulAPI client code generation and reduce development costs.

[0028] Thirdly, the present invention generates codes in multiple programming languages ​​simultaneously, and automatically pushes different target language codes to different language package management repositories. Once generated, multiple callers can use them simultaneously, significantly improving the efficiency of code development and use. BRIEF DESCRIPTION OF THE DRAWINGS

[0029] In order to more clearly illustrate the technical solutions in the present invention or the prior art, the following briefly introduces the drawings required for use in the embodiments or the description of the prior art. Obviously, the drawings described below are some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying any creative work.

[0030] Figure 1 This is a method flow diagram of a code generation management method, system, and storage medium for a RESTful API client provided by the present invention;

[0031] Figure 2 This is a schematic diagram of the content of a code generation management method, system, and storage medium for a RESTful API client provided by the present invention for creating a business service;

[0032] Figure 3 This is a system structure diagram of a code generation management method, system and storage medium for a RESTful API client provided by the present invention. DETAILED DESCRIPTION

[0033] To make the objectives, technical solutions, and advantages of the present invention more clear, the technical solutions of the present invention will be clearly and completely described below in conjunction with the accompanying drawings and embodiments of the present invention. Obviously, the embodiments described are only some embodiments of the present invention, not all embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without making creative efforts shall fall within the scope of protection of the present invention.

[0034] It should be noted that, in the description of the embodiments of the present invention, the terms "comprises," "includes," or any other variations thereof are intended to encompass non-exclusive inclusion, such that a method, step, or system comprising a series of elements includes not only those elements, but also other elements not explicitly listed, or elements inherent to such method, step, or system. In the absence of further limitations, an element defined by the phrase "comprising a ..." does not exclude the presence of other identical elements in the method, step, or system comprising the element.

[0035] The present invention relates to service information management under the microservice architecture. In order to solve the technical problems of low code generation efficiency, inability to generate multiple programming languages ​​at the same time, and poor versatility, the present invention proposes a code generation management method for RESTful API client, such as Figure 1 As shown, the specific method is:

[0036] Obtain or create a business service and determine whether the language code of the business service is the latest version. If so, the user directly downloads the language code to the corresponding project or introduces the corresponding code dependency. Otherwise, regenerate the target language code of the RESTful API client.

[0037] It should be noted that if the business service cannot be obtained, it means that the business service does not exist and you need to create the business service.

[0038] Obtaining a business service means determining whether the business service exists. If so, directly obtain the business service and determine whether the language code of the business service is the latest version. If not, create the business service, edit the content of the business service, and then determine whether the target language code of the business service is the latest version.

[0039] The content of business services generally includes: service identifier, name, description, interface document URL, and interface path prefix. Figure 2 The following shows the content to be entered when creating a business service.

[0040] The interface document URL is used to obtain the OpenAPI interface information of the business service. The interface path prefix is ​​used to delete the unified path prefix of the request path. In the case of a gateway, the service can adjust the path prefix as needed. The path of the generated language code will delete the interface path prefix, so that the generated language code is not affected by the path prefix.

[0041] As an embodiment, a method for determining whether the language code of a business service is the latest version includes: obtaining the content of the business service and the corresponding business input information in the database, and determining whether the object entities of the two are consistent. If so, it indicates that the language code is the latest version; otherwise, it indicates that the language code is not the latest version.

[0042] Generating the target language code for the RESTful API client includes: obtaining the OpenAPI protocol based on the content of the business service, parsing the OpenAPI protocol and converting it into object entities; inputting the object entities into the established code template and generating target language codes in multiple programming languages ​​at the same time; and automatically pushing the multiple target language codes to the corresponding language package management repository. The user then downloads the target language code from the language package management repository to the corresponding project or introduces the corresponding code dependency.

[0043] Furthermore, obtaining the OpenAPI protocol based on the content of the business service can be to request the interface document URL address of the business service, obtain the latest OpenAPI interface information of the OpenAPI protocol, compare the latest OpenAPI interface information with the corresponding business input information in the database, if the object entities are different, it means that the language codes are inconsistent, then parse the OpenAPI protocol and convert it into the object entity processed when the language code is generated.

[0044] It should be noted that the object entity includes: request type (Get, Post, Put, Delete, Head, Option), request path, request parameters, request header, request body, and response body.

[0045] Code templates include multiple independent sub-templates based on different coding languages. Each sub-template can directly generate language code for the corresponding coding language from object entities. In other words, one sub-template corresponds to each coding language. Consequently, a single coding language can be converted into language code for multiple encoding formats simultaneously, supporting multi-language and multi-platform code generation. Furthermore, a single generated code can be used simultaneously by multiple callers based on their needs, significantly improving code development and usage efficiency.

[0046] It should be noted that the establishment of code templates needs to fully consider the coding habits of different languages. Code templates can be established based on constraints such as folder organization, program packages, naming methods of class methods and variables, commonly used RESTful client call libraries, selection of JSON serialization and deserialization libraries, timeout retry strategies, and exception handling.

[0047] As an embodiment, the code sub-model is established as follows: based on the coding habits of different languages, the class template of the request parameters and response parameters of the target language is defined through the Velocity template syntax, the request service and interface method are defined, and the code sub-model is generated in the form of folder organization, program packages, and class method variable naming.

[0048] Specifically, the class templates of the target language's request and response parameters are first defined using the Velocity template syntax. Taking Java as an example, the class template includes fields supporting strings (String), integers (Integer, Long), floating-point types (Float, Double), Boolean types (Bool), collection types (List, Map, Set), and class nesting (Class).

[0049] Define request services and interface methods, which can specifically include interface comments and method bodies. The method body can be divided into multiple parts, such as splicing URL addresses, processing request parameters and request bodies, Http method calls, deserialization, logging, and exception handling.

[0050] Then generate code sub-templates including file directory structure, type, method and variable name case.

[0051] It should be noted that the construction of sub-templates for other coding languages ​​also follows similar steps. By following the coding habits of different languages, a code sub-model is generated based on the organization of folders, program packages, and the naming of class methods and variables.

[0052] For example, a class template could be:

[0053]

[0054]

[0055]

[0056] The specific construction code of the method body is:

[0057]

[0058] More specifically, the code of the interface method may include method comments, method signature, pathurl, feedback of calling the hypertext transfer protocol deserialization process, and method signature.

[0059] It should also be noted that the generated interface mainly includes the service call address, service layer configuration, and creation or update of services.

[0060] As an embodiment, the generation of target language code includes: constructing different code sub-templates using different programming languages, inputting object entities into different code sub-templates at the same time, completing rendering through the Velocity template engine, and generating multiple target language codes corresponding to different programming languages.

[0061] For example, a Java sub-template is established based on the Java language, and a C# sub-template is established based on the C# language. The code template includes mutually independent Java sub-templates and C# sub-templates. The object entity is input into the Java sub-template and the C# sub-template at the same time, and the corresponding Java target language code and C# target language code are obtained.

[0062] Based on established code templates, RESTful client interface call code is generated in multiple coding languages. This directly connects to package management platforms for various programming languages, then automatically pushes the code in multiple target languages ​​to the corresponding language package management repository for use by multiple RESTful interface callers. Generate once, and everyone can use it, reducing duplication of work. Developers can simply download the target language code from the language package management repository into the corresponding project or import the corresponding code dependencies. This significantly improves RESTful client code development efficiency. Furthermore, code generation functionality is integrated with multiple coding languages, including Java and C#, and can be expanded to more programming languages ​​as needed.

[0063] Furthermore, the automatic push includes uploading the generated multiple target language codes to multiple language pack management repositories.

[0064] Different language pack management repositories are used to store target language codes for different coding languages. For example, the generated Java class target language code is automatically pushed to the Maven repository, and the generated C# class target language code is automatically pushed to the NuGet repository.

[0065] As an example, the target language code version is maintained through service-level design. Maintaining interfaces and generated code at the service level allows for centralized management and reuse. The same business service only needs to generate language code once, which can be used by multiple upstream clients, avoiding repeated configuration, acquisition, parsing, and code generation.

[0066] It should be noted that the generated target language code can view service details such as the latest version number and version document URL, directly download the language code to the corresponding project for use, or introduce the corresponding code dependency package through different language pack management repositories for use. You can also switch to use historical versions of the target language code.

[0067] In the second aspect, the present invention also proposes a code generation management system for a RESTful API client, such as Figure 3 As shown, the system includes:

[0068] The service management module is used to obtain or create business services and transmit the content of business services to the OpenAPI protocol acquisition and parsing module;

[0069] The version management module is used to receive the content of the business service of the OpenAPI protocol acquisition and parsing module, determine whether the language code of the business service is the latest version, and feed back the result to the OpenAPI protocol acquisition and parsing module; further, the version management module can also record all historical versions, generation time, operator, remarks and other code information of the language code generated by a business service, which can be easily viewed and selected as needed.

[0070] The OpenAPI protocol acquisition and parsing module is used to receive the content of the business service, obtain the OpenAPI protocol based on the content of the business service, parse the OpenAPI protocol and convert it into an object entity, and send the object entity to the multi-language code generation module;

[0071] The multi-language code generation module is used to establish a code template, input the received object entity into the code template, and simultaneously generate target language codes in multiple programming languages ​​and send them to the code push module;

[0072] The code push module automatically pushes code for multiple target languages ​​to the corresponding language pack management repositories. This module automatically manages language code version numbers, compiles, and pushes language code to different language pack management repositories, eliminating the need for manual operations and improving code usage efficiency.

[0073] In a third aspect, the present invention provides a computer-readable storage medium having a computer program / instruction stored thereon, which implements the steps of any of the above methods when executed by a processor.

[0074] The system and storage medium are consistent with the technical solutions of the above method and will not be described in detail here.

[0075] It should be noted that for the aforementioned embodiments, for the sake of simplicity, they are all expressed as a series of action combinations, but those skilled in the art should be aware that this application is not limited to the order of the actions described. According to this application, certain steps can be performed in other orders or simultaneously. Secondly, those skilled in the art should also be aware that the embodiments described in this specification are all preferred embodiments, and the actions and modules involved are not necessarily required by this application.

[0076] In the above embodiments, the description of each embodiment has its own emphasis. For parts that are not described in detail in a certain embodiment, please refer to the relevant description of other embodiments. In the several embodiments provided in this application, it should be understood that the disclosed method or system can be implemented in other ways. For example, the embodiments described above are merely schematic. For example, the division of the units is only a logical function division. In actual implementation, there may be other division methods, such as multiple units or components can be combined or integrated into another system, or some features can be ignored or not executed.

[0077] The units described as separate components may or may not be physically separate, and the components shown as units may or may not be physical units, that is, they may be located in one place or distributed across multiple network units. Some or all of these units may be selected to achieve the purpose of this embodiment according to actual needs.

[0078] In addition, the functional units in the various embodiments of the present application may be integrated into a single processing unit, or each unit may exist physically separately, or two or more units may be integrated into a single unit. The aforementioned integrated units may be implemented in the form of hardware or software functional units.

[0079] The above is only an exemplary embodiment of the present disclosure and cannot be used to limit the scope of the present disclosure. That is, any equivalent changes and modifications made according to the teachings of the present disclosure are still within the scope of the present disclosure. After considering the specification and practicing the disclosure herein, those skilled in the art will easily think of the implementation scheme of the present disclosure. This application is intended to cover any variation, use or adaptation of the present disclosure, which follows the general principles of the present disclosure and includes common knowledge or customary technical means in the art that are not recorded in the present disclosure. The description and examples are to be regarded as exemplary only, and the scope and spirit of the present disclosure are defined by the claims.

[0080] The technical features of the above embodiments can be combined arbitrarily. In order to make the description concise, not all possible combinations of the technical features in the above embodiments are described. However, as long as there is no contradiction in the combination of these technical features, they should be considered to be within the scope of this specification.

[0081] It will be easily understood by those skilled in the art that the above description is only a preferred embodiment of the present invention and is not intended to limit the present invention. Any modifications, equivalent substitutions and improvements made within the spirit and principles of the present invention should be included in the scope of protection of the present invention.

Claims

1. A code generation management method for a RESTful API client, characterized in that: The method comprises: Obtain or create a business service and determine whether the language code of the business service is the latest version. If so, the user directly downloads the language code to the corresponding project or introduces the corresponding code dependency. Otherwise, regenerate the target language code of the RESTful API client. The target language code for generating the RESTful API client includes: Obtain the OpenAPI protocol based on the content of the business service, parse the OpenAPI protocol and convert it into an object entity; input the object entity into the established code template, and generate the target language code in multiple programming languages ​​at the same time; and automatically push the multiple target language codes to the corresponding language package management repository, and the user then downloads the target language code from the language package management repository to the corresponding project or introduces the corresponding code dependency.

2. A code generation management method for a RESTful API client according to claim 1, characterized in that: The content of the business service includes: service identifier, name, description, interface document URL, and interface path prefix.

3. A code generation management method for a RESTful API client according to claim 1, characterized in that: Methods for determining whether the language code of a business service is the latest version include: The content of the business service and the corresponding business input information in the database are obtained to determine whether the object entities of the two are consistent. If so, it means that the language code is the latest version; otherwise, it means that the language code is not the latest version.

4. A code generation management method for a RESTful API client according to claim 1, characterized in that: The object entity includes: request type, request path, request parameters, request header, request body, and response body.

5. A code generation management method for a RESTful API client according to claim 1, characterized in that: The generation of the target language code includes: Different code sub-templates are constructed using different programming languages, the object entities are simultaneously input into different code sub-templates, rendering is completed through the Velocity template engine, and a plurality of target language codes corresponding to different programming languages ​​are generated.

6. A code generation management method for a RESTful API client according to claim 5, characterized in that: The code sub-model is established as follows: Based on the coding habits of different languages, the class templates of the target language's request parameters and response parameters are defined through the Velocity template syntax, the request service and interface methods are defined, and a code sub-model is generated in the form of folder organization, program packages, and class method variable naming.

7. A code generation management method for a RESTful API client according to claim 1, characterized in that: The target language code is maintained through service granularity design.

8. A code generation management method for a RESTful API client according to claim 1, characterized in that: The automatic push includes: uploading the generated multiple target language codes to multiple language package management repositories.

9. A code generation management system for a RESTful API client, characterized in that: The system comprises: The service management module is used to obtain or create business services and transmit the content of business services to the OpenAPI protocol acquisition and parsing module; A version management module is used to receive the content of the business service of the OpenAPI protocol acquisition and parsing module, determine whether the language code of the business service is the latest version, and feed back the result to the OpenAPI protocol acquisition and parsing module; An OpenAPI protocol acquisition and parsing module is used to receive the content of the business service, obtain the OpenAPI protocol based on the content of the business service, parse the OpenAPI protocol and convert it into an object entity, and send the object entity to the multilingual code generation module; A multi-language code generation module is used to establish a code template, input the received object entity into the code template, and simultaneously generate the target language code in multiple programming languages ​​and send it to the code push module; The code push module is used to automatically push the target language codes to the corresponding language pack management warehouse.

10. A computer-readable storage medium having a computer program / instruction stored thereon, characterized in that: When the computer program / instruction is executed by a processor, the steps of the method according to any one of claims 1 to 8 are implemented.