Interface definition acquisition method, electronic equipment and program product
By determining the interface file from the target code file and obtaining relevant information for assembly, the problem of manual addition of interface definitions is solved and the invasiveness of automatic generation of tools is achieved, and efficient, code-free intrusive interface definition acquisition is achieved.
Patent Information
- Application Number
- CN202510369226.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-03-26
- Publication Date
- 2025-07-25
AI Technical Summary
In the prior art, manually adding interface definitions is labor-intensive and prone to errors, while automatic generation tools have code intrusive problems, increasing code complexity and maintenance difficulty.
By determining the interface file from the target code file based on the interface file identification rules, obtaining the interface name, description, request information and return information, and assembling it to obtain the interface definition without adding annotations to the code.
It realizes the invasive acquisition of interface definitions without code, reduces maintenance costs, improves analysis efficiency, and avoids human errors and code complexity.
Smart Images

Figure CN120371378A_ABST
Abstract
Description
Technical Field
[0001] The present disclosure relates to the technical fields of computers and the like, and particularly relates to a method for obtaining interface definitions, an electronic device, a storage medium, and a program product. Background Art
[0002] In the field of modern software development, with the wide application of microservice architectures and distributed systems, communication between services has become increasingly frequent and complex. As a key link for communication between different services, accurate and complete interface definitions are crucial for efficient and accurate data interaction between various services. Clear and accurate interface definitions are of great significance to the work of developers, testers, and operation and maintenance personnel.
[0003] Currently, there are usually two ways to obtain interface definitions: manual addition and automatic generation; manual addition requires relevant staff to sort out and record the detailed information of each interface one by one, which not only consumes a large amount of human resources but also is extremely prone to human errors. To solve the drawbacks of manual addition, automatic generation tools such as Swagger, Apipost, Postman, etc. have emerged. These tools can automatically extract interface definitions from the code by adding specific annotations or configurations in the code.
[0004] However, since automatic generation tools need to add specific annotations or configurations in the code, that is, they need to invade the code to obtain interface definitions, this method has the problem of code invasiveness, which affects the original code structure and increases the complexity and maintenance difficulty of the code. Summary of the Invention
[0005] The present disclosure provides a method for obtaining interface definitions, an electronic device, a storage medium, and a program product.
[0006] According to one aspect of the present disclosure, a method for obtaining interface definitions is provided, including: Determining an interface file from a target code file based on an interface file identification rule; Obtaining an interface name, an interface description, interface request information, and interface return information based on the interface file; Assembling the interface name, the interface description, the interface request information, and the interface return information to obtain an interface definition.
[0007] According to the method for obtaining interface definitions according to at least one embodiment of the present disclosure, the determining an interface file from a target code file based on an interface file identification rule includes: Obtaining the file names of multiple sub-files included in the target code file; Determine multiple conventionally named files from the multiple sub-files whose file names conform to the file naming rule in the interface file identification rule; Obtain the annotations included in the multiple conventionally named files; Determine interface files from the multiple conventionally named files whose annotations conform to the interface file annotation rule in the interface file identification rule.
[0008] According to the interface definition obtaining method of at least one embodiment of the present disclosure, the obtaining of the interface name, interface description, interface request information, and interface return information based on the interface file includes: Determine whether the interface file includes annotation information related to the interface; If it includes, determine the interface description according to the annotation information; If it does not include, obtain the annotation of the method of the interface from the interface file; Determine whether the annotation of the method of the interface includes an interface operation annotation; If the annotation of the method of the interface includes an interface operation annotation, determine the interface description according to the interface operation annotation; If the annotation of the method of the interface does not include an interface operation annotation, assign the interface description as empty.
[0009] According to the interface definition obtaining method of at least one embodiment of the present disclosure, the obtaining of the interface name, interface description, interface request information, and interface return information based on the interface file includes: Obtain the annotation related to the interface from the interface file; Determine whether the annotation related to the interface includes a request method annotation; If it includes, determine the request method of the interface according to the request method annotation, and the request method is part of the interface request information; If it does not include, assign the request method of the interface as empty.
[0010] According to the interface definition obtaining method of at least one embodiment of the present disclosure, the obtaining of the interface name, interface description, interface request information, and interface return information based on the interface file includes: Obtain the method of the interface from the interface file; Obtain the parameters of the method of the interface; For the parameters of the method of the interface with annotations, obtain the annotation type of the annotation and parse the parameters of the method of the interface according to the annotation type to obtain the interface request information other than the request method of the interface; For the parameters of the method of the interface without annotations, obtain the request method of the interface and parse the parameters of the method of the interface according to the request method to obtain the interface request information other than the request method of the interface.
[0011] An interface definition acquisition method according to at least one embodiment of the present disclosure, the method for parsing the parameters of the interface according to the request method, obtaining interface request information other than the request method of the interface, including: Obtain the parsing method related to the request method; Use the parsing method to parse the parameters of the interface method, and obtain interface request information other than the request method and parameter type of the interface; Obtain the parameter passing method and data type of the parameters of the interface; Analyze the parameter passing method and data type to obtain the parameter type of the interface.
[0012] An interface definition acquisition method according to at least one embodiment of the present disclosure, the method for parsing the parameters of the interface according to the annotation type, including: When the annotation type is the first type, obtain the parsing method related to the annotation type; parse the parameters of the interface method according to the parsing method related to the annotation type; When the annotation type is the second type, obtain the class name of the parameters of the interface method, and parse the parameters of the interface method according to the class name.
[0013] An interface definition acquisition method according to at least one embodiment of the present disclosure, the method for obtaining an interface name, an interface description, interface request information, and interface return information based on the interface file, including: Obtain the return object type of the interface from the interface file; Determine the return type of the interface according to the return object type of the interface, and the return type is a part of the interface return information.
[0014] According to another aspect of the present disclosure, there is provided an electronic device, including: a memory storing execution instructions; and a processor that executes the execution instructions stored in the memory, so that the processor executes the interface definition acquisition method of any one of the embodiments of the present disclosure.
[0015] According to still another aspect of the present disclosure, there is provided a computer program product, including a computer program, which when executed by a processor implements the interface definition acquisition method of any one of the embodiments of the present disclosure. BRIEF DESCRIPTION OF THE DRAWINGS
[0016] The drawings illustrate exemplary embodiments of the present disclosure and, together with the description, are used to explain the principles of the present disclosure. These drawings are included to provide a further understanding of the present disclosure and are included in this specification and form a part of this specification.
[0017] Figure 1 It is a flowchart of an interface definition acquisition method according to an embodiment of the present disclosure.
[0018] Figure 2 It is Figure 1 a flowchart of a file determination method in the interface definition acquisition method shown.
[0019] Figure 3 It is Figure 1 a flowchart of an information acquisition method in the interface definition acquisition method shown. Figure 1 .
[0020] Figure 4 It is Figure 1 a flowchart of an information acquisition method in the interface definition acquisition method shown. Figure 2 .
[0021] Figure 5 It is Figure 4 a flowchart of a method acquisition method in the information acquisition method shown.
[0022] Figure 6 It is Figure 1 a flowchart of an information acquisition method in the interface definition acquisition method shown. Figure 3 .
[0023] Figure 7 It is Figure 6 a flowchart of an annotation-free analysis method in the information acquisition method shown.
[0024] Figure 8 It is Figure 6 a flowchart of an annotation analysis method in the information acquisition method shown.
[0025] Figure 9 It is Figure 1 a flowchart of an information acquisition method in the interface definition acquisition method shown. Figure 4 .
[0026] Figure 10 It is Figure 9 a flowchart of a type determination method in the information acquisition method shown.
[0027] Figure 11 It is a schematic flowchart of an interface definition acquisition method according to an embodiment of the present disclosure.
[0028] Figure 12 It is a schematic block diagram of the structure of an interface definition acquisition device according to an embodiment of the present disclosure.
[0029] Figure 13 It is a schematic block diagram of the structure of an electronic device according to an embodiment of the present disclosure. Specific embodiments
[0030] The present disclosure will be further described in detail below with reference to the accompanying drawings and examples. It can be understood that the specific examples described herein are only used to explain the relevant content and do not limit the present disclosure. In addition, it should be noted that for the convenience of description, only the parts related to the present disclosure are shown in the drawings.
[0031] It should be noted that, without conflict, the embodiments in the present disclosure and the features in the embodiments can be combined with each other. The technical solutions of the present disclosure will be described in detail below with reference to the drawings and in combination with the embodiments.
[0032] Taking an e-commerce system development project as an example, the system includes multiple microservices such as user service, product service, and order service. In the user service, there are interfaces such as obtaining user information and modifying user passwords; in the product service, there are interfaces such as querying the product list and obtaining product details; in the order service, there are interfaces such as creating an order and querying the order status.
[0033] In the initial stage of the project, due to the insufficient understanding of the importance of interface definition by the staff, the method of manually adding interface definitions was adopted. Several staff members spent a lot of time sorting out the interface definitions of each interface; during the sorting process, many errors occurred. For example, when recording the return data structure of the query product list interface, the product inventory field was omitted, resulting in incomplete product information obtained by the testers during interface testing, which affected the entire testing progress. Moreover, as the project progresses, the business requirements change continuously, and the interfaces also change frequently. Each time an interface changes, the interface definition needs to be sorted out and modified again, which not only consumes a lot of manpower and has low efficiency, but also increases the possibility of errors.
[0034] To solve the problem of manual addition, the staff introduced the Swagger automatic generation tool. This automatic generation tool needs to add various annotations and configurations in the code. For example, add the @ApiOperation annotation to describe the interface function on the method of the obtain product details interface. However, this method has the problem of code intrusion, and these annotations not only increase the complexity of the code, but also may affect the maintainability of the code. Especially when dealing with team collaboration, different staff members may use the annotations inconsistently, resulting in unstable interface definitions and even errors.
[0035] Therefore, the present disclosure proposes a method for obtaining interface definitions, an electronic device, a storage medium, and a program product. For small-scale code analysis, the present disclosure can be implemented by code analysis software installed on electronic devices such as computers; for large-scale code analysis, the present disclosure can be implemented by code analysis software installed on electronic devices such as servers.
[0036] For the convenience of description and to make the technical solutions of the specific embodiments of the present disclosure easier to understand, the technical terms related to the present disclosure are explained as follows: An interface definition is a clear description of the interaction rules between different systems, modules, or components.
[0037] An interface file is a document or code file used to record and describe interface-related information.
[0038] An interface operation annotation refers to an annotation used to identify operations related to an interface.
[0039] Figure 1 The overall flowchart of the interface definition acquisition method M100 according to an embodiment of the present disclosure is shown. As Figure 1 The shown interface definition acquisition method includes steps S110 to S130. Among them, the interface definition acquisition method can be executed by an electronic device such as a computer or a server.
[0040] Specifically, Figure 1 The shown interface definition acquisition method includes: Step S110, determining an interface file from a target code file based on an interface file identification rule.
[0041] In some embodiments of the present disclosure, the target code file in step S110 is usually a backend application code file, such as a code file with annotations in backend frameworks such as Java, Python, or Node.js.
[0042] The process of determining an interface file from a target code file through step S110 may include: judging whether the sub-files included in the target code file are interface files according to the preset interface file identification rules such as content features, naming specifications, or file structures, and obtaining a judgment result; then determining the interface file from the target code file according to the judgment result.
[0043] Step S120, obtaining an interface name, an interface description, interface request information, and interface return information based on the interface file.
[0044] In some embodiments of the present disclosure, the interface name, the interface description, the interface request information, and the interface return information in step S120 are key contents for clarifying the interface function, usage method, input / output requirements, and related constraints.
[0045] Among them, the interface name and interface description are the identification and functional description of the interface itself; the interface request information describes how to call the interface, that is, by what means to send a request, including various parameters and data in the request, such as the request method, request path, request parameters, request headers, request body, request body type, request body form, etc.; the interface return information describes the data format, content and status code of the interface response, which may include status code, return headers, return body, return type, return parameters, etc.
[0046] For any interface described in the interface file, obtaining the interface name, interface description, interface request information and interface return information through step S120 may specifically include: determining the associated interface file of the interface from multiple sub-files included in the interface file; analyzing the associated interface file of the interface to obtain the interface name, interface description, interface request information and interface return information of the interface. Among them, the associated interface file of the interface is the file that describes the relevant information of the interface in the content.
[0047] Step S130, assemble the interface name, interface description, interface request information and interface return information to obtain an interface definition.
[0048] In some embodiments of the present disclosure, step S130 may assemble the interface name, interface description, interface request information and interface return information based on assembly rules, interface definition templates, etc., so that the interface name, interface description, interface request information and interface return information are organized according to a certain structure.
[0049] After obtaining the interface name, interface description, interface request information and interface return information of all interfaces at one time through step S120, the interface name, interface description, interface request information and interface return information of each interface can be assembled one by one through step S130.
[0050] In particular, step S120 may be to obtain the interface name, interface description, interface request information and interface return information of a certain interface based on the interface file and then assemble through step S130, and repeat the above process until all interfaces in all interface files are processed. This process can avoid the complexity brought by analyzing all interface names, interface descriptions, interface request information and interface return information at one time and improve the analysis efficiency.
[0051] The interface definition acquisition method provided by the present disclosure can deduce the interface definition by directly analyzing the interface file, without the need to manually add annotations, without invading the original code, and reducing the maintenance cost. This interface definition acquisition method solves the problem in the prior art that since the automatic generation tool needs to add specific annotations or configurations to the code, that is, it needs to invade the code to obtain the interface definition. Therefore, this method has the problem of code invasiveness, which affects the original code structure, increases the complexity of the code and the difficulty of maintenance.
[0052] Regarding step S110, in some embodiments of the present disclosure, it may include steps S111 to S114 as Figure 2 shown.
[0053] Step S111, obtaining the file names of multiple sub-files included in the target code file.
[0054] In some embodiments of the present disclosure, the file names obtained through step S111 may be composed of two parts: the main file name and the file extension; among them, the main file name is the main identification part of the sub-file, which is used to describe the content, purpose or theme of the sub-file, facilitating the distinction and identification of different sub-files; the file extension is the part following the main file name in the file name, separated by a dot (.), usually consisting of several characters, and it is used to identify the type or format of the file.
[0055] Step S112, determining multiple conventionally named files from the multiple sub-files whose file names conform to the file naming rule in the interface file recognition rule.
[0056] In some embodiments of the present disclosure, the file naming rule in step S112 may be the rule in the interface file recognition rule preset by the user. This file naming rule may be fixed or may be modified in real time as needed. In order to simply, quickly and efficiently determine multiple conventionally named files, this file naming rule may specifically be a file naming rule set based on the file extension.
[0057] Specifically, taking the target code file in Java format as an example, the file naming rule in step S112 may be to accept all files with the extension.java. The process of determining multiple conventionally named files through step S112 may include: judging one by one whether the file name of each sub-file conforms to the file naming rule to obtain a judgment result; and taking the sub-files that conform to the file naming rule as multiple conventionally named files according to the judgment result.
[0058] Step S113, obtaining the annotations included in the multiple conventionally named files.
[0059] In some embodiments of the present disclosure, for any conventionally named file, the annotations included in the conventionally named file generally provide additional information for code elements (such as classes, methods, fields, etc.), but do not directly affect the logical operation of the code; the maintainability, extensibility, and flexibility of the code can be improved through annotations.
[0060] Step S113 can parse each conventionally named file respectively to obtain the annotations included in each conventionally named file.
[0061] Step S114, determine the interface files whose annotations conform to the interface file annotation rules in the interface file recognition rules from multiple conventionally named files.
[0062] In some embodiments of the present disclosure, step S114 can match the annotations included in each conventionally named file with the interface file annotation rules respectively to determine the interface files whose annotations conform to the interface file annotation rules. The interface file annotation rules in step S114 can be rules preset by the user, and the interface file annotation rules can be fixed or modified in real time as needed.
[0063] Specifically, taking the target code file in Java format as an example, the interface file annotation rules can be files that accept annotations including @RestController, @Controller, @RemoteService. The process of determining the interface files through step S114 can include: judging one by one whether the annotations included in each conventionally named file include the content specified in the interface file annotation rules to obtain a judgment result; taking the conventionally named files that conform to the interface file annotation rules as interface files according to the judgment result.
[0064] By narrowing the file scope through steps S111 to S112 and parsing whether the file is an interface file through steps S113 to S114, the determination efficiency of the interface files can be improved.
[0065] Regarding step S120, in some embodiments of the present disclosure, it may include steps S121 to S126 as Figure 3 shown.
[0066] Step S121, judge whether the interface file includes interface-related annotation information.
[0067] In some embodiments of the present disclosure, step S121 can judge whether the interface file includes interface-related annotation information by attempting to obtain the annotation information based on the annotation acquisition method.
[0068] When it is determined in step S121 that the interface file includes annotation information related to the interface, step S122 is executed; otherwise, step S123 is executed. The annotation information related to the interface in step S121 can be either single-line annotation information or multi-line annotation information.
[0069] Step S122, determine the interface description according to the annotation information.
[0070] In some embodiments of the present disclosure, step S122 may use the annotation information as the interface description of the corresponding interface; step S122 may also process the annotation information to obtain the interface description, such as extracting structured information from the annotation information as the interface description, etc. The interface description determined through step S122 is a textual description of the interface function and purpose, and is usually used to help users understand the role and problems solved by the interface.
[0071] Step S123, obtain the annotation of the method of the interface from the interface file.
[0072] In some embodiments of the present disclosure, when it is determined in step S121 that the interface file does not include annotation information related to one or more interfaces, the annotation of the method of the above interface can be obtained from the interface file through step S123, and steps S124 to S126 are also performed for the above interface.
[0073] Step S124, determine whether the annotation of the method of the interface includes an interface operation annotation.
[0074] In some embodiments of the present disclosure, when it is determined in step S124 that the annotation of the method of the interface includes an interface operation annotation, step S125 is executed; otherwise, step S126 is executed.
[0075] Step S125, determine the interface description according to the interface operation annotation.
[0076] In some embodiments of the present disclosure, through step S125, the interface operation annotation can be analyzed to obtain the interface description.
[0077] Step S126, assign the interface description to be empty.
[0078] Steps S121 to S126 can obtain an accurate interface description based on the annotation information and the interface operation annotation.
[0079] Regarding step S120, in some embodiments of the present disclosure, it may also include steps S127 to S131 as Figure 4 shown.
[0080] Step S127, obtain the annotation related to the interface from the interface file.
[0081] In some embodiments of the present disclosure, step S127 may obtain annotations related to the interface by parsing the interface file.
[0082] Step S128: Determine whether the annotations related to the interface include a request method annotation.
[0083] In some embodiments of the present disclosure, when it is determined through step S128 that the annotations related to the interface include a request method annotation, step S129 is executed; otherwise, step S131 is executed. The request method annotation is an annotation used to identify the type of HTTP request (such as GET, POST, PUT, DELETE, etc.). The request method annotations may include: @RequestMapping for representing a general request method annotation, @GetMapping for handling GET requests, PostMapping for handling POST requests, @PutMapping for handling PUT requests, @DeleteMapping for handling DELETE requests, and @PatchMapping for handling PATCH requests.
[0084] Step S129: Determine the request method of the interface according to the request method annotation.
[0085] In some embodiments of the present disclosure, step S129 may determine the request method according to the specific type of the request method annotation. The request method determined through step S129 is part of the interface request information and is used to specify the HTTP method, indicating the type or operation of the request.
[0086] When it is determined through step S128 that one or more annotations related to the interface include a request method annotation, the corresponding request method can be determined through step S129 according to the request method annotations of these interfaces.
[0087] Step S131: Assign the request method of the interface as empty.
[0088] In some embodiments of the present disclosure, when it is determined through step S128 that one or more annotations related to the interface do not include a request method annotation, the request methods of these interfaces can be assigned as empty through step S131.
[0089] Steps S127 to S131 determine the request method based on the annotation, and can automatically, quickly, and accurately define the interface behavior.
[0090] Regarding step S129, in some embodiments of the present disclosure, it may include steps S1291 to S1297 as Figure 5 shown.
[0091] Step S1291: Obtain the annotation type of the request method annotation.
[0092] In some embodiments of the present disclosure, step S1291 may obtain the annotation type according to the specific content of the request method annotation; the annotation type may include: GET request annotation, POST request annotation, PUT request annotation, DELETE request annotation, or general request annotation.
[0093] For example: when the request method annotation includes @RequestMapping, the obtained annotation type is a general request annotation; when the request method annotation includes @GetMapping, the obtained annotation type is a GET request annotation; when the request method annotation includes @PostMapping, the obtained annotation type is a POST request annotation; when the request method annotation includes @PutMapping, the obtained annotation type is a PUT request annotation; when the request method annotation includes @DeleteMapping, the obtained annotation type is a DELETE request annotation.
[0094] When the annotation type obtained through step S1291 is a GET request annotation, execute step S1292; when the annotation type obtained through step S1291 is a POST request annotation, execute step S1293; when the annotation type obtained through step S1291 is a PUT request annotation, execute step S1294; when the annotation type obtained through step S1291 is a DELETE request annotation, execute step S1295; when the annotation type obtained through step S1291 is a general request annotation, execute step S1296.
[0095] Step S1292: Set the request method to a GET request.
[0096] Step S1293: Set the request method to a POST request.
[0097] Step S1294: Set the request method to a PUT request.
[0098] Step S1295: Set the request method to a DELETE request.
[0099] Step S1296: Obtain the value of the method attribute in the request method annotation and determine the request method according to the value of the method attribute.
[0100] In some embodiments of the present disclosure, the method attribute in the request method annotation is usually used to specify the HTTP method type of the request, and the value of the method attribute may be RequestMethod.GET, RequestMethod.POST, RequestMethod.PUT, RequestMethod.DELETE, etc.
[0101] When the value of the obtained method attribute is RequestMethod.GET, the determined request method is a GET request; when the value of the obtained method attribute is RequestMethod.POST, the determined request method is a POST request; when the value of the obtained method attribute is RequestMethod.PUT, the determined request method is a PUT request; when the value of the obtained method attribute is RequestMethod.DELETE, the determined request method is a DELETE request.
[0102] Steps S1291 to S1296 can quickly and accurately define the request method through the annotation type and the value of the method attribute.
[0103] Regarding step S120, in some embodiments of the present disclosure, it may include steps S132 to S135 as Figure 6 shown.
[0104] Step S132, obtain the method of the interface from the interface file.
[0105] In some embodiments of the present disclosure, the methods obtained through step S132 constitute the basic units of the class functions, and they define the behaviors of the class.
[0106] Step S133, obtain the parameters of the method of the interface.
[0107] In some embodiments of the present disclosure, when the method of the interface obtained through step S133 is a defined method, the parameters of the method of the interface are the variables placed within the parentheses after the method name.
[0108] Step S134, for the parameters of the method of the annotated interface, obtain the annotation type of the annotation and parse the parameters of the method of the interface according to the annotation type to obtain the interface request information other than the interface request method.
[0109] In some embodiments of the present disclosure, step S134 may use the parsing method corresponding to the annotation type to parse the parameters of the relevant method of the interface. The annotation type can be set according to the fields of the annotation.
[0110] Step S135, for the parameters of the method of the non-annotated interface, obtain the interface request method and parse the parameters of the method of the interface according to the request method to obtain the interface request information other than the interface request method.
[0111] In some embodiments of the present disclosure, step S135 may use the parsing method corresponding to the request method to parse the parameters of the relevant method of the interface.
[0112] Annotation of the parameters of the interface-based method through steps S132 to S135 can quickly and accurately parse the parameters of the interface method.
[0113] When the parameter of the method of the interface is a query parameter, regarding step S135, in some embodiments of the present disclosure, it may include steps S1351 to S1354 as Figure 7 shown.
[0114] Step S1351, obtain the parsing method related to the request method.
[0115] In some embodiments of the present disclosure, the parsing method obtained through step S1351 is related to the request method, and is specifically used to parse unannotated structured data; when the request method is a GET request, the parsing method obtained through step S1351 may be an unannotated query parsing method; when the request method is a POST request, the parsing method obtained through step S1351 may be an unannotated form parsing method.
[0116] Step S1352, use the parsing method to parse the parameters of the interface method to obtain interface request information other than the request method and parameter type of the interface.
[0117] Step S1353, obtain the parameter passing method and data type of the parameters of the interface.
[0118] In some embodiments of the present disclosure, the parameter passing method obtained through step S1353 may include query method, form method, etc.; the data types obtained through step S1353 may include String, MultipartFile, basic type, array type, class or interface type, etc.
[0119] Step S1354, analyze the parameter passing method and data type to obtain the parameter type of the interface.
[0120] In some embodiments of the present disclosure, step S1354 may analyze the parameter passing method and data type based on mapping relationships or calculation rules, etc., to obtain the parameter type of the interface.
[0121] When the parameter passing method is the query method, step S134 may be: when the data type is String, set the parameter type of the interface to "string"; when the data type is MultipartFile, set the parameter type of the interface to "object"; when the data type is a basic type, use the type acquisition method to obtain the parameter type of the interface; when the data type is an array type, set the parameter type of the interface to "array"; when the data type is a class or interface type, set the parameter type of the interface according to the subclass of the class or interface type. Among them, setting the parameter type of the interface according to the subclass of the class or interface type may include: when the subclass of the class or interface type is a wrapper class, use the type acquisition method to obtain the parameter type of the interface; when the subclass of the class or interface type is a Map class, set the parameter type of the interface to "object"; when the subclass of the class or interface type is a date type, set the parameter type of the interface to "string"; when the subclass of the class or interface type is a custom class, obtain the class name and file path of the custom class, and when the file path does not exist, set the parameter type of the interface to "object", and when the file path exists, recursively parse the types of the fields of the custom class until all fields of the custom class are parsed, and obtain the parameter type of the interface.
[0122] When the parameter passing method is the form method, step S134 may be: when the data type is String, set the parameter type of the interface to "text"; when the data type is MultipartFile, set the parameter type of the interface to "file"; when the data type is a basic type, set the parameter type of the interface to "text"; when the data type is an array type, set the parameter type of the interface to "text"; when the data type is a class or interface type, set the parameter type of the interface according to the subclass of the class or interface type. Among them, setting the parameter type of the interface according to the subclass of the class or interface type may include: when the subclass of the class or interface type is a wrapper class, set the parameter type of the interface to "text"; when the subclass of the class or interface type is a Map class, set the parameter type of the interface to "text"; when the subclass of the class or interface type is a custom class, obtain the class name and file path of the custom class, and when the file path does not exist, set the parameter type of the interface to "text", and when the file path exists, recursively parse the types of the fields of the custom class until all fields of the custom class are parsed, and obtain the parameter type of the interface.
[0123] In some embodiments of the present disclosure, for the parameters of the methods of the annotated interfaces, step S134 may obtain interface request information other than the request method and parameter type based on information such as the content of the annotation.
[0124] For example: Step S134 may determine the interface request type according to the content of the annotation and the request method; specifically, if the content of the annotation includes @RequestParam and the request method is GET, the determined interface request type is Query; if the content of the annotation includes @RequestParam and the request method is POST, the determined interface request type is Form; if the content of the annotation includes @RequestBody, the determined interface request type is Json.
[0125] Step S134 specifically determines the request path according to the path parameter annotation in the annotation; Step S134 may also obtain the annotation of the method related to this annotation and the annotation of the class related to this method, and determine the first sub-path according to the value of the class annotation, and determine the second sub-path according to the value of the method annotation, and merge the first sub-path and the second sub-path as the request path.
[0126] Step S134 may use the method of extracting parameter names to obtain the parameter names in the request parameters; Step S134 may determine the "whether mandatory" attribute in the request parameters based on the content of the annotation. For example, if the annotation contains Notnull or required=true, the "whether mandatory" attribute is "mandatory", otherwise it is "not mandatory"; Step S134 may determine the "field description" attribute in the request parameters based on the content of the annotation. For example, if the annotation contains RequestParam, the field description is determined according to the value of value or name. If the annotation contains @RequestBody, the field description is determined according to the annotation. If the annotation contains ApiParam annotation, the field description is determined according to the value of value; Step S134 may use the method of extracting field types to obtain the field types in the request parameters; Step S134 may determine the default value of the request parameters according to the value of DefaultValue when the annotation contains RequestParam or ApiParam annotation.
[0127] Regarding Step S134, in some embodiments of the present disclosure, it may include steps S1341 to S1342 as Figure 8 shown.
[0128] Step S1341, when the annotation type is the first type, obtain the parsing method related to the annotation type; parse the parameters of the method of the interface according to the parsing method related to the annotation type.
[0129] In some embodiments of the present disclosure, the first type in Step S1341 may be the type of annotation including @RequestParam, @RequestHeader, or @PathVariable, etc. For different annotations, the method parameters of the interface may be parsed using the relevant parsing methods.
[0130] Step S1342, when the annotation type is the second type, obtain the class name of the parameter of the method of the interface, and parse the parameter of the method of the interface according to the class name.
[0131] In some embodiments of the present disclosure, the second type in step S1342 may be a type including annotations such as @RequestBody, and step S1342 may parse the parameter of the method of the interface using a relevant parsing method based on the class name.
[0132] Specifically, when the annotation includes the @RequestParam annotation, step S134 may further include obtaining the type described by the annotation and setting the parameter type of the interface according to the type described by the annotation. Among them, the process of setting the parameter type of the interface according to the type described by the annotation may be: if the type described by the annotation is String, set the parameter type of the interface to "string"; if the type described by the annotation is MultipartFile, set the parameter type of the interface to "object"; if the type described by the annotation is a basic type, the parameter type of the interface is obtained based on the type obtaining method; if the type described by the annotation is an array type, set the parameter type of the interface to "array"; if the type described by the annotation is a class or interface type, obtain the subclass of the class or interface type, and set the parameter type of the interface according to the subclass of the class or interface type; if the type described by the annotation does not belong to any of the above, the parameter type of the interface may be set to "string".
[0133] The process of setting the parameter type of the interface according to the subclass of the class or interface type may include: if the specific type represented by the subclass of the class or interface type is a wrapper type, obtain the parameter type of the interface based on the type obtaining method; if the specific type represented by the subclass of the class or interface type is a Map type, set the parameter type of the interface to "object"; if the specific type represented by the subclass of the class or interface type is a date type, set the parameter type of the interface to "string"; if the specific type represented by the subclass of the class or interface type is a collection type, set the parameter type of the interface to "array".
[0134] Regarding step S120, in some embodiments of the present disclosure, it may include steps S136 to S137 as Figure 9 shown.
[0135] Step S136, obtain the return object type of the interface from the interface file.
[0136] In some embodiments of the present disclosure, the return object type obtained through step S136 may be null, string type, basic type, array type, class or interface type, etc.
[0137] Step S137, determine the return type of the interface according to the return object type of the interface.
[0138] In some embodiments of the present disclosure, for different return object types, step S137 may use different methods to obtain the return type. This return type is part of the interface return information.
[0139] Regarding step S137, in some embodiments of the present disclosure, it may include steps S1371 to S1375 as Figure 10 shown.
[0140] Step S1371, when the return object type is null, set the return type to null.
[0141] Step S1372, when the return object type is string type, set the return type to string type.
[0142] Step S1373, when the return object type is basic type, use the type acquisition method to obtain the return type.
[0143] Step S1374, when the return object type is array type, recursively parse the return object type to obtain the return type.
[0144] Step S1375, when the return object type is class or interface type, determine the return type according to the existence of the class related to the return object type.
[0145] In some embodiments of the present disclosure, when the return object type is class or interface type, it may be determined whether the class related to the return object type exists through step S1375. If it exists, use the type acquisition method to obtain the return type; if not, set the return type to "object".
[0146] For the information other than the return type in the interface return information, the corresponding acquisition method can be used to obtain the relevant information.
[0147] In some embodiments of the present disclosure, for convenient use, multiple lists may be set up to store request parameters of different types.
[0148] After obtaining the interface name, interface description, interface request information, and interface return information through step S120, step S130 can be executed in real time or delayed. At this time, the above information obtained through step S120 can be stored in the cache. When executed later, step S130 can first obtain the interface path from multiple interface files; determine whether the interface path is empty; if it is empty, return an empty interface definition; if the interface definition is not empty, try to obtain the interface name, interface description, interface request information, and interface return information from the cache; if the attempt fails, store them in the cache after analysis through step S120; if the attempt is successful, parse the specific content of the interface name, interface description, interface request information, and interface return information and then assemble them.
[0149] Figure 11 An exemplary flowchart implemented based on the interface definition acquisition method of the present disclosure is shown.
[0150] Figure 11 In the shown flowchart, taking the target code file as a code file for implementing user management including interfaces for obtaining user information, creating users, etc., the interface definition acquisition process may include: Step S210, determining the interface file from the target code file based on the interface file identification rule.
[0151] In some embodiments of the present disclosure, the target code file may include an entity class file for defining the data structure of users; a data access layer file responsible for interacting with the database and defining methods for adding, deleting, modifying, and querying user data; a service implementation class file for completing specific business logic processing; a configuration file containing the configuration information of the project; an interface file for defining interface specifications, etc. The interface file identification rule in step S210 is a pre-set rule, and the control interface file and service interface file can be determined from the target code file through step S210.
[0152] Step S220, for any interface described in the control interface file and service interface file, analyze the interface file to obtain the interface name, interface description, interface request information, and interface return information of the interface.
[0153] In some embodiments of the present disclosure, step S220 can analyze the annotations related to the interface in the control interface file and service interface file, or use related methods for analysis.
[0154] Step S230, assemble the interface name, interface description, interface request information, and interface return information to obtain the interface definition.
[0155] In some embodiments of the present disclosure, step S230 may assemble the interface name, interface description, interface request information, and interface return information based on a preset assembly rule.
[0156] Based on any of the above embodiments, the present disclosure further provides an interface definition acquisition device.
[0157] Figure 12 It is a schematic block diagram of the structure of the interface definition acquisition device according to an embodiment of the present disclosure.
[0158] As Figure 12 shown, the interface definition acquisition device includes: A file screening module 110, configured to determine an interface file from a target code file based on an interface file identification rule; An information acquisition module 120, configured to acquire an interface name, an interface description, interface request information, and interface return information based on the interface file.
[0159] An information assembly module 130, configured to assemble the interface name, the interface description, the interface request information, and the interface return information to obtain an interface definition.
[0160] The above interface definition acquisition device may be in the form of computer software, and each module of the above interface definition acquisition device may be implemented by a computer software module.
[0161] For the specific implementation process of the functions and roles of each module in the above device, please refer to the implementation process of the corresponding steps in the above interface definition acquisition method, which will not be elaborated here.
[0162] The execution subject of the interface definition acquisition method in the specific embodiments of the present disclosure may be an electronic device such as a computer or a server.
[0163] Therefore, based on any of the above embodiments, the present disclosure further provides an electronic device, which can execute the interface definition acquisition method of any of the above embodiments described in the present disclosure.
[0164] Figure 13 It is a schematic block diagram of the structure of an electronic device 1000 according to an embodiment of the present disclosure.
[0165] The hardware structure of the electronic device 1000 may be implemented using a bus architecture. The bus architecture may include any number of interconnected buses and bridges, depending on the specific application of the hardware and the overall design constraints. The bus 1100 connects various circuits including one or more processors 1200, a memory 1300, and / or hardware modules together. The bus 1100 may also connect various other circuits 1400 such as peripheral devices, voltage regulators, power management circuits, external antennas, etc.
[0166] The bus 1100 can be an Industry Standard Architecture (ISA) bus, a Peripheral Component Interconnect (PCI) bus, an Extended Industry Standard Architecture (EISA) bus, or the like. The bus can be divided into an address bus, a data bus, a control bus, etc. For the sake of convenience in representation, only one connection line is used in this figure, but it does not mean that there is only one bus or one type of bus.
[0167] The present disclosure also provides a readable storage medium, in which a computer program is stored. When the computer program is executed by a processor, it is used to implement the above method. The "readable storage medium" can be any device that can contain, store, communicate, propagate, or transmit a program for use by or in connection with an instruction execution system, apparatus, or device. More specific examples of the readable storage medium include the following: an electrical connection part (electronic device) having one or more wirings, a portable computer disk cartridge (magnetic device), a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber device, and a portable read-only memory (CDROM), etc.
[0168] The present disclosure also provides a computer program product. The method of the present disclosure can be implemented in whole or in part by software, hardware, firmware, or any combination thereof. When implemented using software, it can be implemented in whole or in part in the form of a computer program product. The computer program product includes one or more computer programs or instructions. When the computer program or instructions are loaded and executed, the processes or functions of the present disclosure are executed in whole or in part.
[0169] The computer program or instructions can be stored in a readable storage medium, or transmitted from one readable storage medium to another. For example, the computer program or instructions can be transmitted from one website, computer, server, or data center to another website, computer, server, or data center in a wired or wireless manner. The readable storage medium can be any available medium that can be accessed, or a data storage device such as a server or data center that integrates one or more available media. The available medium can be a magnetic medium, such as a floppy disk, a hard disk, or a magnetic tape; it can also be an optical medium, such as a digital video disc; or it can be a semiconductor medium, such as a solid-state drive. The computer-readable storage medium can be a volatile or non-volatile storage medium, or can include both volatile and non-volatile types of storage media.
[0170] Those skilled in the art should understand that the embodiments of the present disclosure may be provided as a method, a system, or a computer program product. Therefore, the present disclosure may take the form of a complete hardware embodiment, a complete software embodiment, or an embodiment combining software and hardware aspects. Moreover, the present disclosure may take the form of a computer program product implemented on one or more computer-usable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) that contain computer-usable program code.
[0171] The present disclosure is described with reference to the flowcharts and / or block diagrams of methods, apparatuses (systems), and computer program products according to the present disclosure. It should be understood that each flow and / or block in the flowchart and / or block diagram, as well as the combination of flows and / or blocks in the flowchart and / or block diagram, can be implemented by computer program instructions. These computer program instructions can be provided to the processor of a general-purpose computer, a special-purpose computer, an embedded processor, or other programmable data processing devices to generate a machine, such that the instructions executed by the processor of the computer or other programmable data processing devices generate means for implementing the functions specified in Figure 1 one flow or multiple flows and / or blocks Figure 1 one block or multiple blocks.
[0172] These computer program instructions can also be stored in a computer-readable memory that can direct a computer or other programmable data processing device to work in a specific manner, such that the instructions stored in the computer-readable memory generate a manufactured article including instruction means that implement the functions specified in Figure 1 one flow or multiple flows and / or blocks Figure 1 one block or multiple blocks.
[0173] These computer program instructions can also be loaded onto a computer or other programmable data processing device, such that a series of operation steps are executed on the computer or other programmable device to generate a computer-implemented process, and thus the instructions executed on the computer or other programmable device provide steps for implementing the functions specified in Figure 1 one flow or multiple flows and / or blocks Figure 1 one block or multiple blocks.
[0174] In the description of this specification, the descriptions with reference to terms such as "one embodiment / way", "some embodiments / ways", "example", "specific example", or "some examples", etc., mean that the specific features, structures, or characteristics described in connection with the embodiment / way or example are included in at least one embodiment / way or example of the present disclosure. In this specification, the schematic expressions of the above terms do not necessarily refer to the same embodiment / way or example. Moreover, the specific features, structures, or characteristics described can be combined in a suitable manner in any one or more embodiments / ways or examples. In addition, without contradiction, those skilled in the art can combine and combine the different embodiments / ways or examples described in this specification and the features of different embodiments / ways or examples.
[0175] In addition, the terms "first" and "second" are only used for descriptive purposes and cannot be construed as indicating or implying relative importance or implicitly specifying the quantity of the indicated technical features. Thus, the features defined with "first" and "second" may explicitly or implicitly include at least one of such features. In the description of the present disclosure, "a plurality" means at least two, such as two, three, etc., unless otherwise specifically defined.
[0176] Those skilled in the art should understand that the above embodiments are only for clearly explaining the present disclosure and are not intended to limit the scope of the present disclosure. For those skilled in the art, other changes or modifications can be made on the basis of the above disclosure, and these changes or modifications are still within the scope of the present disclosure.
Claims
1. An interface definition acquisition method, characterized in that, Including: Determining an interface file from a target code file based on an interface file recognition rule; Obtaining an interface name, an interface description, interface request information, and interface return information based on the interface file; And Assembling the interface name, the interface description, the interface request information, and the interface return information to obtain an interface definition.
2. The interface definition acquisition method according to claim 1, wherein The determining an interface file from a target code file based on an interface file recognition rule includes: Obtaining the file names of multiple sub-files included in the target code file; Determining multiple convention-named files from the multiple sub-files whose file names conform to the file naming rule in the interface file recognition rule; Obtaining the annotations included in the multiple convention-named files; and Determining an interface file from the multiple convention-named files whose annotations conform to the interface file annotation rule in the interface file recognition rule.
3. The interface definition acquisition method according to claim 1 or 2, characterized in that The obtaining an interface name, an interface description, interface request information, and interface return information based on the interface file includes: Judging whether the interface file includes annotation information related to the interface; If it includes, determining the interface description according to the annotation information; If it does not include, obtaining the annotation of the method of the interface from the interface file; Judging whether the annotation of the method of the interface includes an interface operation annotation; If the annotation of the method of the interface includes an interface operation annotation, determining the interface description according to the interface operation annotation; and If the annotation of the method of the interface does not include an interface operation annotation, assigning an empty value to the interface description.
4. The interface definition acquisition method according to claim 1 or 2, characterized in that The obtaining an interface name, an interface description, interface request information, and interface return information based on the interface file includes: Obtaining the annotation related to the interface from the interface file; Judging whether the annotation related to the interface includes a request method annotation; If it includes, determining the request method of the interface according to the request method annotation, and the request method is part of the interface request information; and If it does not include, assigning an empty value to the request method of the interface.
5. The interface definition acquisition method according to claim 1 or 2, characterized in that, The obtaining an interface name, an interface description, interface request information, and interface return information based on the interface file includes: Obtaining the method of the interface from the interface file; Obtaining the parameters of the method of the interface; For the parameters of the method of the interface with annotations, obtaining the annotation type of the annotation and parsing the parameters of the method of the interface according to the annotation type to obtain the interface request information other than the request method of the interface; and For the parameters of the method of the interface without annotations, obtaining the request method of the interface and parsing the parameters of the method of the interface according to the request method to obtain the interface request information other than the request method of the interface.
6. The interface definition acquisition method according to claim 5, wherein, The parsing the parameters of the method of the interface according to the request method to obtain the interface request information other than the request method of the interface includes: Obtaining the parsing method related to the request method; Using the parsing method to parse the parameters of the method of the interface to obtain the interface request information other than the request method and the parameter type of the interface; Obtaining the parameter passing method and data type of the parameters of the interface; and Analyzing the parameter passing method and data type to obtain the parameter type of the interface.
7. The method for obtaining an interface definition according to claim 5, wherein The parsing the parameters of the method of the interface according to the annotation type includes: When the annotation type is the first type, obtain the parsing method related to the annotation type; parse the parameters of the method of the interface according to the parsing method related to the annotation type; and When the annotation type is the second type, obtain the class name of the parameters of the method of the interface, and parse the parameters of the method of the interface according to the class name.
8. The interface definition acquisition method according to claim 1 or 2, characterized in that The obtaining of the interface name, interface description, interface request information, and interface return information based on the interface file includes: Obtain the return object type of the interface from the interface file; and Determine the return type of the interface according to the return object type of the interface, and the return type is part of the interface return information.
9. An electronic device, characterized in that, Includes: A memory that stores execution instructions; And A processor that executes the execution instructions stored in the memory, so that the processor executes the interface definition obtaining method according to any one of claims 1 to 8.
10. A computer program product, comprising a computer program, characterized in that, When the computer program is executed by the processor, it implements the interface definition obtaining method according to any one of claims 1 to 8.