A method, system, device and medium for automatically generating code
Through the automatic code generation method, Restful API text is converted into Spring Boot code, which solves the problems of repeated work and error risks in Spring Boot development, achieves automatic code generation and rule consistency, and reduces maintenance costs.
Patent Information
- Application Number
- CN202210722649.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-06-24
- Publication Date
- 2024-10-15
- Estimated Expiration
- 2042-06-24
AI Technical Summary
In Spring Boot's RESTful API development, the existing technology has problems such as a lot of duplication of work, error risks, and maintenance difficulties. In particular, the request method naming rules written by different people in team development are different, resulting in higher maintenance costs in the future.
Through an automatic code generation method, parsers and builders are used to convert Restful API text files into Map objects, and Spring Boot code is generated based on the Map objects, including Controller, Service and POJO objects, simplifying the development process, improving efficiency and reducing errors. risks and implement unified code specifications.
It realizes the automatic generation of Spring Boot code, simplifies the development process, improves development efficiency, reduces error risks and maintenance costs, and ensures the consistency of code rules.
Smart Images

Figure CN115113856B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of code development technology, and in particular to a method, system, device and medium for automatically generating code. Background Art
[0002] The development method of separating the front-end and back-end has become the most popular server / client development method due to its advantages such as being applicable to multi-terminal displays and clear development responsibilities. RESTful API is the current best practice for separating the front-end and back-end. As the most commonly used framework in Java Web development, Spring Boot fully supports RESTful-style API interface development. In the current RESTful development of Spring Boot, usually the Controller, Service, and POJO objects are written first, and then the specific RESTful request path is specified for each method. This method involves a lot of repetitive work, is time-consuming, and also has a high risk of errors. In addition, the naming rules of the request methods written by different people in the team development are different, which is not convenient for later maintenance. Summary of the invention
[0003] In view of this, the present invention proposes a method, system, device and medium for automatic code generation, which realizes the automatic generation of Spring Boot code from RESTful API text files, greatly improves the development efficiency of developers and reduces the risk of errors. At the same time, this method can generate unified code specifications, facilitate developers' later code maintenance, and reduce maintenance costs.
[0004] Based on the above purpose, an embodiment of the present invention provides a method for automatically generating a code, which specifically includes the following steps:
[0005] Determine a target code, and write a parser and a builder based on the target code, wherein the target code includes a target object;
[0006] Read the text to be parsed and verify whether the text to be parsed is correct;
[0007] In response to the text to be parsed being correct, calling the parser to convert the file to be parsed into a Map object;
[0008] The builder is called to create a directory according to the Map object and generate a corresponding target object, and the corresponding target object is stored in the directory.
[0009] In some implementations, the target code is Spring Boot code, the target objects include Controller objects, Service objects, and POJO objects, and the text to be parsed is Restful API text.
[0010] In some implementations, calling the parser to convert the to-be-parsed file into a Map object includes:
[0011] The parser is called and based on it the following steps are performed:
[0012] Parse the first variable, the second variable, the third variable and the fourth variable in the Restful API text, and place the first variable, the second variable, the third variable and the fourth variable into the Map object, wherein the first variable is used to indicate the path where the parsing result is to be stored, the second variable is used to indicate the package path where the parsing result file is stored, the third variable is used to indicate the return value of the access result of the Controller object, and the fourth variable is used to indicate the part to be ignored when parsing the path part of the URL;
[0013] Parse the request block in the Restful API text, and determine the attributes of the POJO object, the Controller object, and the Service object in sequence based on the request block to place them in the Map object, wherein the attributes of the POJO object include the attribute name of the POJO object and its Java value type, the attributes of the Controller object include the name of the Controller object, the method name, the method mapping path, the method parameters, and the calling relationship between the method in the Controller object and the method of the Service object, and the attributes of the Service object include the name of the Service object, the method name, the method parameter list, and the return value.
[0014] In some implementations, calling a builder to create a directory according to the Map object and generating a corresponding target object and storing it in the corresponding directory includes:
[0015] The builder is called and based on it the following steps are performed:
[0016] Reading the first variable and the second variable from the Map object, and creating a directory based on the first variable and the second variable;
[0017] Reading a third variable from the Map object, creating an R object and a Java file corresponding to the R object based on the third variable, and placing the R object and the Java file corresponding to the R object into the directory;
[0018] Read the attribute values of the POJO object, the Service object, and the Controller object from the Map object respectively to create the POJO object, the Service object, the Controller object, and the Java files corresponding to the POJO object, the Service object, and the Controller object in sequence, and put the POJO object, the Service object, the Controller object, and the Java files corresponding to the POJO object, the Service object, and the Controller object into the directory.
[0019] In some embodiments, the method further comprises:
[0020] A fourth variable is read from the Map object, and a request path prefix of the POJO object is determined based on the fourth variable.
[0021] In some implementations, the attribute values of the POJO object, the Service object, and the Controller object are read from the Map object respectively to sequentially create the POJO object, the Service object, the Controller object, and Java files corresponding to the POJO object, the Service object, and the Controller object, including:
[0022] Read the attribute values of the POJO object, create the POJO object based on the attribute values of the POJO object and write it to the corresponding Java file;
[0023] Read the attribute values of the Service object, create a Service object and method based on the attribute values of the Service object, and write them to the corresponding Java file;
[0024] Read the attribute value of the Controller object, create a Controller object and method based on the attribute value of the Controller object, call the specified Service method in the Controller method, and write it to the corresponding Java file.
[0025] In some implementations, in response to the text to be parsed being correct, calling the parser to convert the file to be parsed into a Map object includes:
[0026] The text to be parsed is read in units of lines, and the file to be parsed is parsed according to preset conditions to convert the text to be parsed into the Map object line by line.
[0027] Another aspect of the embodiment of the present invention further provides a system for automatically generating codes, including:
[0028] A writing module, the writing module is configured to determine a target code and write a parser and a builder based on the target code, the target code including a target object;
[0029] A verification module, wherein the verification module is configured to read the text to be parsed and verify whether the text to be parsed is correct;
[0030] A conversion module, wherein the conversion module is configured to call the parser to convert the to-be-parsed file into a Map object in response to the to-be-parsed text being correct;
[0031] A generation module is configured to call the builder to create a directory and generate a corresponding target object according to the Map object, and store the corresponding target object in the directory.
[0032] According to another aspect of an embodiment of the present invention, a computer device is provided, comprising: at least one processor; and a memory, wherein the memory stores a computer program executable on the processor, and the computer program implements the steps of the above method when executed by the processor.
[0033] According to another aspect of the embodiments of the present invention, a computer-readable storage medium is provided, which stores a computer program that implements the above method steps when executed by a processor.
[0034] The present invention has at least the following beneficial technical effects: by determining the target code, and writing a parser and a builder based on the target code, wherein the target code includes a target object; reading the text to be parsed, and verifying whether the text to be parsed is correct; in response to the text to be parsed being correct, calling the parser to convert the file to be parsed into a Map object; calling the builder to create a directory and generate a corresponding target object according to the Map object, and storing the corresponding target object in the directory, the automatic generation of the target code is realized, the target code development process is simplified, and the target code development efficiency is improved. At the same time, since the builder is used to realize the unified construction of all methods, the rule consistency of the code is guaranteed, and the later code maintenance cost is reduced. BRIEF DESCRIPTION OF THE DRAWINGS
[0035] In order to more clearly illustrate the embodiments of the present invention or the technical solutions in the prior art, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, the drawings in the following description are only some embodiments of the present invention. For ordinary technicians in this field, other embodiments can be obtained based on these drawings without paying creative work.
[0036] Figure 1 A block diagram of an embodiment of the method for automatically generating codes provided by the present invention;
[0037] Figure 2 A schematic diagram of a process for converting a Restful API text into a Spring Boot code according to an embodiment of the present invention;
[0038] Figure 3 A schematic diagram of the structure of an embodiment of the file directory of the finally generated Spring Boot code provided by the present invention;
[0039] Figure 4 A schematic diagram of an embodiment of a system for automatically generating codes provided by the present invention;
[0040] Figure 5 A schematic diagram of the structure of an embodiment of a computer device provided by the present invention;
[0041] Figure 6 A schematic diagram of the structure of an embodiment of a computer-readable storage medium provided by the present invention. DETAILED DESCRIPTION
[0042] In order to make the objectives, technical solutions and advantages of the present invention more clearly understood, the embodiments of the present invention are further described in detail below in combination with specific embodiments and with reference to the accompanying drawings.
[0043] It should be noted that all expressions using "first" and "second" in the embodiments of the present invention are for distinguishing two non-identical entities with the same name or non-identical parameters. It can be seen that "first" and "second" are only for the convenience of expression and should not be understood as limitations on the embodiments of the present invention. The subsequent embodiments will not explain this one by one.
[0044] Based on the above purpose, the first aspect of the embodiment of the present invention provides an embodiment of a method for automatically generating a code. Figure 1 As shown, it includes the following steps:
[0045] S10, determining a target code, and writing a parser and a builder based on the target code, wherein the target code includes a target object;
[0046] S20, reading the text to be parsed, and verifying whether the text to be parsed is correct;
[0047] S30, in response to the text to be parsed being correct, calling the parser to convert the file to be parsed into a Map object;
[0048] S40, calling the builder to create a directory and generate a corresponding target object according to the Map object, and storing the corresponding target object in the directory.
[0049] In a specific embodiment, the text to be parsed is a Restful API text, the target code is a Spring Boot code, and the corresponding target objects include a Controller object, a Service object, and a POJO object.
[0050] Based on the specifications of Spring Boot code and Restful API text, use Python to write parsers and builders; before parsing the file to be parsed, first verify the correctness of the Restful API text. If the Restful API text is correct, it means that it can be parsed normally and continue to execute; on the basis that the Restful API text is correct, call the parser to parse the file based on the Restful API text into a Map object, and call the builder to finally convert the Map object into specific Spring Boot code, thereby simplifying the Spring Boot code development process and improving the efficiency of Spring Boot code development. At the same time, since the builder is used to implement the unified construction of all methods, the consistency of code rules is guaranteed, and the cost of later code maintenance is also reduced.
[0051] In another specific embodiment, Figure 2 The following is a flowchart of converting Restful API text into Spring Boot code, which includes the following steps:
[0052] Front-end and back-end personnel jointly negotiate to generate a consistent RESTful API interface that complies with the specifications;
[0053] Backend developers write Restful API text based on the Restful API interface and call the parser and builder written in Python to convert the text into Spring Boot code, including:
[0054] Read the Restful API text and verify its correctness;
[0055] Call the parser to convert the text into a Map object description, including:
[0056] Parse the baseDir, basePackage, result, prefix and other variable values into a Map object, where baseDir indicates the path where the parsing result is to be stored, basePackage indicates the package path where the parsing result file is stored, result indicates the return value of the Controller object access result, and prefix indicates the part to be ignored when parsing the path part of the URL;
[0057] Parse the request blocks of POST, PUT, GET, and DELETE, and determine the attribute names and Java value types of POJOs based on JSON data and request parameters;
[0058] Determine the Controller object name, method name, method mapping path, method parameters, etc. through the request block.
[0059] The Service object name, method name, method parameter list, return value, etc. are determined through the request block and parameter values.
[0060] Determine the calling relationship between the methods in the Controller and the methods in the Service.
[0061] Put the attributes and attribute values of the POJO object, Controller object, and Service object parsed above into the Map object in turn, thereby completing the parsing of the Restful API text;
[0062] The builder creates the specified directory based on the Map object and generates Controller, Service, and POJO objects, including:
[0063] Read the values of variables such as baseDir and basePackage to determine whether the directory of the Spring Boot code exists. If not, create a non-existent directory based on the values of variables such as baseDir and basePackage.
[0064] Read the result variable value to determine whether the R object in the Spring Boot code exists. If the R object does not exist, create the R object and the corresponding Java file.
[0065] Read pojo attribute values, create POJO objects in sequence, and write them to specific Java files;
[0066] Read the service attribute value, create the Service object and internal methods, and write them to the specific Java file. This process should ensure that the POJO object has been created successfully to prevent failure when importing the POJO object into the Service object;
[0067] Read the controller attribute value, create the Controller object and internal methods, and introduce the Service object. Call the specified Service method in the Controller method and write it to the specific Java file. This process should ensure that the POJO and Service objects have been created successfully to prevent failure when importing POJO and Service objects into the Controller object.
[0068] Through the above process, the automatic generation of Restful API text to Spring Boot code is completed, which simplifies the Spring Boot code development process and improves the efficiency of Spring Boot code development. At the same time, since the builder is used to achieve unified construction of all methods, the consistency of code rules is guaranteed and the cost of later code maintenance is reduced.
[0069] In some implementations, the target code is Spring Boot code, the target objects include Controller objects, Service objects, and POJO objects, and the text to be parsed is Restful API text.
[0070] The present invention uses Spring Boot and Lombok as basic dependency packages, generates a specified directory structure of Spring Boot code by parsing RESTful API files, and puts the generated Controller files, Service files, and POJO files into corresponding directories.
[0071] In a specific embodiment, the file format of a RESTful API text is described.
[0072] Developers write RESTful API text that complies with the specification, and write some variables in the text as fixed variables of the text. For example, write 4 variables as fixed variables, namely the first variable, the second variable, the third variable, and the fourth variable. The format of the RESTful API text is as follows:
[0073]
[0074]
[0075] Define the first variable as @baseDir. @baseDir indicates the path where the parsing results are to be stored. If not specified, the results are stored in the path where the current text file is located.
[0076] Define the second variable as @basePackage, where @basePackage represents the package path where the parsing result file is stored.
[0077] Define the third variable as @result. @result represents the return value of the Controller access result. If not specified, null is returned.
[0078] Define the fourth variable as @prefix, where @prefix represents the part that is ignored when parsing the path part of the URL, so that only valid paths are parsed.
[0079] @baseDir, @basePackage, @result, and @prefix are fixed variable names and cannot be used to specify common variable values. Parsing methods include POST, PUT, GET, and DELETE. When passing parameters in HTTP requests, use the application / json format.
[0080] It should be noted that the RESTful API text is not in the standard RESTful API file format. Instead, it is based on the standard RESTful API file format. In order to be more suitable for the development environment, a unified specification is jointly developed by front-end and back-end developers. The RESTful API text is formed based on this unified specification. For example, the {id} part is used to obtain the meaning of the variable content in the path. When making an actual request, you only need to replace {id} with a specific value.
[0081] In some implementations, calling the parser to convert the to-be-parsed file into a Map object includes:
[0082] The parser is called and based on it the following steps are performed:
[0083] Parse the first variable, the second variable, the third variable and the fourth variable in the Restful API text, and place the first variable, the second variable, the third variable and the fourth variable into the Map object, wherein the first variable is used to indicate the path where the parsing result is to be stored, the second variable is used to indicate the package path where the parsing result file is stored, the third variable is used to indicate the return value of the access result of the Controller object, and the fourth variable is used to indicate the part to be ignored when parsing the path part of the URL;
[0084] Parse the request block in the Restful API text, and determine the attributes of the POJO object, the Controller object, and the Service object in sequence based on the request block to place them in the Map object, wherein the attributes of the POJO object include the attribute name of the POJO object and its Java value type, the attributes of the Controller object include the name of the Controller object, the method name, the method mapping path, the method parameters, and the calling relationship between the method in the Controller object and the method of the Service object, and the attributes of the Service object include the name of the Service object, the method name, the method parameter list, and the return value.
[0085] In a specific embodiment, the specific process of converting the Restful API text into a Map object is described.
[0086] S2.1 reads the RESTful API text and verifies whether it can be parsed correctly. If the parsing fails, the parsing is stopped and the error cause is specified. If the parsing is normal, the execution continues.
[0087] S2.2 calls the parser to save the successfully parsed data structure as a Map object, and finally hands the object to the builder for directory construction and file construction.
[0088] The specific process of calling the parser to parse the RESTful API text is as follows:
[0089] 1) Read the text file line by line. When a variable starting with @ is parsed, a key with the same name is created in the Map object and the variable value is saved under the corresponding key value. If there are multiple variables with the same name, the new value will overwrite the old value, and only the last value will be valid.
[0090] 2) When reading the line starting with the POST, PUT, GET, or DELETE method, it will continue reading to the next ### comment position and parse these lines together, that is, the content between the two ### is the request block.
[0091] 3) By parsing the PATH part of the URL, the object path and object name (POJO name, such as book) to be processed are extracted from the path, and used as the key value of the Map to store all related controller, service, mapping and other information.
[0092] 4) Obtain the attribute information of the book object through the JSON part in the POST or PUT request and the QUERY or FRAGMENT part in the GET request, and save it in fields (file). At the same time, determine the name and package information of the object, and determine the field type (such as String, Long, etc.) during construction based on the attribute value.
[0093] 5) The controller in the Map object is used to save the methods in the Controller object. The method is derived from the parsing of POST, PUT, GET, and DELETE methods, and is converted into different method names according to the method type (such as POST is converted into addBookXxx, PUT is converted into updateBook, GET is converted into getBookXxx, and DELETE is converted into deleteBookXxx), and records the request method type (method), mapping address (mapping), parameter list (params), and corresponding service method name (service) called by the controller method.
[0094] 6) The service attribute in the Map object is used to save the methods in the Service object. The parsing process is similar to that in 5), but the return value type needs to be determined for each method. Usually, the add / update / get method returns a POJO object or an object list, and the delete method returns a Boolean value.
[0095] 7) If PATH contains {} package content (such as {id}), it means that the content is variable content in the request path, and you need to add the annotation PathVariable to the constructed method receiving parameters.
[0096] 8) Repeat steps 2)-7) to eventually generate a specific Map object.
[0097] After successful parsing, the Map object will be handed over to the builder for directory creation and file generation. The format of the Map object generated after successful parsing is as follows:
[0098]
[0099]
[0100]
[0101] In some implementations, calling a builder to create a directory according to the Map object and generating a corresponding target object and storing it in the corresponding directory includes:
[0102] The builder is called and based on it the following steps are performed:
[0103] Reading the first variable and the second variable from the Map object, and creating a directory based on the first variable and the second variable;
[0104] Reading a third variable from the Map object, creating an R object and a Java file corresponding to the R object based on the third variable, and placing the R object and the Java file corresponding to the R object into the directory;
[0105] Read the attribute values of the POJO object, the Service object, and the Controller object from the Map object respectively to create the POJO object, the Service object, the Controller object, and the Java files corresponding to the POJO object, the Service object, and the Controller object in sequence, and put the POJO object, the Service object, the Controller object, and the Java files corresponding to the POJO object, the Service object, and the Controller object into the directory.
[0106] In some embodiments, the method further comprises:
[0107] A fourth variable is read from the Map object, and a request path prefix of the POJO object is determined based on the fourth variable.
[0108] In some implementations, the attribute values of the POJO object, the Service object, and the Controller object are read from the Map object respectively to sequentially create the POJO object, the Service object, the Controller object, and Java files corresponding to the POJO object, the Service object, and the Controller object, including:
[0109] Read the attribute values of the POJO object, create the POJO object based on the attribute values of the POJO object and write it to the corresponding Java file;
[0110] Read the attribute values of the Service object, create a Service object and method based on the attribute values of the Service object, and write them to the corresponding Java file;
[0111] Read the attribute value of the Controller object, create a Controller object and method based on the attribute value of the Controller object, call the specified Service method in the Controller method, and write it to the corresponding Java file.
[0112] In a specific embodiment, the specific process of calling a builder to generate Spring Boot code based on a Map object is described.
[0113] S2.3 calls the builder to read the Map object and create the specified directory and Controller, Service, POJO, and result object files.
[0114] The specific process of calling the builder to generate Spring Boot code is as follows:
[0115] 1) Determine the root directory address of the created directory through the baseDir value. If the root directory does not exist, create it.
[0116] 2) Determine the root address of the project package through basePackage and finally resolve it to a directory. If the directory does not exist, create it.
[0117] 3) Determine the request path prefix of book through prefix, and use it for request path mapping in method annotations later (such as @DeleteMapping(" / {id}"), etc.).
[0118] 4) Determine the return value of the method in the Controller through result. If the R object does not exist, generate the R object and specific Java file through the specified template.
[0119] 5) Build POJO objects and specific Java files through the pojo values in the Map object. Use Lombok to simplify the getter / setter methods of the object during the construction process. The generated POJO file format is as follows:
[0120]
[0121]
[0122] 6) Construct the Service and specific Java files through the service value in the Map object, that is, construct the Service interface and the specific implementation object of the interface in sequence. The Service object can only be created after the POJO object is successfully created to prevent the failure of referencing the POJO object in the Service object. The format of the generated Service and its interface files is as follows:
[0123]
[0124]
[0125] 7) Use the controller value in the Map object to build the Controller object and the specific Java file. The Controller object can only be created after the Service object is successfully created to prevent the failure of referencing the Service object in the Controller object. The format of the generated Controller file is as follows:
[0126]
[0127] 8) When the Map object is built, the resulting file directory structure is as follows Figure 3 shown.
[0128] The file directory generated by the above process and the corresponding POJO files, Service files, and Controller files in the directory complete the generation of Spring Boot code.
[0129] Through the above process, the automatic generation of Restful API text to Spring Boot code is completed, which simplifies the Spring Boot code development process and improves the efficiency of Spring Boot code development. At the same time, since the builder is used to achieve unified construction of all methods, the consistency of code rules is guaranteed and the cost of later code maintenance is reduced.
[0130] In some implementations, in response to the text to be parsed being correct, calling the parser to convert the file to be parsed into a Map object includes:
[0131] The text to be parsed is read in units of lines, and the file to be parsed is parsed according to preset conditions to convert the text to be parsed into the Map object line by line.
[0132] Based on the same inventive concept, according to another aspect of the present invention, Figure 4 As shown, an embodiment of the present invention further provides a system for automatically generating codes, comprising:
[0133] A writing module 110, wherein the writing module 110 is configured to determine a target code and write a parser and a builder based on the target code, wherein the target code includes a target object;
[0134] A verification module 120, wherein the verification module 120 is configured to read the text to be parsed and verify whether the text to be parsed is correct;
[0135] A conversion module 130, wherein the conversion module 130 is configured to call the parser to convert the file to be parsed into a Map object in response to the text to be parsed being correct;
[0136] The generating module 140 is configured to call the builder to create a directory according to the Map object and generate a corresponding target object, and store the corresponding target object in the directory.
[0137] Based on the same inventive concept, according to another aspect of the present invention, Figure 5 As shown, an embodiment of the present invention further provides a computer device 30, which includes a processor 310 and a memory 320. The memory 320 stores a computer program 321 that can be run on the processor. When the processor 310 executes the program, the steps of the above method are performed.
[0138] Among them, the memory, as a non-volatile computer-readable storage medium, can be used to store non-volatile software programs, non-volatile computer executable programs and modules, such as the program instructions / modules corresponding to the automatic code generation method in the embodiment of the present application. The processor executes various functional applications and data processing of the device by running the non-volatile software programs, instructions and modules stored in the memory, that is, the automatic code generation method of the above method embodiment is implemented.
[0139] The memory may include a program storage area and a data storage area, wherein the program storage area may store an operating system, an application required for at least one function; the data storage area may store data created according to the use of the device, etc. In addition, the memory may include a high-speed random access memory, and may also include a non-volatile memory, such as at least one disk storage device, a flash memory device, or other non-volatile solid-state storage device. In some embodiments, the memory may optionally include a memory remotely arranged relative to the processor, and these remote memories may be connected to the local module via a network. Examples of the above-mentioned network include, but are not limited to, the Internet, an intranet, a local area network, a mobile communication network, and combinations thereof.
[0140] Based on the same inventive concept, according to another aspect of the present invention, Figure 6 As shown, an embodiment of the present invention further provides a computer-readable storage medium 40, which stores a computer program 410 for executing the above method when executed by a processor.
[0141] Finally, it should be noted that a person of ordinary skill in the art can understand that all or part of the processes in the above-mentioned embodiments can be implemented by instructing the relevant hardware through a computer program, and the program can be stored in a computer-readable storage medium. When the program is executed, it can include the processes of the embodiments of the above-mentioned methods. Among them, the storage medium of the program can be a disk, an optical disk, a read-only storage memory (ROM) or a random access memory (RAM), etc. The above-mentioned computer program embodiments can achieve the same or similar effects as the corresponding above-mentioned arbitrary method embodiments.
[0142] It will also be appreciated by those skilled in the art that various exemplary logic blocks, modules, circuits and algorithm steps described in conjunction with the disclosure herein can be implemented as electronic hardware, computer software or a combination of the two. In order to clearly illustrate this interchangeability of hardware and software, a general description has been given to the functions of various schematic components, blocks, modules, circuits and steps. Whether this function is implemented as software or hardware depends on specific applications and the design constraints imposed on the entire system. Those skilled in the art can implement the function in various ways for each specific application, but this implementation decision should not be interpreted as causing a departure from the disclosed scope of the embodiments of the present invention.
[0143] The above are exemplary embodiments disclosed in the present invention, but it should be noted that various changes and modifications may be made without departing from the scope of the embodiments disclosed in the present invention as defined in the claims. The functions, steps and / or actions of the method claims according to the disclosed embodiments described herein do not need to be performed in any particular order. The serial numbers of the embodiments disclosed in the above embodiments of the present invention are for description only and do not represent the advantages and disadvantages of the embodiments. In addition, although the elements disclosed in the embodiments of the present invention may be described or required in individual form, they may also be understood as multiple unless explicitly limited to the singular.
[0144] It should be understood that, as used herein, the singular forms "a", "an" are intended to include the plural forms as well, unless the context clearly supports an exception. It should also be understood that, as used herein, "and / or" refers to any and all possible combinations including one or more of the associated listed items.
[0145] A person skilled in the art should understand that the discussion of any of the above embodiments is only exemplary and is not intended to imply that the scope of the disclosure of the embodiments of the present invention (including the claims) is limited to these examples; under the concept of the embodiments of the present invention, the technical features in the above embodiments or different embodiments can also be combined, and there are many other changes in different aspects of the embodiments of the present invention as above, which are not provided in detail for the sake of simplicity. Therefore, any omissions, modifications, equivalent substitutions, improvements, etc. made within the spirit and principles of the embodiments of the present invention should be included in the protection scope of the embodiments of the present invention.
Claims
1. A method for automatically generating code, It is characterized in that include: Determine a target code, and write a parser and a builder based on the target code, wherein the target code includes a target object; Read the text to be parsed and verify whether the text to be parsed is correct; In response to the text to be parsed being correct, calling the parser to convert the text to be parsed into a Map object; Calling the builder to create a directory and generate a corresponding target object according to the Map object, and storing the corresponding target object in the directory; Calling the parser to convert the text to be parsed into a Map object includes: The parser is called and based on it the following steps are performed: Parse the first variable, the second variable, the third variable and the fourth variable in the Restful API text, and place the first variable, the second variable, the third variable and the fourth variable into the Map object, wherein the first variable is used to indicate the path where the parsing result is to be stored, the second variable is used to indicate the package path where the parsing result file is stored, the third variable is used to indicate the return value of the Controller object access result, and the fourth variable is used to indicate the part to be ignored when parsing the path part of the URL; Parse the request block in the Restful API text, and determine the attributes of the POJO object, the Controller object, and the Service object in sequence based on the request block to place them in the Map object, wherein the attributes of the POJO object include the attribute name of the POJO object and its Java value type, the attributes of the Controller object include the name of the Controller object, the method name, the method mapping path, the method parameters, and the calling relationship between the method in the Controller object and the method of the Service object, and the attributes of the Service object include the name of the Service object, the method name, the method parameter list, and the return value; Use the builder to create a directory based on the Map object and generate the corresponding target object and store it in the corresponding directory, including: The builder is called and based on it the following steps are performed: Reading the first variable and the second variable from the Map object, and creating a directory based on the first variable and the second variable; Reading a third variable from the Map object, creating an R object and a Java file corresponding to the R object based on the third variable, and placing the R object and the Java file corresponding to the R object into the directory; Read the attribute values of the POJO object, the Service object, and the Controller object from the Map object respectively to create the POJO object, the Service object, the Controller object, and the Java files corresponding to the POJO object, the Service object, and the Controller object in sequence, and put the POJO object, the Service object, the Controller object, and the Java files corresponding to the POJO object, the Service object, and the Controller object into the directory.
2. The method according to claim 1, It is characterized in that The target code is Spring Boot code, the target objects include Controller objects, Service objects and POJO objects, and the text to be parsed is Restful API text.
3. The method according to claim 1, It is characterized in that Further including: A fourth variable is read from the Map object, and a request path prefix of the POJO object is determined based on the fourth variable.
4. The method according to claim 1, It is characterized in that Reading the attribute values of the POJO object, the Service object, and the Controller object from the Map object respectively to sequentially create the POJO object, the Service object, the Controller object, and the Java files corresponding to the POJO object, the Service object, and the Controller object, including: Read the attribute values of the POJO object, create the POJO object based on the attribute values of the POJO object and write it to the corresponding Java file; Read the attribute values of the Service object, create a Service object and method based on the attribute values of the Service object, and write them to the corresponding Java file; Read the attribute value of the Controller object, create a Controller object and method based on the attribute value of the Controller object, call the specified Service method in the Controller method, and write it to the corresponding Java file.
5. The method according to claim 1, It is characterized in that In response to the text to be parsed being correct, calling the parser to convert the text to be parsed into a Map object, including: The text to be parsed is read in units of lines, and the text to be parsed is parsed according to preset conditions to convert the text to be parsed into the Map object line by line.
6. A system for automatically generating codes, It is characterized in that include: A writing module, the writing module is configured to determine a target code and write a parser and a builder based on the target code, the target code including a target object; A verification module, wherein the verification module is configured to read the text to be parsed and verify whether the text to be parsed is correct; A conversion module, wherein the conversion module is configured to call the parser to convert the text to be parsed into a Map object in response to the text to be parsed being correct; A generation module, wherein the generation module is configured to call the builder to create a directory according to the Map object and generate a corresponding target object, and store the corresponding target object in the directory; The process of calling the parser to convert the text to be parsed into a Map object includes: The parser is called and based on it the following steps are performed: Parse the first variable, the second variable, the third variable and the fourth variable in the Restful API text, and place the first variable, the second variable, the third variable and the fourth variable into the Map object, wherein the first variable is used to indicate the path where the parsing result is to be stored, the second variable is used to indicate the package path where the parsing result file is stored, the third variable is used to indicate the return value of the Controller object access result, and the fourth variable is used to indicate the part to be ignored when parsing the path part of the URL; Parse the request block in the Restful API text, and determine the attributes of the POJO object, the Controller object, and the Service object in sequence based on the request block to place them in the Map object, wherein the attributes of the POJO object include the attribute name of the POJO object and its Java value type, the attributes of the Controller object include the name of the Controller object, the method name, the method mapping path, the method parameters, and the calling relationship between the method in the Controller object and the method of the Service object, and the attributes of the Service object include the name of the Service object, the method name, the method parameter list, and the return value; Use the builder to create a directory based on the Map object and generate the corresponding target object and store it in the corresponding directory, including: The builder is called and based on it the following steps are performed: Reading the first variable and the second variable from the Map object, and creating a directory based on the first variable and the second variable; Reading a third variable from the Map object, creating an R object and a Java file corresponding to the R object based on the third variable, and placing the R object and the Java file corresponding to the R object into the directory; Read the attribute values of the POJO object, the Service object, and the Controller object from the Map object respectively to create the POJO object, the Service object, the Controller object, and the Java files corresponding to the POJO object, the Service object, and the Controller object in sequence, and put the POJO object, the Service object, the Controller object, and the Java files corresponding to the POJO object, the Service object, and the Controller object into the directory.
7. A computer device, include: at least one processor; as well as A memory storing a computer program executable on the processor, wherein the processor executes the steps of the method according to any one of claims 1 to 5 when executing the program.
8. A computer-readable storage medium storing a computer program. It is characterized in that When the computer program is executed by a processor, the steps of the method according to any one of claims 1 to 5 are performed.
