Disclosure of Invention
In view of this, the embodiments of the present invention provide a method and apparatus for converting a call interface, which can support direct call of a target interface between service frameworks of different programming languages; the source code of the service frame is not required to be expanded, or a client corresponding to the service frame is not required to be provided; the application is wide.
To achieve the above object, according to one aspect of an embodiment of the present invention, there is provided a method of converting a call interface.
The method for converting the call interface comprises the following steps: acquiring an interface identifier of a target interface; the interface identifier carries a request path of the target interface; generating interface information of the target interface according to the interface identifier, and establishing mapping between the interface information and the request path; when the original interface is called through a call request, the target interface is called based on the mapping conversion; wherein the call request carries the request path.
Optionally, the interface identification includes request address annotation and method parameter annotation; the method further comprises the following steps of: the request address annotation and the method parameter annotation are added to the target interface.
Optionally, generating the interface information of the target interface according to the interface identifier includes: obtaining interface realization class and abstract method of the target interface according to the request address annotation and the method parameter annotation; obtaining a method parameter array of the abstract method according to the method parameter annotation of the target interface; and generating the interface information based on the interface implementation class, the abstract method and the method parameter array.
Optionally, the method parameter array includes a parameter name of the abstract method and a check rule indicating whether the parameters of the abstract method need to be filled; invoking the target interface based on the mapping transformation includes: acquiring the request path from the call request; acquiring the interface implementation class, the abstract method and the method parameter array from the mapping based on the request path; checking the abstract method according to the method parameter array; and when the abstract method passes the inspection, the Java reflection technology is utilized to realize class conversion and call the target interface according to the interface.
Optionally, establishing the mapping of the interface information and the request path includes: annotating the request address of the target interface to obtain the request path of the target interface; and establishing a mapping between the interface information of the target interface and a request path of the target interface.
Optionally, the method further comprises: and converting the return value of the calling target interface into a preset format and returning.
Optionally, the target interface is an RPC service interface, and the original interface is an HTTP interface.
To achieve the above object, according to still another aspect of the embodiments of the present invention, there is provided an apparatus for converting a call interface.
The device for converting the call interface in the embodiment of the invention comprises: the acquisition module is used for acquiring the interface identification of the target interface; the interface identifier carries a request path of the target interface; the generation module is used for generating interface information of the target interface according to the interface identifier and establishing a mapping between the interface information and the request path; the conversion calling module is used for calling the target interface based on the mapping conversion when the original interface is called through the calling request; wherein the call request carries the request path.
Optionally, the interface identification includes request address annotation and method parameter annotation; the apparatus further comprises: and the adding module is used for adding the request address annotation and the method parameter annotation for the target interface.
Optionally, the generating module is further configured to: obtaining interface realization class and abstract method of the target interface according to the request address annotation and the method parameter annotation; obtaining a method parameter array of the abstract method according to the method parameter annotation of the target interface; and generating the interface information based on the interface implementation class, the abstract method and the method parameter array.
Optionally, the method parameter array includes a parameter name of the abstract method and a check rule indicating whether the parameters of the abstract method need to be filled; and the conversion calling module is further used for: acquiring the request path from the call request; acquiring the interface implementation class, the abstract method and the method parameter array from the mapping based on the request path; checking the abstract method according to the method parameter array; and when the abstract method passes the inspection, the Java reflection technology is utilized to realize class conversion and call the target interface according to the interface.
Optionally, the generating module is further configured to: annotating the request address of the target interface to obtain the request path of the target interface; and establishing a mapping between the interface information of the target interface and a request path of the target interface.
Optionally, the apparatus further comprises: and the conversion module is used for converting the return value of the calling target interface into a preset format and returning.
Optionally, the target interface is an RPC service interface, and the original interface is an HTTP interface.
To achieve the above object, according to still another aspect of the embodiments of the present invention, there is provided an electronic device that converts a call interface.
The electronic equipment for converting the calling interface comprises the following components: one or more processors; and the storage device is used for storing one or more programs, and when the one or more programs are executed by the one or more processors, the one or more processors are enabled to realize the method for converting the calling interface.
To achieve the above object, according to still another aspect of the embodiments of the present invention, there is provided a computer-readable storage medium.
A computer-readable storage medium of an embodiment of the present invention has stored thereon a computer program which, when executed by a processor, implements a method of converting a call interface of an embodiment of the present invention.
One embodiment of the above invention has the following advantages or benefits: because the interface identification of the target interface is acquired; generating interface information of a target interface according to the interface identifier, and establishing mapping between the interface information and a request path; when the original interface is called through the call request, the call target interface is converted and called through the interface identification based on the technical means of mapping and converting the call target interface, so that the problem that the service interface cannot be called across program languages is overcome; if the client is not available, the service interface cannot be directly called, or the HTTP call of the phase change cannot be timely supported after the service framework is upgraded; the registry can not provide access to the outside, can only access in the local area network, and needs to adopt a specific request address format, so that the technical problem of limited application is solved, and the aim of directly calling the target interface between service frameworks of different programming languages is further achieved; the source code of the service frame is not required to be expanded, or a client corresponding to the service frame is not required to be provided; the application is wide.
Further effects of the above-described non-conventional alternatives are described below in connection with the embodiments.
Detailed Description
Exemplary embodiments of the present invention will now be described with reference to the accompanying drawings, in which various details of the embodiments of the present invention are included to facilitate understanding, and are to be considered merely exemplary. Accordingly, those of ordinary skill in the art will recognize that various changes and modifications of the embodiments described herein can be made without departing from the scope and spirit of the invention. Also, descriptions of well-known functions and constructions are omitted in the following description for clarity and conciseness.
It should be noted that the embodiments of the present invention and the technical features in the embodiments may be combined with each other without collision.
Fig. 1 is a schematic diagram of the main steps of a method of converting a call interface according to an embodiment of the invention.
As shown in fig. 1, the method for converting a call interface according to the embodiment of the present invention mainly includes the following steps:
step S101: and obtaining the interface identification of the target interface.
Since different service frameworks may use different programming languages, invoking a service interface between service frameworks may be performed through a generic interface (i.e., an original interface), but the original interface may be limited by certain requirements or format requirements, and the transmission efficiency or security is not as good as invoking the service interface directly. Such as a hypertext transfer protocol (HTTP) interface, which is limited to the hypertext transfer protocol, the HTTP interface needs to be invoked with an HTTP request header.
In addition, the calling service interface between the service frameworks can also be realized through a client or a registry. However, when the service interface is called by the client, the service frame is required to provide a corresponding client, if the service frame does not provide the client, the service interface cannot be directly called, or the HTTP call of the phase change cannot be timely supported after the service frame is upgraded; the registry cannot provide access to the outside, can only access in the local area network, and needs to adopt a specific request address format, so that the application is limited.
The method for converting and calling the interface can realize the conversion and calling of the service interface (namely the target interface) when the original interface is called through the interface identification, supports the calling of the target interface between the service frames of different programming languages, does not need to expand the source code of the service frames, or provides the client corresponding to the service frames, and can be widely applied. The interface identifier of the target interface carries the request path of the target interface, so that the interface identifier of the target interface can be acquired before the target interface is called.
The interface identification may include request address comments and method parameter comments. The request address annotation is an annotation indicating a request path of the target interface, and the value of the request address annotation is the request path.
In the embodiment of the invention, before the interface identifier of the target interface is acquired, request address annotation and method parameter annotation can be added to the target interface. Wherein request address annotations may be imposed on interface implementation classes and abstract methods of the target interface. The method parameter annotations may be added to the abstract method of the target interface.
Step S102: and generating interface information of the target interface according to the interface identifier, and establishing a mapping between the interface information and the request path.
After the interface identification of the target interface is obtained, the related information of the target interface is obtained according to the interface identification, and interface information is generated. And the interface identifier carries the request path of the target interface, so that the mapping between the interface information and the request path can be directly established.
In the embodiment of the invention, the generation of the interface information of the target interface can be realized by the following modes: obtaining interface realization class and abstract method of the target interface according to request address annotation and method parameter annotation; obtaining a method parameter array of the abstract method according to the method parameter annotation of the target interface; interface information is generated based on interface implementation classes, abstract methods and method parameter arrays.
Because the request address annotation is added on the interface realization class and the abstract method of the target interface, and the method parameter annotation is added on the abstract method of the target interface, the interface realization class and the abstract method and the method parameter array of the abstract method can be obtained according to the request address annotation and the method parameter annotation, and further interface information is generated.
In the embodiment of the invention, the establishment of the mapping of the interface information and the request path can be realized by the following modes: annotating according to the request address of the target interface to obtain a request path of the target interface; and establishing a mapping between the interface information of the target interface and the request path of the target interface.
The request address annotation is added on the interface implementation class and the abstract method of the target interface, namely the annotation value of the interface implementation class and the annotation value of the abstract method are the complete request paths of the target interface. After the request path is obtained through the request address annotation, a mapping of interface information and the request path can be established.
The method parameter array can indicate which parameters of the abstract method are mandatory. The method parameter array comprises a parameter name of the abstract method, a check rule for indicating whether the parameters of the abstract method are necessary to be filled, and the like.
Step S103: when the original interface is called by the call request, the target interface is called based on the mapping conversion.
The call request for calling the original interface carries a request path, and a corresponding target interface can be obtained from the mapping based on the request path, so that the call target interface is converted.
In the embodiment of the present invention, step S103 may be implemented by: acquiring a request path from a call request; acquiring interface realization classes, abstract methods and parameter arrays of the methods from the mapping based on the request path; checking the abstract method according to the method parameter array; when the abstract method passes the inspection, a Java reflection technology is utilized to realize class conversion and call the target interface according to the interface.
The mapping is the corresponding relation between the interface information and the request path, and the interface information of the target interface can be obtained through the mapping and the request path carried in the call request, so that the interface implementation class, the abstract method and the method parameter array of the target interface are obtained. According to the method parameter array, parameters of the abstract method can be checked, if the necessary parameters of the abstract method meet the requirements and the parameters are correct, the abstract method is confirmed to pass the check, and at the moment, a JAVA (JAVA-oriented object programming language) reflection technology can be utilized to realize class conversion and call the target interface according to the interface.
The JAVA reflection technique refers to: in the running state, for any one class, all the attributes and methods of the class can be known; any method and attribute can be invoked on any object. I.e. all properties and abstract methods of the interface implementation class can be known by JAVA reflection technology, so that the target interface can be invoked.
In the embodiment of the invention, the return value of the call target interface can be converted into a preset format and returned. Because the original interface is based on a text protocol, the interaction adopts a text format, and the service framework returns an object, when the service interface (namely the target interface) of the service framework is converted and called through the original interface, the return value of the calling target interface is converted into a general preset format and then returned to a calling party, for example, a JSON format or an XML format and the like.
In the embodiment of the invention, the target interface is an RPC service interface, and the original interface is an HTTP interface. Currently, there are many types of service frameworks that can be used to build independent order systems, payment systems, merchandise systems, or user systems, etc., such as RPC service frameworks.
The method for converting and calling the interface according to the embodiment of the invention can be seen in that the interface identifier of the target interface is acquired; generating interface information of a target interface according to the interface identifier, and establishing mapping between the interface information and a request path; when the original interface is called through the call request, the call target interface is converted and called through the interface identification based on the technical means of mapping and converting the call target interface, so that the problem that the service interface cannot be called across program languages is overcome; if the client is not available, the service interface cannot be directly called, or the HTTP call of the phase change cannot be timely supported after the service framework is upgraded; the registry can not provide access to the outside, can only access in the local area network, and needs to adopt a specific request address format, so that the technical problem of limited application is solved, and the aim of directly calling the target interface between service frameworks of different programming languages is further achieved; the source code of the service frame is not required to be expanded, or a client corresponding to the service frame is not required to be provided; the application is wide.
Fig. 2 is a flow diagram of an implementation of a method of converting a call interface according to an embodiment of the invention.
As shown in fig. 2, the method for converting and calling an interface according to the embodiment of the present invention may utilize a proxy server to implement a service interface for converting and calling a service framework through an original interface. Taking the conversion of the RPC service interface to the HTTP interface as an example, the method for converting the call interface is described, the annotation is selected as the interface identifier, and the specific implementation flow comprises:
first, annotations are defined, which fall into two categories:
The request address annotation (APIUrl) is an annotation for marking a request path of a target interface, and can be added on an interface implementation class and an abstract method of the target interface, APIUrl has an attribute value (value), the value is a part of the request path, the whole path of the target interface is an annotation value of the interface implementation class and an annotation value of the abstract method, for example, the annotation of the interface implementation class is @ APIUrl ("/order"), the annotation of the abstract method is @ APIUrl ("get"), and then the complete request path of the target interface is "/order/get";
The method parameter annotation (MethodParam) is an annotation of a method parameter array for marking the abstract method, the method parameter array comprises a parameter name of the abstract method, a check rule for marking whether the parameter of the abstract method is necessary to fill, and the like, the value of the annotation attribute 'name' is the name of the parameter, and the value of the annotation attribute 'must' is the check rule for marking whether the parameter is necessary to fill.
Then, a mapping is established in the proxy server:
After adding the annotation to the RPC service interface, a proxy server is needed as a proxy, which acts to accept HTTP requests, translate call RPC service interfaces, and return data. Before the proxy server provides the conversion call, the annotation of all the RPC service interfaces needs to be acquired first, the annotation can be acquired by scanning all the RPC service interfaces, the interface information of the RPC service interfaces is generated based on the annotation, and the mapping between the interface information and the request path is established. When the method for converting the call interface is applied, an initialization class can be established, and initialization is started when the proxy server is started, and the method comprises the following steps: the first step, all interface realization classes and abstract methods added with request address annotation and request paths are obtained; and a second step of: obtaining a method parameter array of the abstract method according to the method parameter annotation of the abstract method; and a third step of: packaging the interface implementation class, the abstract method and the method parameter array into a method detail object (RpcApiInfo), namely generating interface information of the RPC service interface; fourth step: and establishing a mapping between the interface information and the request path by taking the request path as a key and the interface information as a value (value) for storage.
Finally, proxy calls the request:
After the preparation work is completed, a call request of a calling party for calling the HTTP interface may be processed, specifically: the first step: acquiring a call request for calling an HTTP interface, and adding an interceptor into a proxy server to intercept all call requests of a user; and a second step of: inquiring interface information of a corresponding RPC service interface in the mapping according to a request path in the call request, thereby obtaining an interface implementation class, an abstract method and a method parameter array of the RPC service interface; and a third step of: checking the abstract method according to the method parameter array; after the abstract method passes the inspection, the RPC service interface is called by using Java reflection technology, and the return value of the RPC service interface is converted into a preset format and returned to the calling party.
Fig. 3 is a schematic diagram of main modules of an apparatus for converting a call interface according to an embodiment of the present invention.
As shown in fig. 3, an apparatus 300 for converting a call interface according to an embodiment of the present invention includes: an acquisition module 301, a generation module 302 and a conversion calling module 303.
Wherein,
An obtaining module 301, configured to obtain an interface identifier of a target interface; the interface identifier carries a request path of the target interface;
A generating module 302, configured to generate interface information of the target interface according to the interface identifier, and establish a mapping between the interface information and the request path;
a conversion calling module 303, configured to call the target interface based on the mapping conversion when the original interface is called by the call request; wherein the call request carries the request path.
In an embodiment of the invention, the interface identifier comprises request address annotation and method parameter annotation; the apparatus further comprises: and the adding module is used for adding the request address annotation and the method parameter annotation for the target interface.
In the embodiment of the present invention, the generating module 302 is further configured to: obtaining interface realization class and abstract method of the target interface according to the request address annotation and the method parameter annotation; obtaining a method parameter array of the abstract method according to the method parameter annotation of the target interface; and generating the interface information based on the interface implementation class, the abstract method and the method parameter array.
In the embodiment of the invention, the method parameter array comprises a parameter name of the abstract method and a check rule for marking whether the parameters of the abstract method need to be filled; and the conversion calling module 303 is further configured to: acquiring the request path from the call request; acquiring the interface implementation class, the abstract method and the method parameter array from the mapping based on the request path; checking the abstract method according to the method parameter array; and when the abstract method passes the inspection, the Java reflection technology is utilized to realize class conversion and call the target interface according to the interface.
In an embodiment of the present invention, the generating module 302 is further configured to: annotating the request address of the target interface to obtain the request path of the target interface; and establishing a mapping between the interface information of the target interface and a request path of the target interface.
In an embodiment of the present invention, the apparatus further includes: and the conversion module is used for converting the return value of the calling target interface into a preset format and returning.
Furthermore, the target interface is an RPC service interface.
The device for converting the call interface according to the embodiment of the invention can be seen in that the interface identifier of the acquisition target interface is adopted; generating interface information of a target interface according to the interface identifier, and establishing mapping between the interface information and a request path; when the original interface is called through the call request, the call target interface is converted and called through the interface identification based on the technical means of mapping and converting the call target interface, so that the problem that the service interface cannot be called across program languages is overcome; if the client is not available, the service interface cannot be directly called, or the HTTP call of the phase change cannot be timely supported after the service framework is upgraded; the registry can not provide access to the outside, can only access in the local area network, and needs to adopt a specific request address format, so that the technical problem of limited application is solved, and the aim of directly calling the target interface between service frameworks of different programming languages is further achieved; the source code of the service frame is not required to be expanded, or a client corresponding to the service frame is not required to be provided; the application is wide.
Fig. 4 illustrates an exemplary system architecture 400 of a method of converting a call interface or an apparatus of converting a call interface to which embodiments of the present invention may be applied.
As shown in fig. 4, the system architecture 400 may include terminal devices 401, 402, 403, a network 404, and a server 405. The network 404 is used as a medium to provide communication links between the terminal devices 401, 402, 403 and the server 405. The network 404 may include various connection types, such as wired, wireless communication links, or fiber optic cables, among others.
A user may interact with the server 405 via the network 404 using the terminal devices 401, 402, 403 to receive or send messages or the like. Various communication client applications, such as shopping class applications, web browser applications, search class applications, instant messaging tools, mailbox clients, social platform software, etc., may be installed on the terminal devices 401, 402, 403.
The terminal devices 401, 402, 403 may be various electronic devices having a display screen and supporting web browsing, including but not limited to smartphones, tablets, laptop and desktop computers, and the like.
The server 405 may be a server providing various services, such as a background management server providing support for shopping-type websites browsed by the user using the terminal devices 401, 402, 403. The background management server can analyze and other processing on the received data such as the product information inquiry request and the like, and feed back processing results (such as target push information and product information) to the terminal equipment.
It should be noted that, the method for converting the call interface provided in the embodiment of the present invention is generally executed by the server 405, and accordingly, the device for converting the call interface is generally disposed in the server 405.
It should be understood that the number of terminal devices, networks and servers in fig. 4 is merely illustrative. There may be any number of terminal devices, networks, and servers, as desired for implementation.
Referring now to FIG. 5, there is illustrated a schematic diagram of a computer system 500 suitable for use in implementing an embodiment of the present invention. The terminal device shown in fig. 5 is only an example, and should not impose any limitation on the functions and the scope of use of the embodiment of the present invention.
As shown in fig. 5, the computer system 500 includes a Central Processing Unit (CPU) 501, which can perform various appropriate actions and processes according to a program stored in a Read Only Memory (ROM) 502 or a program loaded from a storage section 508 into a Random Access Memory (RAM) 503. In the RAM 503, various programs and data required for the operation of the system 500 are also stored. The CPU 501, ROM 502, and RAM 503 are connected to each other through a bus 504. An input/output (I/O) interface 505 is also connected to bus 504.
The following components are connected to the I/O interface 505: an input section 506 including a keyboard, a mouse, and the like; an output portion 507 including a Cathode Ray Tube (CRT), a Liquid Crystal Display (LCD), and the like, and a speaker, and the like; a storage portion 508 including a hard disk and the like; and a communication section 509 including a network interface card such as a LAN card, a modem, or the like. The communication section 509 performs communication processing via a network such as the internet. The drive 510 is also connected to the I/O interface 505 as needed. A removable medium 511 such as a magnetic disk, an optical disk, a magneto-optical disk, a semiconductor memory, or the like is mounted on the drive 510 as needed so that a computer program read therefrom is mounted into the storage section 508 as needed.
In particular, according to embodiments of the present disclosure, the processes described above with reference to flowcharts may be implemented as computer software programs. For example, embodiments of the present disclosure include a computer program product comprising a computer program embodied on a computer readable medium, the computer program comprising program code for performing the method shown in the flow chart. In such an embodiment, the computer program may be downloaded and installed from a network via the communication portion 509, and/or installed from the removable media 511. The above-described functions defined in the system of the present invention are performed when the computer program is executed by a Central Processing Unit (CPU) 501.
The computer readable medium shown in the present invention may be a computer readable signal medium or a computer readable storage medium, or any combination of the two. The computer readable storage medium can be, for example, but not limited to, an electronic, magnetic, optical, electromagnetic, infrared, or semiconductor system, apparatus, or device, or a combination of any of the foregoing. More specific examples of the computer-readable storage medium may include, but are not limited to: an electrical connection having one or more wires, a portable computer diskette, a hard disk, a Random Access Memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disc read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the foregoing. In the context of this document, a computer readable storage medium may be any tangible medium that can contain, or store a program for use by or in connection with an instruction execution system, apparatus, or device. In the present invention, however, the computer-readable signal medium may include a data signal propagated in baseband or as part of a carrier wave, with the computer-readable program code embodied therein. Such a propagated data signal may take any of a variety of forms, including, but not limited to, electro-magnetic, optical, or any suitable combination of the foregoing. A computer readable signal medium may also be any computer readable medium that is not a computer readable storage medium and that can communicate, propagate, or transport a program for use by or in connection with an instruction execution system, apparatus, or device. Program code embodied on a computer readable medium may be transmitted using any appropriate medium, including but not limited to: wireless, wire, fiber optic cable, RF, etc., or any suitable combination of the foregoing.
The flowcharts and block diagrams in the figures illustrate the architecture, functionality, and operation of possible implementations of systems, methods and computer program products according to various embodiments of the present invention. In this regard, each block in the flowchart or block diagrams may represent a module, segment, or portion of code, which comprises one or more executable instructions for implementing the specified logical function(s). It should also be noted that, in some alternative implementations, the functions noted in the block may occur out of the order noted in the figures. For example, two blocks shown in succession may, in fact, be executed substantially concurrently, or the blocks may sometimes be executed in the reverse order, depending upon the functionality involved. It will also be noted that each block of the block diagrams or flowchart illustration, and combinations of blocks in the block diagrams or flowchart illustration, can be implemented by special purpose hardware-based systems which perform the specified functions or acts, or combinations of special purpose hardware and computer instructions.
The modules involved in the embodiments of the present invention may be implemented in software or in hardware. The described modules may also be provided in a processor, for example, as: a processor includes an acquisition module, a generation module, and a conversion calling module. The names of these modules do not in any way limit the module itself, and the acquisition module may also be described as a "module for acquiring the interface identifier of the target interface", for example.
As another aspect, the present invention also provides a computer-readable medium that may be contained in the apparatus described in the above embodiments; or may be present alone without being fitted into the device. The computer readable medium carries one or more programs which, when executed by a device, cause the device to include: step S101: acquiring an interface identifier of a target interface; step S102: generating interface information of a target interface according to the interface identifier, and establishing mapping between the interface information and a request path; step S103: when the original interface is called by the call request, the target interface is called based on the mapping conversion.
According to the technical scheme of the embodiment of the invention, the interface identification of the target interface is acquired; generating interface information of a target interface according to the interface identifier, and establishing mapping between the interface information and a request path; when the original interface is called through the call request, the call target interface is converted and called through the interface identification based on the technical means of mapping and converting the call target interface, so that the problem that the service interface cannot be called across program languages is overcome; if the client is not available, the service interface cannot be directly called, or the HTTP call of the phase change cannot be timely supported after the service framework is upgraded; the registry can not provide access to the outside, can only access in the local area network, and needs to adopt a specific request address format, so that the technical problem of limited application is solved, and the aim of directly calling the target interface between service frameworks of different programming languages is further achieved; the source code of the service frame is not required to be expanded, or a client corresponding to the service frame is not required to be provided; the application is wide.
The above embodiments do not limit the scope of the present invention. It will be apparent to those skilled in the art that various modifications, combinations, sub-combinations and alternatives can occur depending upon design requirements and other factors. Any modifications, equivalent substitutions and improvements made within the spirit and principles of the present invention should be included in the scope of the present invention.