Microservice code automatic generation method, system, device, medium and product

By automatically generating microservice codes, the problems of low automatic generation efficiency and inconsistent interface formats of microservice codes are solved, standardization and unification of codes are achieved, and development efficiency and quality are improved.

CN118796153APending Publication Date: 2024-10-18SHANXI CHINA MOBILE COMM CORP +1
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202410259037.X
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-03-07
Publication Date
2024-10-18

AI Technical Summary

Technical Problem

In the existing technology, the efficiency of automatic generation of microservice codes is low, and the programming interface formats of different developers are not uniform, resulting in code confusion and deviation from development specifications, making it difficult to ensure the robustness and efficiency of the code.

Method used

Provides a method for automatically generating microservice code. It obtains the microservice information input by the user, performs legality verification, and generates the target class source code required by the microservice framework based on the legal information, including interface classes, data transfer object DTO classes, For implementation classes and entity classes, code is generated using preset interface naming rules and JSON parameter parsing to ensure the standardization and uniformity of the code.

Benefits of technology

It realizes one-click automatic generation of source code for interface classes, DTO classes, implementation classes and entity classes, improves code development quality and generation efficiency, ensures the standardization and unification of interfaces, and reduces developers' workload and error rate.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN118796153A_ABST
    Figure CN118796153A_ABST
Patent Text Reader

Abstract

The invention relates to the technical field of computers, and provides a micro-service code automatic generation method, system and device, a medium and a product. The method comprises the following steps: acquiring micro-service information input by a user; the micro-service information comprises operation parameters of the micro-service engineering; carrying out legality verification on the micro-service information; if the validity verification is passed, generating a source code of a target class required by the micro-service framework based on the micro-service information; the target class comprises an interface class, a data transmission object DTO class, an implementation class and an entity class. According to the embodiment of the invention, the source codes of the interface class, the DTO class, the implementation class and the entity class are automatically generated through one key, developers are helped to concentrate on business logic arrangement, and meanwhile, the development quality and the generation efficiency of the codes are improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the field of computer technology, and in particular to a method, system, device, medium and product for automatically generating microservice code. Background Art

[0002] At present, two code automatic generation tools, EasyCode and Bito, are mainly used to generate microservice codes. Among them, EasyCode is mainly used to generate model entities, and Bito is mostly used for code completion in the process of writing code logic. Under microservices, microservice framework codes need to be generated based on different microservice requirements, and there is currently no corresponding tool to achieve this. At the same time, in the actual development process, the programming interface formats of different developers are not unified, and with the long-term iteration of the project, the code of the interface layer will become more and more chaotic and deviate from the established development specifications, resulting in low efficiency in the automatic generation of microservice code. Summary of the invention

[0003] The embodiments of the present application provide a method, system, device, medium and product for automatically generating microservice code, so as to solve the problem of low efficiency of automatically generating microservice code in the prior art.

[0004] In a first aspect, an embodiment of the present application provides a method for automatically generating microservice code, including:

[0005] Obtaining microservice information input by a user; the microservice information includes operating parameters of the microservice project;

[0006] Performing a legality check on the microservice information;

[0007] If the legality check passes, the source code of the target class required by the microservice framework is generated based on the microservice information; the target class includes an interface class, a data transmission object DTO class, an implementation class and an entity class.

[0008] According to a method for automatically generating microservice code provided by an embodiment of the present application, the source code of a target class required by a microservice framework is generated based on the microservice information, including:

[0009] Based on the preset interface naming information, the interface name in the microservice information is parsed to obtain the source code of the interface class; the interface class includes an interface class name, an implementation class name and a method name.

[0010] According to a method for automatically generating microservice code provided by an embodiment of the present application, the source code of a target class required by a microservice framework is generated based on the microservice information, including:

[0011] Parse the JSON input parameters and JSON output parameters in the microservice information to obtain data field information to be encapsulated;

[0012] Create an empty input parameter DTO class and an empty output parameter DTO class;

[0013] Based on the data field information to be encapsulated, determine first attribute information of the JSON input parameter and second attribute information of the JSON output parameter;

[0014] The first attribute information and its method are added to the empty input parameter DTO class, and the second attribute information and its method are added to the empty output parameter DTO class to generate the source code of the DTO class.

[0015] According to a method for automatically generating microservice code provided by an embodiment of the present application, the source code of a target class required by a microservice framework is generated based on the microservice information, including:

[0016] Based on the microservice information, obtain the interface name, input parameter DTO class name, output parameter DTO class name and entity class name;

[0017] Create a Java class file based on the interface name, the input parameter DTO class name, the output parameter DTO class name and the entity class name;

[0018] In the Java class file, the business logic processing information of the method defined in the interface class is determined to generate the source code of the implementation class.

[0019] According to a method for automatically generating microservice code provided by an embodiment of the present application, the source code of a target class required by a microservice framework is generated based on the microservice information, including:

[0020] Parse the JSON input parameters in the microservice information to obtain all node information in the JSON input parameters; the node information includes node hierarchy, node content, node type, and upper and lower node information;

[0021] Based on the node information, a Java object is stored to generate source code of the entity class.

[0022] According to a method for automatically generating microservice code provided by an embodiment of the present application, the legitimacy check includes interface naming specification check, interface directory legitimacy check, method same name check, class same name check, JSON input parameter legitimacy check and JSON output parameter legitimacy check; the legitimacy check of the microservice information includes:

[0023] Performing the interface naming specification verification and interface directory legitimacy verification on the microservice information;

[0024] If both the interface naming specification check and the interface directory legitimacy check are passed, the method same name check and the class same name check are performed on the microservice information;

[0025] If both the method same name check and the class same name check pass, a legitimacy check of the JSON input parameter and a legitimacy check of the JSON output parameter are performed on the microservice information.

[0026] According to a method for automatically generating microservice code provided in an embodiment of the present application, the step of obtaining microservice information input by a user includes:

[0027] Display the microservice project;

[0028] Through the front-end UI component, the microservice information of the microservice project input by the user is obtained.

[0029] In a second aspect, an embodiment of the present application provides a microservice code automatic generation system, including: a front-end UI component, an interceptor component, and a code automatic generation component;

[0030] The front-end UI component is used to display the microservice information input by the user;

[0031] The interceptor component is used to verify the legitimacy of the microservice information;

[0032] The automatic code generation component is used to generate source code of the target class required by the microservice framework based on the microservice information.

[0033] In a third aspect, an embodiment of the present application provides an electronic device, comprising a processor and a memory storing a computer program, wherein when the processor executes the program, the steps of the method for automatically generating microservice code described in the first aspect are implemented.

[0034] In a fourth aspect, an embodiment of the present application provides a non-transitory computer-readable storage medium having a computer program stored thereon, and when the computer program is executed by a processor, the steps of the method for automatically generating microservice code described in the first aspect are implemented.

[0035] In a fifth aspect, an embodiment of the present application provides a computer program product, including a computer program, which, when executed by a processor, implements the steps of the method for automatically generating microservice code described in the first aspect.

[0036] The microservice code automatic generation method, system, device, medium and product provided in the embodiments of the present application obtain the microservice information input by the user; the microservice information includes the operating parameters of the microservice project; the microservice information is verified for legality; if the legality verification passes, the source code of the target class required by the microservice framework is generated based on the microservice information; the target class includes the interface class, the data transmission object DTO class, the implementation class and the entity class. The embodiment of the present application automatically generates the source code of the interface class, DTO class, implementation class and entity class with one click, which helps developers focus on business logic arrangement and improves the development quality and generation efficiency of the code. BRIEF DESCRIPTION OF THE DRAWINGS

[0037] In order to more clearly illustrate the technical solutions in the present application or the prior art, a brief introduction will be given below to 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 application. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying any creative work.

[0038] Figure 1 It is a flowchart of a method for automatically generating microservice code provided in an embodiment of the present application;

[0039] Figure 2 It is a structural diagram of a microservice code automatic generation system provided in an embodiment of the present application;

[0040] Figure 3 It is a schematic diagram of the processing flow of the interceptor component provided in an embodiment of the present application;

[0041] Figure 4 It is a schematic diagram of the structure of an electronic device provided in an embodiment of the present application. DETAILED DESCRIPTION

[0042] In order to make the purpose, technical solutions and advantages of this application clearer, the technical solutions in this application will be clearly and completely described below in conjunction with the drawings in the embodiments of this application. Obviously, the described embodiments are part of the embodiments of this application, not all of them. Based on the embodiments in this application, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of this application.

[0043] Microservices (or microservices architecture) is a cloud-native architecture approach that includes many small, loosely coupled and independently deployable components or services in a single application. Microservices architecture makes applications easier to scale and faster to develop, thereby accelerating innovation and shortening the time to market for new features.

[0044] At present, two code automatic generation tools, EasyCode and Bito, are mainly used to generate microservice codes. Among them, EasyCode is mainly used to generate model entities, and Bito is mostly used for code completion in the process of writing code logic. Under microservices, it is necessary to generate microservice framework codes based on different microservice requirements, and automatically generate source codes for interface classes, DTO (Data Transfer Object, data transfer object) classes, implementation classes, and entity classes. Currently, there is no corresponding tool to achieve this.

[0045] In the actual development process, different developers do not use the same programming interface format, and as the project iterates over a long period of time, the interface layer code becomes increasingly confusing and deviates from the established development specifications. For example, DTO class code writing is prone to omissions and errors, which lead to program bugs and make it difficult to ensure the robustness of the program. Many system problems are ultimately caused by omissions in the writing of input and output parameters. For example, the parameter PHONE_NO may be written as PHONE_No by developers due to negligence, which may not be easily detected during the development process, resulting in the problem being exposed only in the testing phase or even the production phase, leading to unnecessary production waste.

[0046] In order to solve the problem that the existing technology cannot automatically generate the source code of interface class, DTO class, implementation class and entity class when generating microservice framework code based on different microservice requirements, resulting in low efficiency of automatic microservice code generation, the present application proposes a method for automatic microservice code generation.

[0047] Figure 1 Schematic diagram of the process of automatically generating microservice code provided by the embodiment of the present application. Figure 1 , the embodiment of the present application provides a method for automatically generating microservice code, which may include:

[0048] Step 100: Obtain microservice information input by the user.

[0049] Among them, the microservice information includes the operating parameters of the microservice project, which include the interface name, the location of the microservice project, Json input parameters (or Json input parameters) and Json output parameters (or Json output parameters).

[0050] Specifically, the microservice project is automatically scanned to obtain a set of microservice local paths, and a desired microservice project is selected. The desired microservice project is displayed, and the microservice information of the microservice project input by the user is obtained through the front-end UI component.

[0051] Step 200: verify the legitimacy of the microservice information.

[0052] Pre-configure multiple regular expressions named in the microservice development specification, including regular expressions that match interface naming, interface directory naming, method naming, etc., and then verify the legality of the input interface name, Json input parameters, and Json output parameters based on the pre-configured regular expressions.

[0053] Step 300: If the legality check passes, the source code of the target class required by the microservice framework is generated based on the microservice information.

[0054] If the legality check passes, the source code of the target class required by the microservice framework is generated based on the microservice information. For example, the source code of the target class is automatically generated with one click through the code automatic generation component. The target class includes the interface class, the data transfer object DTO class, the implementation class and the entity class, where:

[0055] Interface class: In object-oriented programming, an abstract class used to define a set of method signatures. It is only responsible for providing an interface but does not implement specific business logic. In programming, an interface class is used to define the specifications and signatures of a set of operations, while the code that implements these operations is located in the implementation files of its subclasses or other classes.

[0056] DTO class: An object used to transfer data between the client and the server. The DTO class contains the data fields that need to be transferred and the corresponding getter and setter methods. It generally does not contain business logic and is only used for data transmission.

[0057] Implementation class: used to implement the methods defined in the interface class and provide corresponding business logic processing. An implementation class is a specific class that implements an interface, in which other services or methods can be called to complete specific business functions.

[0058] Entity class: used to represent entities or objects in the business domain, such as users, orders, products, etc. The entity class contains information such as the attributes, methods, and behaviors of the entity.

[0059] The microservice code automatic generation method provided in the embodiment of the present application obtains the microservice information input by the user; the microservice information includes the operating parameters of the microservice project; the microservice information is verified for legitimacy; if the legitimacy verification passes, the source code of the target class required by the microservice framework is generated based on the microservice information; the target class includes the interface class, the data transmission object DTO class, the implementation class and the entity class. The embodiment of the present application automatically generates the source code of the interface class, the DTO class, the implementation class and the entity class with one click, which helps developers focus on business logic arrangement and improves the development quality and generation efficiency of the code.

[0060] Based on the above embodiment, the source code of the target class required by the microservice framework is generated based on the microservice information, including:

[0061] Step 311, based on the preset interface naming information, the interface name in the microservice information is parsed to obtain the source code of the interface class; the interface class includes an interface class name, an implementation class name and a method name.

[0062] The preset interface naming information refers to the preset interface naming rules: package name-interface name-Svc-method name. Then, the interface name that has passed the verification is parsed according to the preset interface naming rules to generate the source code of the interface class, including the interface class name, implementation class name, and method name. For example, the source code of the interface class can be generated by the following steps:

[0063] Generate interface class name: According to the interface naming rules, the interface name is between "package name-Svc", so you can get the interface name by intercepting this part, and then use the interface name as the interface class name.

[0064] Generate implementation class name: When generating implementation class name based on interface name, you can remove the prefix of interface name and keep the first letter capitalized. For example, if the interface name is "IGrpOnePlus", the corresponding implementation class name is "GrpOnePlus" after removing the "I" prefix.

[0065] Generate method name: According to the interface naming rules, the method name is located after "Svc-", so you can get the method name by intercepting this part.

[0066] For example, suppose the interface information entered by the front end is:

[0067] com_sitech_crmoup_grp_inter_IGrpOnePlusSvc_getFee;

[0068] Based on the interface naming rules, the interface class name is IGrpOnePlus, the implementation class name is GrpOnePlus, and the method name is getFee.

[0069] The embodiment of the present application can standardize and unify the interface definition by automatically generating the source code of the interface class, making the interface easier to use and manage, thereby improving the development quality and generation efficiency of the code. On the other hand, through the definition of the naming rule, the various parts of the interface can be quickly identified, including the interface name, the implementation class name and the method name, etc., so as to facilitate development and maintenance.

[0070] Based on the above embodiment, the source code of the target class required by the microservice framework is generated based on the microservice information, including:

[0071] Step 321, parsing the JSON input parameters and the JSON output parameters in the microservice information to obtain data field information to be encapsulated;

[0072] Step 322, create an empty input parameter DTO class and an empty output parameter DTO class;

[0073] Step 323, based on the data field information to be encapsulated, determine the first attribute information of the JSON input parameter and the second attribute information of the JSON output parameter;

[0074] Step 324, adding the first attribute information and its method to the empty input parameter DTO class, and adding the second attribute information and its method to the empty output parameter DTO class, so as to generate the source code of the DTO class.

[0075] Generate an empty input parameter DTO class and an empty output parameter DTO class through the class generation method provided by Intellij SDK, traverse the obtained input and output parameter data objects, fill in the properties and methods corresponding to the input and output parameters, and obtain the source code of the input parameter DTO class and the source code of the output parameter DTO class.

[0076] For example, you can generate the source code of the DTO class by following these steps:

[0077] Step 1: Parse the JSON input parameters and JSON output parameters: Recursively parse the data structure of the JSON input parameters and JSON output parameters to obtain the data field information of the DTO class that needs to be encapsulated.

[0078] It is understandable that when parsing JSON input parameters and JSON output parameters, if the data structure has complex situations such as nesting and circular references, it is necessary to recursively traverse the entire data structure to ensure that all data field information that needs to be encapsulated as a DTO class can be fully obtained. For example, the steps of recursively parsing JSON input parameters and JSON output parameters include:

[0079] 1.1) Check the current node: First check the type and attributes of the current node to determine whether the node needs to be further parsed.

[0080] 1.2) Recursively parse child nodes: If the current node is a composite type (such as an object, array, etc.), its child nodes need to be recursively parsed to obtain the data field information of the child nodes.

[0081] 1.3) Merge field information: Merge the data field information of the current node and the child nodes to construct the complete data field information of the DTO class.

[0082] 1.4) Return result: Return the parsed data field information to the upper layer call, or continue to process other nodes.

[0083] Through recursive parsing, various complex data structures can be effectively processed, including multi-layer nesting, circular references, etc., so as to accurately extract the data field information that needs to be encapsulated as a DTO class.

[0084] Step 2. Use the class generation method provided by Intellij SDK to generate empty input parameter DTO class and empty output parameter DTO class: In IntelliJ IDEA, use shortcut keys (such as Alt+Insert) or the function in the right-click menu to generate an empty Java class file as the basic template for the DTO class.

[0085] Step 3: Determine the attributes of the DTO class based on the parsing results: Determine the attribute information that the DTO class needs to include based on the data field information obtained through the parsing, where each attribute corresponds to a data field.

[0086] Step 4. Add attributes to the DTO class: Add attributes corresponding to the data fields in the empty input parameter DTO class and the empty output parameter DTO class. You can define the attributes by selecting the appropriate data type according to the field type.

[0087] Step 5. Add getter and setter methods: Add corresponding getter and setter methods for each attribute so that you can get and set the value of the attribute.

[0088] Step 6. Generate DTO class: Based on the parsing results, build the overall structure of the DTO class, including properties, methods, etc., to ensure that the DTO class can accurately represent the structure and content of the data.

[0089] For example, suppose you need to generate a DTO class that represents student information, including student name, age, and gender. First, you can parse the data structure of student information to determine the fields and types that need to be included. Then, use IntelliJ IDEA to generate an empty StudentDTO class. Finally, fill the parsed fields and types into the empty StudentDTO class to obtain the StudentDTO class.

[0090] The embodiment of the present application can clearly define and encapsulate the data structure of the input and output parameters by automatically generating the source code of the DTO class, thereby improving the readability and maintainability of the code. At the same time, by recursively parsing the JSON input parameters and JSON output parameters, the respective data structures and attributes can be obtained and encapsulated into the corresponding DTO class. Based on this, when writing specific code, the DTO class can be used directly without caring about the specific data structure and attributes, thereby improving the development quality and generation efficiency of the code.

[0091] Based on the above embodiment, the source code of the target class required by the microservice framework is generated based on the microservice information, including:

[0092] Step 331, based on the microservice information, obtain the interface name, input parameter DTO class name, output parameter DTO class name and entity class name;

[0093] Step 332, creating a Java class file based on the interface name, the input parameter DTO class name, the output parameter DTO class name and the entity class name;

[0094] Step 333: In the Java class file, determine the business logic processing information of the method defined in the interface class to generate the source code of the implementation class.

[0095] Generate the source code of the implementation class based on the input interface name, input parameter DTO class name, output parameter DTO class name and entity class name. The input parameter DTO class name refers to the input parameter transfer object class, and the output parameter DTO class name refers to the parameter transfer object class. Specifically, the source code of the implementation class can be generated by the following steps:

[0096] Step 1. Create a Java class file and name it the required implementation class name.

[0097] Step 2: Implement the methods defined in the interface in the Java class file and write specific business logic.

[0098] Step 3: Call the corresponding service, process the input parameters and generate output results.

[0099] Step 4: Perform exception handling and error handling as needed, and finally save the implementation class to the specified location or directory.

[0100] For example, suppose the input information is: interface name: IUserInfo, input parameter transmission object class name: UserInfoInDTO, output parameter transmission object class name: UserInfoOutDTO, implementation class name: UserInfoImpl and entity class name: UserInfoBusi. If an addUser method is defined in the IUserInfo interface for adding users, the implementation class UserInfoImpl.java needs to make sure that the addUser method calls the addUser method (business logic is written by the developer) of the entity class UserInfoBusi to implement the business logic. The implementation class UserInfoImpl implements the IUserInfo interface and overrides the addUser method therein. This method receives a UserInfoInDTO object as a parameter, which represents the user input information, and then performs the business logic processing of adding the user based on the input information, and finally returns a UserInfoOutDTO object, which represents the result information of adding the user.

[0101] The embodiment of the present application automatically generates the source code of the implementation class, which can automatically generate some repetitive codes and templates, reducing the workload of developers. At the same time, through the automatically generated implementation class, the functions defined by the interface can be quickly implemented, and due to the uniformity of the code template, code errors and maintenance difficulties can be reduced.

[0102] Based on the above embodiment, the source code of the target class required by the microservice framework is generated based on the microservice information, including:

[0103] Step 341, parse the JSON input parameter in the microservice information to obtain all node information in the JSON input parameter; the node information includes node hierarchy, node content, node type, and upper and lower node information;

[0104] Step 342: storing the node information into a Java object to generate source code of the entity class.

[0105] Use recursive traversal on the JSON input parameters to obtain all the details of the JSON input parameters. If the JSON input parameter node is a root node, continue to traverse its nodes until all nodes are obtained, and store the node information in a Java object in turn, including node hierarchy, node content, node type, and upper and lower node information, to generate the source code of the entity class.

[0106] For example, you can generate the source code of the entity class by following these steps:

[0107] Step 1: Parse the input JSON message: Parse the JSON message recursively to obtain all node information in the message, for example, using a JSON parsing library such as Jackson, Gson, etc. The input JSON message can be understood as a JSON input parameter or an input transmission message.

[0108] Step 2: Create entity class objects: Create corresponding entity class objects according to the information of the message node. Each message node can correspond to an entity class, including attributes such as node hierarchy, node content, node type, and upper and lower node information.

[0109] Step 3: Organize the relationship between entity classes: Organize the relationship between entity classes according to the superior-subordinate relationship of nodes. For example, add references to parent nodes and child nodes in the entity class to facilitate operations in the program.

[0110] Step 4: Store entity class objects: Store the created entity class objects. You can choose to store them in a collection in memory or in a database.

[0111] The embodiment of the present application automatically generates the source code of the entity class, and can convert the JSON message into a Java object, which is convenient for subsequent data operations and logical processing. At the same time, by recursively traversing the JSON message, all the details of the entire message object can be obtained, including node hierarchy, content, type, and upper and lower node information, etc., and storing this information in the Java object can facilitate data operations and logical processing, further realizing the function of the interface.

[0112] Based on the above embodiment, the legitimacy of the microservice information is verified, including:

[0113] Step 211, performing the interface naming specification verification and interface directory legitimacy verification on the microservice information;

[0114] Step 212: If both the interface naming specification check and the interface directory legitimacy check are passed, the method same name check and the class same name check are performed on the microservice information;

[0115] Step 213: If both the method same name check and the class same name check pass, a validity check of the JSON input parameter and a validity check of the JSON output parameter are performed on the microservice information.

[0116] Among them, the legality check includes interface naming standard check, interface directory legality check, method same name check, class same name check, JSON input parameter legality check and JSON output parameter legality check.

[0117] Interface naming standard verification: compare the interface name entered by the front end with the preset regular expression of matching interface naming to check whether it meets the naming standard. If the interface name does not meet the standard, the system will feedback to the front end to re-enter it.

[0118] Interface directory validity check: compare the interface directory name entered by the front end with the preset regular expression of matching interface directory naming to check whether it meets the specifications. If the directory name does not meet the specifications, the system will remind the front end to re-enter it.

[0119] Method name verification: The system will scan all files in the global directory to check whether there is a method with the same name. If there is a method with the same name, the system will feedback to the front end that the duplicate method needs to be processed.

[0120] Class name verification: The system will check whether there is a class with the same name in the global directory. If there is a class with the same name, the system will ask the front end to handle this duplicate naming situation.

[0121] Verify the validity of JSON input parameters: When parsing the input JSON message, the system will check whether the node naming and hierarchy in the message conform to the development specifications. If it is found that the node naming does not conform to the specifications or the hierarchy is wrong, the system will prompt the front-end to re-enter.

[0122] Validation of JSON output parameters: Similar to the validation of JSON input parameters, the system will also parse the output JSON message and check its validity. If the node naming or hierarchy in the output JSON message does not meet the specification, the system will ask the front end to make corrections.

[0123] Among them, the legitimacy of microservice information can be verified through the following steps:

[0124] 1. Interface naming specifications, interface directory legitimacy check and method name verification: The system uses pre-configured regular expressions to match the interface name, directory name and method name entered by the front end to check whether they meet the specifications. If any of them does not meet the specifications, the system will immediately feedback to the front end to re-enter to ensure that the input meets the specifications.

[0125] 2. After the interface is verified, the class and method are verified: Once the interface name, directory name and method name are verified, the system will globally scan all files in the directory to check whether there are classes or methods with the same name. If there are classes or methods with the same name, the system will require the front end to re-enter to avoid naming conflicts.

[0126] 3. After the class and method are verified, the legality of the input and output JSON messages is verified: the system will parse the input and output JSON messages and judge the nodes in them to ensure that the naming and hierarchical structure in the nodes meet the development specifications. If any non-compliance is found, the system will prompt the front-end to re-enter to ensure the legality of the data format.

[0127] The embodiment of the present application uses a preset regular expression, and the system can quickly verify the input interface information and JSON message, effectively avoiding the complexity and inaccuracy of manual verification and improving development efficiency. Verifying the interface, input parameters and output parameters can ensure that the input data meets the development specifications and requirements, effectively avoiding code errors and exceptions caused by problems such as naming and formatting, and improving code quality. On the other hand, by fully verifying the interface naming, input parameter objects and output parameter objects, the integrity, standardization and accuracy of the generated content are guaranteed, while improving the robustness of the program, and effectively reducing the problems caused by negligence due to developers, reducing waste in the production process.

[0128] In order to further analyze and explain the method for automatically generating microservice code proposed in this application, refer to the following embodiments.

[0129] The embodiment of the present application specifically proposes a plug-in interface microservice code automatic generation method. Through a plug-in for generating a microservice code framework, and importing the plug-in into a local development tool, the microservice framework code is generated at one time. The microservice framework code includes the interface class, DTO class, implementation class and entity class required for microservice interface development. Specifically, the following steps are included:

[0130] Step 1: Automatically scan microservice projects, obtain a collection of microservice local paths, and select the required microservice project.

[0131] Get the current window project through Intellij SDK, get the root project where the editor is currently located, scan all directories under the root project, scan the pom.xml files in the subdirectories. When the value corresponding to the parameter packaging in the file is jar, it indicates that the subdirectory is a microservice code project directory. Save the local path of the subdirectory into a collection. After all scans are completed, a collection of microservice local paths is obtained.

[0132] The collection of microservice local paths is stored in the front-end UI component. By clicking the drop-down box of the front-end UI component, the microservice project collection is displayed, and the required microservice project is selected from it.

[0133] Step 2: Enter the interface name, Json input parameter, and Json output parameter in the front-end UI component according to the required microservice project.

[0134] When automatically generating microservice code, you need to obtain four types of parameters required for the microservice code to run: interface name, microservice project location, Json input parameter, and Json output parameter. Therefore, after determining the microservice project, you need to determine the interface name, Json input parameter, and Json output parameter of the microservice project. The microservice project location refers to the location of the currently opened microservice project.

[0135] Step 3: Verify the legality of the input interface name, Json input parameter, and Json output parameter.

[0136] The system is pre-configured with multiple regular expressions named in the microservice development specification, including regular expressions that match interface naming, interface directory naming, and method naming. Based on these regular expressions, the input interface name, Json input parameter, and Json output parameter are verified for legality, including:

[0137] Step 3.1: Check the interface naming convention, interface directory legitimacy and method name homonym. After the verification passes, proceed to step 3.2.

[0138] The interceptor component matches the interface name, directory name and method name input by the front end with the preset regular expressions of matching interface naming, interface directory naming and method naming. If one item does not match, the item will be fed back to the front end, and the front end needs to re-enter it before continuing the subsequent operation.

[0139] Step 3.2: After the interface is verified, verify the class and method. After the verification is passed, proceed to step 3.3.

[0140] Scan all files in the directory globally to check whether there is a class or method with the same name. If so, the check fails and feedback is given to the front end, which needs to re-enter before continuing with subsequent operations.

[0141] Step 3.3: After the class and method are verified, verify the legitimacy of the input and output Json messages.

[0142] Parse the message and judge the nodes in it. If the naming and node hierarchy in the node do not meet the development specifications, feedback will be given to the front end, which needs to re-enter the information before continuing the subsequent operations. If it meets the development specifications, the verification passes.

[0143] Step 4: Generate an interface class for the interface name that passes verification.

[0144] According to the naming rules of the interface: package name-interface name-Svc-method name, parse the interface name entered in step 2 to obtain the interface name, implementation class name and method name, and save them as the class name or method name for the subsequent generation of related classes.

[0145] Step 5: Generate an implementation class. The implementation class name includes the input parameter transfer object class name (i.e. input parameter DTO class name), the output parameter transfer object class name (i.e. output parameter DTO class name) and the entity class name.

[0146] Step 6: Generate entity class. Use recursive traversal to traverse the Json message object input in step 2 to obtain all the details of the entire message object. If the Json message node is a root node, continue to traverse its lower nodes until all nodes are obtained, and store the node information in a JAVA object in turn, including node hierarchy, node content, node type, and upper and lower node information.

[0147] Step 7: Recursively parse the input parameter transmission class and output parameter transmission class messages to generate the DTO class.

[0148] Generate an empty input and output parameter JAVA data transmission object class through the class generation method provided by Intellij SDK, traverse the obtained input and output parameter data objects, fill in the properties and methods corresponding to the input and output parameters, and get GrpOnePlusInDto and GrpOnePlusOutDto, which represent the input parameter DTO class and the output parameter DTO class respectively.

[0149] The embodiment of the present application generates interface classes, implementation classes, entity classes and DTO classes through the class generation method provided by Intellij SDK, fills the configured template codes into these classes, and replaces the relevant characters in the template codes in combination with the names generated in the above steps to obtain interface classes, implementation classes, entity classes and DTO classes respectively. Based on different microservice requirements, the embodiment of the present application can automatically generate the source code of the interface classes, DTO classes, implementation classes and entity classes required by the microservices, greatly improving the development quality and efficiency.

[0150] refer to Figure 2 The embodiment of the present application proposes a microservice code automatic generation system, specifically a plug-in interface microservice code automatic generation system, including a front-end UI component, an interceptor component, a code automatic generation component, a code formatting component and a plug-in installation component.

[0151] Front-end UI component, used to display microservice information entered by the user. For example, it is used to display the four parameters entered by the user, such as the interface name, microservice project location, Json input parameter, and Json output parameter. The plug-in scans the current directory by itself, identifies the microservice project under the directory, and displays it in the front-end UI window for developers to select the location for code generation.

[0152] The interceptor component is used to verify the validity of microservice information. For example, refer to Figure 3 The interceptor component processing flow includes: interface naming specification verification, interface directory validity check, method name verification, class name verification, input parameter Json message validity check and output parameter Json message validity check for the microservice information input by the user. If the verification passes, the verified microservice information will be output to the code automatic generation component.

[0153] The code automatic generation component is used to generate the source code of the target class required by the microservice framework based on the microservice information. For example, it parses the interface name, converts the input and output parameter messages into JAVA collection objects, and generates the source code of the DTO class of the input and output parameter transmission classes, as well as the source code of the interface class, implementation class, and entity class.

[0154] The code formatting component is the preferred component, which is used to format the generated code files according to the code formatting component provided in the IDEA SDK to improve the readability of the generated code.

[0155] The plug-in installation component imports the plug-in into the local development tool. Since the plug-in introduces Lombok, the plug-in installation component adds the Maven dependency of Lombok to the project pom.xml file, and then runs the plug-in. The user can pull up the UI page for automatically generating code by clicking GeneratorOup, and then enter relevant information as needed.

[0156] The plug-in interface microservice code automatic generation system provided in the embodiment of the present application can automatically generate the source code of the interface class, DTO class, implementation class and entity class required by the microservice based on different microservice requirements, greatly improving the development quality and efficiency.

[0157] Figure 4 An example of a physical structure diagram of an electronic device is shown in FIG. Figure 4 As shown, the electronic device may include: a processor 410, a communication interface 420, a memory 430 and a communication bus 440, wherein the processor 410, the communication interface 420, and the memory 430 communicate with each other through the communication bus 440. The processor 410 may call a computer program in the memory 430 to execute the steps of the microservice code automatic generation method, for example, including:

[0158] Obtaining microservice information input by a user; the microservice information includes operating parameters of the microservice project;

[0159] Performing a legality check on the microservice information;

[0160] If the legality check passes, the source code of the target class required by the microservice framework is generated based on the microservice information; the target class includes an interface class, a data transmission object DTO class, an implementation class and an entity class.

[0161] In addition, the logic instructions in the above-mentioned memory 430 can be implemented in the form of a software functional unit and can be stored in a computer-readable storage medium when it is sold or used as an independent product. Based on this understanding, the technical solution of the present application can be essentially or partly embodied in the form of a software product that contributes to the prior art, and the computer software product is stored in a storage medium, including several instructions to enable a computer device (which can be a personal computer, a server, or a network device, etc.) to perform all or part of the steps of the method described in each embodiment of the present application. The aforementioned storage medium includes: various media that can store program codes, such as a USB flash drive, a mobile hard disk, a read-only memory (ROM), a random access memory (RAM), a disk or an optical disk.

[0162] On the other hand, an embodiment of the present application further provides a non-transitory computer-readable storage medium on which a computer program is stored. When the computer program is executed by a processor, the steps of the method for automatically generating microservice code provided in the above embodiments are implemented, for example, including:

[0163] Obtaining microservice information input by a user; the microservice information includes operating parameters of the microservice project;

[0164] Performing a legality check on the microservice information;

[0165] If the legality check passes, the source code of the target class required by the microservice framework is generated based on the microservice information; the target class includes an interface class, a data transmission object DTO class, an implementation class and an entity class.

[0166] On the other hand, an embodiment of the present application further provides a computer program product, the computer program product including a computer program, the computer program can be stored on a non-transitory computer-readable storage medium, and when the computer program is executed by a processor, the computer can perform the steps of the microservice code automatic generation method provided in the above embodiments, for example, including:

[0167] Obtaining microservice information input by a user; the microservice information includes operating parameters of the microservice project;

[0168] Performing a legality check on the microservice information;

[0169] If the legality check passes, the source code of the target class required by the microservice framework is generated based on the microservice information; the target class includes an interface class, a data transmission object DTO class, an implementation class and an entity class.

[0170] The device embodiments described above are merely illustrative, wherein the units described as separate components may or may not be physically separated, and the components displayed as units may or may not be physical units, that is, they may be located in one place, or they may be distributed on multiple network units. Some or all of the modules may be selected according to actual needs to achieve the purpose of the scheme of this embodiment. Ordinary technicians in this field can understand and implement it without paying creative labor.

[0171] Through the description of the above implementation methods, those skilled in the art can clearly understand that each implementation method can be implemented by means of software plus a necessary general hardware platform, and of course, it can also be implemented by hardware. Based on this understanding, the above technical solution is essentially or the part that contributes to the prior art can be embodied in the form of a software product, and the computer software product can be stored in a computer-readable storage medium, such as ROM / RAM, a disk, an optical disk, etc., including a number of instructions for a computer device (which can be a personal computer, a server, or a network device, etc.) to execute the methods described in each embodiment or some parts of the embodiments.

[0172] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present application, rather than to limit it. Although the present application has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that they can still modify the technical solutions described in the aforementioned embodiments, or make equivalent replacements for some of the technical features therein. However, these modifications or replacements do not deviate the essence of the corresponding technical solutions from the spirit and scope of the technical solutions of the embodiments of the present application.

Claims

1. A method for automatically generating microservice code, characterized in that: include: Get the microservice information entered by the user; The microservice information includes operating parameters of the microservice project; Performing a legality check on the microservice information; If the legality check passes, the source code of the target class required by the microservice framework is generated based on the microservice information; the target class includes an interface class, a data transmission object DTO class, an implementation class and an entity class.

2. The method for automatically generating microservice code according to claim 1, characterized in that: The generating the source code of the target class required by the microservice framework based on the microservice information includes: Based on the preset interface naming information, the interface name in the microservice information is parsed to obtain the source code of the interface class; the interface class includes an interface class name, an implementation class name and a method name.

3. The method for automatically generating microservice code according to claim 1, characterized in that: The generating the source code of the target class required by the microservice framework based on the microservice information includes: Parse the JSON input parameters and JSON output parameters in the microservice information to obtain data field information to be encapsulated; Create an empty input parameter DTO class and an empty output parameter DTO class; Based on the data field information to be encapsulated, determine first attribute information of the JSON input parameter and second attribute information of the JSON output parameter; The first attribute information and its method are added to the empty input parameter DTO class, and the second attribute information and its method are added to the empty output parameter DTO class to generate the source code of the DTO class.

4. The method for automatically generating microservice code according to claim 1, characterized in that: The generating the source code of the target class required by the microservice framework based on the microservice information includes: Based on the microservice information, obtain the interface name, input parameter DTO class name, output parameter DTO class name and entity class name; Create a Java class file based on the interface name, the input parameter DTO class name, the output parameter DTO class name and the entity class name; In the Java class file, the business logic processing information of the method defined in the interface class is determined to generate the source code of the implementation class.

5. The method for automatically generating microservice code according to claim 1, characterized in that: The generating the source code of the target class required by the microservice framework based on the microservice information includes: Parse the JSON input parameters in the microservice information to obtain all node information in the JSON input parameters; the node information includes node hierarchy, node content, node type, and upper and lower node information; Based on the node information, a Java object is stored to generate source code of the entity class.

6. The method for automatically generating microservice code according to claim 1, characterized in that: The legality check includes interface naming specification check, interface directory legality check, method same name check, class same name check, JSON input parameter legality check and JSON output parameter legality check; Performing a validity check on the microservice information, including: Performing the interface naming specification verification and interface directory legitimacy verification on the microservice information; If both the interface naming specification check and the interface directory legitimacy check are passed, the method same name check and the class same name check are performed on the microservice information; If both the method same name check and the class same name check pass, a legitimacy check of the JSON input parameter and a legitimacy check of the JSON output parameter are performed on the microservice information.

7. The method for automatically generating microservice code according to claim 6, characterized in that: The obtaining of microservice information input by the user includes: Display the microservice project; Through the front-end UI component, the microservice information of the microservice project input by the user is obtained.

8. A microservice code automatic generation system, characterized in that: The microservice code automatic generation method applied to any one of claims 1 to 7 comprises a front-end UI component, an interceptor component and a code automatic generation component; The front-end UI component is used to display the microservice information input by the user; The interceptor component is used to verify the legitimacy of the microservice information; The automatic code generation component is used to generate source code of the target class required by the microservice framework based on the microservice information.

9. An electronic device comprising a processor and a memory storing a computer program, characterized in that: When the processor executes the computer program, the steps of the method for automatically generating microservice code according to any one of claims 1 to 7 are implemented.

10. A non-transitory computer-readable storage medium having a computer program stored thereon, characterized in that: When the computer program is executed by a processor, the steps of the method for automatically generating microservice code according to any one of claims 1 to 7 are implemented.

11. A computer program product, comprising a computer program, characterized in that When the computer program is executed by a processor, the steps of the method for automatically generating microservice code according to any one of claims 1 to 7 are implemented.