Summary of the invention
Method, a kind of server and a kind of client that the embodiment of the present invention provides a kind of method of the REST of generation service, a kind of REST that generates client Java API to realize, the mode that adopts the h coding in existing mode generates the REST service and REST realizes, the problem that efficiency is low and code quality is difficult to guarantee in order to solve.
The embodiment of the present invention provides a kind of method of the REST of generation service, comprising:
Each service interface of server scanning java application DLL (dynamic link library) API, obtain interface message corresponding to each service interface;
For each described service interface, the interface message that server is corresponding according to this service interface generates the class file of REST service class corresponding to this service interface;
For each REST service class, be used for defining the note of REST service features in the interface message of server according to the class file of this REST service class and service interface corresponding to this REST service class, generate REST service corresponding to this REST service class.
The method of utilizing the embodiment of the present invention to provide, server scanning Java API, generate the class file of REST service class, finally generates the REST service, has that efficiency is high, a generating code advantage accurately.
Preferably, one or more service interfaces of described Java API are for establishing the service interface of authority;
Described for each described service interface, the class file that the interface message that server is corresponding according to this service interface generates REST service class corresponding to this service interface also comprises:
Established the service interface of authority for each, server is placed in this with this code that is used for control of authority of having established the rights service interface and has established the class file of REST service class corresponding to rights service interface;
Wherein, this code that is used for control of authority of having established the rights service interface is to be converted to by this note that is used for control of authority of having established interface message corresponding to the service interface of authority.
, by adding and explain on Java API Service interface definition or in the method definition, can control neatly the control of authority strategy of REST service access.Note on Service interface definition can be shared by a plurality of method of this service interface, and this makes code structure more clear succinct, has also improved the reusable degree of code logic.
Preferably, each service interface of described server scanning Java API, obtain interface message corresponding to each service interface, comprising:
Server is determined the service interface of root node service interface for scanning of described Java API;
Server scans the service interface that will scan, and obtains interface message corresponding to service interface;
If it is the method for interface that the interface message that the service interface that obtains is corresponding comprises the rreturn value type, server is determined the service interface that next will scan according to the rreturn value of the method, and returns to the step that the service interface that will scan is scanned; Otherwise end scanning process.
This scheme relatively and prior art, the structure of Java API has been carried out simplifying processed, utilize incidence relation between each service interface of Java API to realize the scanning of all service interfaces, overcome in the method for existing artificial generation REST service, due to the complex structure of Java API with can't find the unified problem of obtaining interface message method corresponding to each service interface.
Preferably, described server scans the service interface that will scan, and obtains interface message corresponding to service interface and comprises:
Server loads the object of Java reflection class from the java class loader, use the method for Java reflection class object to obtain the interface message corresponding to service interface that will scan.
Utilize the method for Java reflection class object, can obtain more simply, easily the interface message corresponding to service interface that will scan.
Preferably, described for each described service interface, the interface message that server is corresponding according to this service interface generates the class file of REST service class corresponding to this service interface, comprising:
For each described service interface, server generates the source code of REST service class corresponding to this service interface according to the interface message of this service interface that obtains;
The source code of the REST service class that server is corresponding according to described this service interface, generate the class file of REST service class corresponding to this service interface.
Write code with existing manual type and compare, the interface message that in this programme, server is corresponding according to service interface generates the class file of REST service class corresponding to this service interface automatically, and efficiency is higher, and the code of generation is more accurate.
Preferably, described for each described service interface, the interface message that server is corresponding according to this service interface generates the class file of REST service class corresponding to this service interface, comprising:
For each described service interface, the interface message that server is corresponding according to this service interface, utilize Java bytecode operative technique, generates the class file of REST service class corresponding to this service interface.
In this programme, adopt Java bytecode operative technique, need not to write source code, saved the time that code transforms, improved efficiency.
Preferably, described for a REST service class, be used for defining the note of REST service features in the interface message of server according to the class file of this REST service class and service interface corresponding to this REST service class, generate REST service corresponding to this REST service class, comprising:
Server is corresponding REST service class to the class file that Java Virtual Machine loads each REST service class that generates, and each REST service class that loading is obtained encapsulates in Wink Application object;
For each REST service class, server, by the Wink middleware, utilizes the annotating information that is used for defining the REST service features in service interface corresponding to this REST service class, generates REST service corresponding to this REST service class.
In this programme, automatically generate the REST service by the Wink middleware, more accurate than existing manual method, efficiency is higher.
A kind of method that the embodiment of the present invention provides REST that generates client Java API to realize comprises:
Each service interface of client scan java application DLL (dynamic link library) API, obtain the interface message of each service interface, and wherein said Java API is identical with the Java API on REST service place server that this client will be called;
For each described service interface, client generates REST corresponding to this service interface according to the interface message of this service interface and realizes the class file of class, and class file comprises the code of the client-requested REST service related parameters of determining according to the note that is used for defining the REST service features in the interface message of this service interface;
Client loads each REST that generates and realizes the class file of class, and the REST that generates client Java API realizes.
The method of utilizing the embodiment of the present invention to provide, client scan Java API, generate REST and realize the class file of class, and the REST of End-Customer end Java API realizes, has that efficiency is high, a generating code advantage accurately.
Preferably, each service interface of described client scan Java API, obtain interface message corresponding to each service interface and comprise:
Client is determined the service interface of root node service interface for scanning of described Java API;
Client scans the service interface that will scan, and obtains interface message corresponding to service interface;
If it is the method for interface that the interface message that the service interface that obtains is corresponding comprises the rreturn value type, client is determined the service interface that next will scan according to the rreturn value of the method, and returns to the step that the service interface that will scan is scanned; Otherwise end scanning process.
This scheme relatively and prior art, the structure of Java API has been carried out simplifying processed, utilize incidence relation between each service interface of Java API to realize the scanning of all service interfaces, overcome in the method that existing artificial generation REST realizes, due to the complex structure of Java API with can't find the unified problem of obtaining interface message method corresponding to each service interface.
Preferably, described client scans the service interface that will scan, and obtains interface message corresponding to service interface and comprises;
Client loads the object of Java reflection class from the java class loader, use the method for Java reflection class object to obtain the interface message corresponding to service interface that will scan.
This programme utilizes the method for Java reflection class object, can obtain more simply, easily the interface message corresponding to service interface that will scan.
Preferably, described for each described service interface, the client interface message corresponding according to this service interface generates REST corresponding to this service interface and realizes that the class file of class comprises:
For each described service interface, the interface message of this service interface that the client basis is obtained generates REST corresponding to this service interface and realizes the source code of class;
Realize the source code of class according to REST corresponding to described this service interface, client generates the REST corresponding with this service interface and realizes the class file of class.
Write code with existing manual type and compare, in this programme, the client interface message corresponding according to service interface generates the class file of REST service class corresponding to this service interface automatically, and efficiency is higher, and the code of generation is more accurate.
Described for each described service interface, the client interface message corresponding according to this service interface generates REST corresponding to this service interface and realizes that the class file of class comprises:
For each described service interface, the interface message that client is corresponding according to this service interface, utilize Java bytecode operative technique, generates the REST corresponding with this service interface and realize the class file of class.
In this programme, adopt Java bytecode operative technique, need not to write source code, saved the time that code transforms, improved efficiency.
The embodiment of the present invention provides a kind of server, and this server comprises:
Scan module, each service interface for scanning java application DLL (dynamic link library) API, obtain interface message corresponding to each service interface;
The class file generation module, be used for for each described service interface, and the interface message corresponding according to this service interface generates the class file of REST service class corresponding to this service interface;
REST service creation module, be used for for each REST service class, be used for defining the note of REST service features in interface message according to the class file of this REST service class and service interface corresponding to this REST service class, generate REST service corresponding to this REST service class.
In this programme, server scanning Java API, generate the class file of REST service class, finally generates the REST service, has that efficiency is high, a generating code advantage accurately.
Preferably, one or more service interfaces of described Java API are for establishing the service interface of authority;
Described class file generation module also is used for: established the service interface of authority for each, this code that is used for control of authority of having established the rights service interface has been placed in this has established the class file of REST service class corresponding to rights service interface;
Wherein, this code that is used for control of authority of having established the rights service interface is that the note that is used for control of authority that this that obtain by described scan module established interface message corresponding to the service interface of authority is converted to.
, by adding and explain on Java API Service interface definition or in the method definition, can control neatly the control of authority strategy of REST service access.Note on Service interface definition can be shared by a plurality of method of this service interface, and this makes code structure more clear succinct, has also improved the reusable degree of code logic.
Preferably, described scan module specifically is used for: the service interface of root node service interface for scanning of determining described Java API; The service interface that will scan is scanned, obtain interface message corresponding to service interface; , if it is the method for interface that the interface message that the service interface that obtains is corresponding comprises the rreturn value type, according to the rreturn value of the method, determines the service interface that next will scan, and return to the step that the service interface that will scan is scanned; Otherwise end scanning process.
This scheme relatively and prior art, the structure of Java API has been carried out simplifying processed, utilize incidence relation between each service interface of Java API to realize the scanning of all service interfaces, overcome in the method that existing artificial generation REST service and REST realize, due to the complex structure of Java API with can't find the unified problem of obtaining interface message method corresponding to each service interface.
Preferably, described the service interface that will scan is scanned, obtaining interface message corresponding to service interface comprises: load the object of Java reflection class from the java class loader, use the method for Java reflection class object to obtain the interface message corresponding to service interface that will scan.
Utilize the method for Java reflection class object, can obtain more simply, easily the interface message corresponding to service interface that will scan.
Preferably, described class file generation module specifically is used for:, for each described service interface, generate the source code of REST service class corresponding to this service interface according to the interface message of this service interface that obtains; The source code of the REST service class corresponding according to described this service interface, generate the class file of REST service class corresponding to this service interface.
Write code with existing manual type and compare, the interface message that in this programme, server is corresponding according to service interface generates the class file of REST service class corresponding to this service interface automatically, and efficiency is higher, and the code of generation is more accurate.
Preferably, described class file generation module specifically is used for: for each described service interface, the interface message corresponding according to this service interface, utilize Java bytecode operative technique, generates the class file of REST service class corresponding to this service interface.
In this programme, adopt Java bytecode operative technique, need not to write source code, saved the time that code transforms, improved efficiency.
Preferably, described REST service creation module specifically is used for: the class file that loads each REST service class that generates to Java Virtual Machine is corresponding REST service class, and each REST service class that loading is obtained encapsulates in Wink Application object; For each REST service class,, by the Wink middleware, utilize the annotating information that is used for defining the REST service features in service interface corresponding to this REST service class, generate REST service corresponding to this REST service class.
In this programme, automatically generate the REST service by the Wink middleware, more accurate than existing manual method, efficiency is higher.
The embodiment of the present invention provides a kind of client, and this client comprises:
Scan module,, for each service interface of scanning java application DLL (dynamic link library) API, obtain the interface message of each service interface, and wherein said Java API is identical with the Java API that the REST that this client will be called serves on the server of place;
The class file generation module, be used for for each described service interface, generate REST corresponding to this service interface according to the interface message of this service interface and realize the class file of class, class file comprises the code of the client-requested REST service related parameters of determining according to the note that is used for defining the REST service features in the interface message of this service interface;
REST realizes generation module, is used for loading each REST that generates and realizes the class file of class, and the REST that generates client Java API realizes.
In this programme, client scan Java API, generate REST and realize the class file of class, and the REST of End-Customer end Java API realizes, compared with prior art, has that efficiency is high, a generating code advantage accurately.
Preferably, described scan module specifically is used for: the service interface of root node service interface for scanning of determining described Java API; The service interface that will scan is scanned, obtain interface message corresponding to service interface; , if it is the method for interface that the interface message that the service interface that obtains is corresponding comprises the rreturn value type, according to the rreturn value of the method, determines the service interface that next will scan, and return to the step that the service interface that will scan is scanned; Otherwise end scanning process.
This scheme relatively and prior art, the structure of Java API has been carried out simplifying processed, utilize incidence relation between each service interface of Java API to realize the scanning of all service interfaces, overcome in the method that existing artificial generation REST realizes, due to the complex structure of Java API with can't find the unified problem of obtaining interface message method corresponding to each service interface.
Preferably, described the service interface that will scan is scanned, obtain interface message corresponding to service interface and comprise; Load the object of Java reflection class from the java class loader, use the method for Java reflection class object to obtain the interface message corresponding to service interface that will scan.
This programme utilizes the method for Java reflection class object, can obtain more simply, easily the interface message corresponding to service interface that will scan.
Preferably, described class file generation module specifically is used for:, for each described service interface, generate REST corresponding to this service interface according to the interface message of this service interface that obtains and realize the source code of class; Realize the source code of class according to REST corresponding to described this service interface, generate the REST corresponding with this service interface and realize the class file of class.
Write code with existing manual type and compare, in this programme, the client interface message corresponding according to service interface generates the class file of REST service class corresponding to this service interface automatically, and efficiency is higher, and the code of generation is more accurate.
Preferably, described class file generation module specifically is used for: for each described service interface, the interface message corresponding according to this service interface, utilize Java bytecode operative technique, generates the REST corresponding with this service interface and realize the class file of class.
In this programme, adopt Java bytecode operative technique, need not to write source code, saved the time that code transforms, improved efficiency.
Embodiment
Method, a kind of server and a kind of client that the embodiment of the present invention provides a kind of method of the REST of generation service, a kind of REST that generates client Java API to realize, in the method that the embodiment of the present invention provides, server scanning Java API, generate the class file of REST service class, final generate the REST service, have that efficiency is high, a generating code advantage accurately.
Below, as follows to the several concept brief descriptions that relate in the embodiment of the present invention:
Service: in computer network, offered the interface that is used for data interaction of client by server.
The REST service: with the service that the REST development scheme provides, its service abstraction with server becomes resource, uses URL to identify and search, and by http protocol, conducts interviews.
Realize: in java applet, interface definition the behavior of class and method, but there is no the specific code of define method, the method that is embodied as in interface provides concrete code, with Interface realization, is class.In the embodiment of the present invention, the realization of the Java API of the realization of this locality of Java API and client is the difference realization of same Java API.
REST realizes: is used for initiating the REST services request to server, and the REST response of reception server, and this response is converted into method rreturn value type corresponding to Java API, return to client application.
Framework: the method that the embodiment of the present invention provides automatically performs when server or client terminal start-up,, for server, is used for generating the REST service; , for client, be used for generating the REST realization of client Java API.Needn't regenerate again REST service or REST realization when server or client operation.In embodiments of the present invention, framework is what move on server or client, based on Java API, and by implementing each step of the embodiment of the present invention, the program that the REST service of generation server or the REST of client realize.
For server, during startup of server, framework starts Apache Wink middleware (hereinafter to be referred as " Wink middleware ") automatically, call the get Application method of the Rest Servlet class of Wink middleware, ApplicationEx class after obtaining to expand, the object of establishment ApplicationEx class, trigger each service interface of carrying out scanning Java API, the process that generates class file, generation REST service according to the interface message of service interface.Wherein, the method of the get Classes of the object by calling this establishment ApplicationEx class, call the Generator class that framework provides in get Classes method, generate the object of Generator class, read the root node service interface (method for the root node service interface is set of perhaps calling the Generator class object arranges the root node service interface) of Java API from configuration file.Call the operation of the scan method of Generator class object and each service interface that the generate method scans respectively Java API and the class file that generates the REST service class, start to scan each service interface of whole Java API from Java API root node service interface, generate the class file of REST service class according to the interface message of obtaining service interface.In get Classes method, the class file that loads each REST service class that generates to Java Virtual Machine is corresponding REST service class and returns to the Wink middleware afterwards.For each REST service class, the Wink middleware utilizes the annotating information that is used for defining the REST service features in service interface corresponding to this REST service class, generates REST service corresponding to this REST service class.
For server, framework has been expanded the Application class of Wink middleware, has rewritten the get Classes method of Application class, has obtained the ApplicationEX class after the expansion; Expand the Rest Servlet class of Wink middleware, rewritten its get Application method, obtained the ApplicationEX class after the expansion.
The example code of ApplicationEx class is as follows, has rewritten the get Classes method of Application class in this code, is used for loading the class file that generates; Use the java class loader, each REST service class is loaded as the Class object.
Code snippet 1, ApplicationEx class
For client, during client terminal start-up, call the Generator class that framework provides, generate the object of Generator class, read Java API root node service interface (method for the root node service interface is set of perhaps calling the Generator class object arranges the root node service interface) from configuration file; The method that the generation client Java API REST that the start method of calling the generator class object is carried out the embodiment of the present invention to be provided realizes, start to scan each service interface of Java API from Java API root node service interface, generate REST according to the interface message of obtaining service interface and realize the class file of class, load each REST that generates and realize the REST realization of the class file generation client Java API of class.
As shown in Figure 1, the process of client-access Java API: when client application D module need to be accessed Java API, create the object of a Java API root node service interface, call the root node interface method (such as: get service1) obtain child node service interface service1, call again child node service interface service1 method (such as: getusername is used for obtaining user name).The C module is converted into the REST services request with the request of the access Java API of module D and issues server, and server returns to the REST response, and the method rreturn value that the C module is converted into the REST response getusername again returns to client application D module.
The procedure schematic diagram that the generation REST that Fig. 2 provides for the embodiment of the present invention serves, as shown in Figure 2, the method comprises the following steps:
S201: each service interface of server scanning java application DLL (dynamic link library) API, obtain interface message corresponding to each service interface;
S202: for each described service interface, the interface message that server is corresponding according to this service interface generates the class file of REST service class corresponding to this service interface;
S203:, for each REST service class, be used for defining the note of REST service features in the interface message of server according to the class file of this REST service class and service interface corresponding to this REST service class, generate REST service corresponding to this REST service class.
In step S201, the interface message that the service interface that obtains is corresponding includes but not limited to: all methods of the interface name of service interface, the note on Service interface definition and service interface, and the parameter of each method of service interface, the rreturn value type of each method of service interface and the note on each method of service interface.
Wherein, note comprises be used to the note that defines the REST service features, such as, the JAX-RS(Java API for RESTful Web Services of standard, adopt the Java API of the web services of REST development scheme exploitation) to explain, it can be used for defining the REST service features.JAX-RS explains, and is the note that meets REST service note standard, and the JAX-RS of standard explains and comprises:
1) be used for arranging the note of HTTP access method;
2) be used for arranging the note of REST service request parameter;
3) be used for arranging the note etc. in the URL path of REST service.
JAX-RS note commonly used and uses thereof is as shown in table 1.
Table 1
Alternatively, one or more service interfaces of Java API are for establishing the service interface of authority;
Step S202, for each service interface, the class file that the interface message that server is corresponding according to this service interface generates REST service class corresponding to this service interface also comprises:
Established the service interface of authority for each, server is placed in this with this code that is used for control of authority of having established the rights service interface and has established the class file of REST service class corresponding to rights service interface;
Wherein, this code that is used for control of authority of having established the rights service interface is to be converted to by this note that is used for control of authority of having established interface message corresponding to the service interface of authority.Below the note that is used for control of authority is described.
, by adding and explain on Java API Service interface definition or in the method definition, can control neatly the control of authority strategy of REST service access.Explain class@REST Access Control(such as using " has Role (admin) "), just can allow admin user to have the authority of access REST service by parameter configuration.Note on Service interface definition can be shared by a plurality of method of this service interface, and this makes code structure more clear succinct, has also improved the reusable degree of code logic.
With establish the note that is used for control of authority in the rights service interface be converted into this example of having established the code that is used for control of authority of service class corresponding to rights service interface can be referring to example four.
In the embodiment of the present invention, the note that is used for control of authority can be added in Service interface definition, also can be added in the method definition of service interface.Be added in all methods that the note that is used for control of authority on Service interface definition is applicable to this service interface, the note that is added in the method definition of service interface is only applicable to corresponding method.If have simultaneously the note that is used for control of authority on Service interface definition and in certain method definition of this service interface, according to the note that is used for control of authority that is positioned in corresponding method definition, generate the code of the control of authority of corresponding method, and be placed in the method body of the method.
The embodiment of the present invention, the note that is used for control of authority identifies with "@Access Control ", when execution step S202 generates the code that is used for control of authority, when identifying "@Access Control ", be about to thereafter followed by bracket in note convert code for control of authority to.
In the method for existing artificial generation REST service,, if will carry out control of authority to the REST service of server, need in code corresponding to REST service, add dispersedly a large amount of codes that are used for control of authority.The method that adopts the embodiment of the present invention to provide, can generate the code for control of authority automatically, implements efficiency higher, and be difficult for makeing mistakes.And the final code that is used for control of authority is the method body that is added in each method, mixed in together with the code of additive method body, if existing employing manual type generates, be not easy identification and safeguard, and the automatic generation of adopting the embodiment of the present invention is used for the code of control of authority, need not to consider the unsharp problem of above-mentioned code structure of artificial generating code.
When a plurality of methods of a service interface have identical control of authority strategy, adopt the method for the embodiment of the present invention, can be by (namely explaining is to be added on Service interface definition adding the mode of explaining on service interface, outside the method body of service interface), a plurality of methods that namely can be this service interface arrange identical control of authority strategy.Then by the S202 step, automatically generate uniformly the identical code that is used for control of authority in each side's body of laws of class file corresponding to this service interface, avoided repeatedly writing same code by manual type in each method body, improved efficiency, and be not easy to make mistakes.So just realized same control of authority strategy, by a plurality of methods, reused.
Alternatively, step S203 comprises following two steps:
Server is corresponding REST service class to the class file that Java Virtual Machine loads each REST service class that generates, and each REST service class that loading is obtained encapsulates in Wink Application object;
For each REST service class, server, by the Wink middleware, utilizes the annotating information that is used for defining the REST service features in service interface corresponding to this REST service class, generates REST service corresponding to this REST service class.
Apache Wink(is designated hereinafter simply as " Wink middleware ") be the middleware that is used for generating the REST service of increasing income.In step S202, generated the class file of the REST service class corresponding with service interface, the class file of each REST service class that server will generate is loaded in Java Virtual Machine, generate corresponding REST service class, and each REST service class that loading obtains is encapsulated in Wink Application object.Class file in the REST service class that generates comprises be used to the note that defines the REST service features, and the Wink middleware just can utilize these fixed generation REST that explain to serve like this.Realize repeating no more here described in the code such as code snippet 1 of above-mentioned steps.
The procedure schematic diagram that the REST of the generation client Java API that Fig. 3 provides for the embodiment of the present invention realizes, as shown in Figure 3, the method comprises the steps:
S301: each service interface of client scan java application DLL (dynamic link library) API, obtain the interface message of each service interface, wherein this Java API is identical with the Java API on REST service place server that this client will be called;
S302: for each described service interface, client generates REST corresponding to this service interface according to the interface message of this service interface and realizes the class file of class, and class file comprises the code of the client-requested REST service related parameters of determining according to the note that is used for defining the REST service features in the interface message of this service interface;
S303: client loads each REST that generates and realizes the class file of class, and the REST that generates client Java API realizes.
In step S301, the Java API that why limits on the REST service place server that Java API and this client will call is identical, that client could be accessed the REST service of this server because only have the client and server end all based on identical Java API, to generate respectively Java API REST realization and the REST service of client.
Step S302 is used for defining the note of REST service features can be referring to table 1, and the method for the code of the client-requested REST service related parameters of determining according to the note that is used for defining the REST service features in the interface message of service interface is referring to example five.
Below, each service interface that scans Java API is described by example one, obtain the method for interface message corresponding to each service interface.
Each service interface of example one scan Java API, obtain interface message corresponding to each service interface
The method of example one is applicable to client and server simultaneously.For client, the executive agent of the method is client; For server, the executive agent of the method is server.Fig. 4 is the structural representation that is scanned the Java API of service interface in example one.For the process of each service interface of scanning Java API can simply clearly be described, Java API structure shown in Figure 4 structure for simplifying, often comprise tens service interfaces in actual Java API.The structure of Java API shown in Figure 4 is tree structure, and its root node service interface is Service Facede interface, its lower polymerization two service interfaces, be respectively Service1 interface and Service2 interface, method is separately arranged in each service interface.The structure of actual Java API is not limited to tree structure,, such as loop configuration etc.,, as long as have incidence relation between each service interface of Java API, can, by each service interface of scanning Java API, obtain interface message corresponding to each service interface.
In example one, from the root node service interface Service Facede interface of Java API, each service interface of scanning Java API, obtain interface message corresponding to each service interface.
The method of determining Java API root node service interface has a lot, such as, when server or client terminal start-up, by reading configuration file, determine Java API root node service interface.
Below,, take Java API shown in Figure 4 as example, illustrate the method for obtaining interface message corresponding to each service interface of Java API, as shown in Figure 5, the method comprises the steps:
S501, the service interface of root node service interface for scanning of determining described Java API;
S502, the service interface that will scan is scanned, obtain interface message corresponding to service interface;
If it is the method for interface that the interface message that the service interface that S503 obtains is corresponding comprises the rreturn value type, server determines according to the rreturn value of the method the service interface that the next one will scan, and returns to the step that the service interface that will scan is scanned; Otherwise end scanning process.
Alternatively, step S502 can utilize the Java reflex mechanism to realize.Utilize the Java reflex mechanism, Class class in the java.lang.reflect bag that use Java development kit (Java Development Kit, JDK) provides is obtained all methods of interface name, the note on Service interface definition and the service interface of service interface; Obtain note on the method name, method rreturn value type, method of service interface, method parameter type etc. with the Method class.
Specific practice is, load and obtain the java.lang.reflect.Class object from the java class loader, call this object method (as the method for table 2java.lang.reflect.Class), obtain all methods of interface name, the note on Service interface definition and the service interface of service interface.
Table 2
All methods of traversal service interface, by the method (as the method for table 3java.lang.reflect.Method) of calling the java.lang.reflect.Method object, obtain the rreturn value type of each method of parameter of each method of this service interface, the note on each method.
Table 3
Obtained each method of service interface by step S502., take the root node service interface Service Facede interface of the Java API in Fig. 4 as example, determine that by step S502 the rreturn value type of its method get Service1 is interface Service1; The rreturn value type of method get Service2 is Service2, and the service interface of so just having determined the Java API that next will scan is Service1 interface and Service2 interface., as long as the rreturn value type of certain method in service interface is interface, can determine the service interface that the next one will scan according to this rreturn value type.For two Service1 interfaces and Serivce2 interface, then each method by java.lang.reflect.Class object in table 2 obtains note, all methods of service interface on service interface title, Service interface definition respectively; Obtain note, parameter type on the method name, method rreturn value type, method of each method of service interface by each method of java.lang.reflect.Method object in table 3.
So circulation is gone down, until comprise that no longer the rreturn value type is the method (namely having obtained interface message corresponding to all service interfaces of Java API) of interface in interface message corresponding to the service interface that obtains.Need to prove, the structure of the structure of Java API in Fig. 4 for simplifying, may comprise tens service interfaces in actual Java API, as long as exist related between each service interface, so these service interfaces are carried out the iterative operation identical with top step,, until traveled through the service interface of all Java API, can obtain interface message corresponding to all service interfaces of this Java API.
In the embodiment of the present invention, the structure of Java API has been carried out simplifying processed, utilize incidence relation between each service interface of Java API realize the scanning of all service interfaces (such as, in example two, method rreturn value by service interface is determined the service interface that next will scan), overcome in the method that existing artificial generation REST service and REST realize, due to the complex structure of Java API with can't find the unified problem of obtaining interface message method corresponding to each service interface.
Below, by two pairs of embodiment of the present invention of example provide for each described service interface, the general approach that the interface message that server is corresponding according to this service interface generates the class file of REST service class corresponding to this service interface is described; By three pairs of embodiment of the present invention of example provide for each described service interface, client generates REST corresponding to this service interface according to the interface message of this service interface and realizes that the general approach of the class file of class is described; By four pairs of embodiment of the present invention of example provide for a specific Java API service interface, server generates the method for the class file of REST service class corresponding to this service interface; By five pairs of embodiment of the present invention of example provide for a specific Java API service interface, client generates REST corresponding to this service interface and realizes the method for the class file of class.
Example two is for each described service interface, and the interface message that server is corresponding according to this service interface generates the class file of REST service class corresponding to this service interface
Fig. 6 is the procedure schematic diagram of example two.As shown in Figure 6, the method comprises the steps:
S601, for each service interface, server generates the source code of REST service class corresponding to this service interface according to the interface message of this service interface of obtaining;
The source code of S602, REST service class that server is corresponding according to this service interface, generate the class file of REST service class corresponding to this service interface.
Alternatively, in step S602, the source code of REST service class that can service interface is corresponding compiles, and generates the class file of REST service class corresponding to this service interface.
Fig. 7 is the procedure schematic diagram of example three.As shown in Figure 7, the method comprises the steps:
S701, for each service interface, client generates REST corresponding to this service interface according to the interface message of this service interface of obtaining and realizes the source code of class;
S702, the REST corresponding according to this service interface realize the source code of class, and client generates the REST corresponding with this service interface and realizes the class file of class.
Alternatively, in step S702, the source code of REST service class that can service interface is corresponding compiles, and generates REST corresponding to this service interface and realizes the class file of class.
Below, illustrate respectively that by example four and example five server and client side generates the method for class file
Example four, server end generate class file
For purposes of simplicity of explanation, certain service interface in Java API is done following simplification.Interface is called Service1, and this service interface only has a method; @Path ,@Get, three notes of@Path Param are that JAX-RS explains ,@Access Control is for being used for the note of control of authority.The Service1 interface is defined as follows:
The Service1 interface code that code snippet 2. is simplified
Based on the interface message of obtaining to the Service1 interface scans, server generates the source code of the REST service class corresponding with the Service1 interface, and wherein the Service1Resource class is the REST service class corresponding with the Service1 interface; @Path ,@GET, three JAX-RS of@Path Param explain, and have been copied on the source code that generates; The note@Access Control that is used for control of authority, be converted into the anterior code that is used for control of authority of method body (get User Age).
The source code of the REST service class corresponding with the Serivce1 interface that generates comprises:
1) being used for is the code of method parameter type corresponding to local Java API with the type conversion of get User Age method parameter;
2) be used for interface message corresponding to the Service1 interface that obtains based on scanning before and generate the code that calls local Java API;
3) being used for is the code of the data type of REST service request with the type conversion of the execution result of local Java API method;
4) be used for returning the code of REST service response result;
5) be used for the code of control of authority.
The source code example of the REST service class corresponding with the Serivce1 interface that server generates is as follows, and wherein splicing resource in interface name back obtains the class title, and actual naming method is not limited to this, as long as different for class name corresponding to different interfaces.All classes are the public type; The note (such as JAX-RS explains) that is used for defining the REST service features is direct copying, Response.* refers to the rreturn value of method, the methodical rreturn value of institute is all with " Response. " beginning, and the method name is identical with the method name in the Service1 interface, and parameter is identical.
The source code of the corresponding REST service class of code snippet 3.Service1 interface
Then, the compilation of source code of the REST service class of above-mentioned generation is become class file.
When server end generates the code of REST service class, directly copy for the note that generates the REST service, do not do other processing.Treat in step S203, recycle these and explain the feature of the REST service of definition generation.When the note that is used for generation REST service was JAX-RS note, available@PATH explained the HTTP path of the access of definition resource, available@Path Param, the parameter of@Query Param note definition REST services request etc., but detailed directions reference table 1.Need to prove, in this example, occurred@Path Param (" userId ") in code, is to explain with@Path Param a part that has defined URL, is the parameter value of method, and the source code that not generates the REST service class is necessary.
Example five, client generate class file
Still, take the Service1 service interface in example four as example, generate REST corresponding to Service1 interface and realize class, REST realizes that in the source code of class, the Service1ClientImpl class has realized the Service1 interface; The Service1ClientImpl.getUserAge method, calling of Java API converted to the REST services request of sending to server, and will be converted into from the REST service response of server the rreturn value of Service1ClientImpl.get User Age method, make like this client can call get User Age method, and need not be concerned about the REST service whether it has called server.
The REST corresponding with the Service1 service interface that generates realizes that the source code of class comprises:
1) be used for obtaining the code of server address, wherein server address can read from the client configuration file;
2) be used for the URL address of the REST service of generation server, and the code of the URL address access services device end service that utilize to generate (according to the note that is used for defining the REST service features on the get User Age method that scans before (such as: JAX-RS explains) generate), wherein, the user ID userId of client has been put into URL;
3) be used for that the parameter type of method get User Age is converted to the REST services request and require the code of type;
4) be used for initiating the REST services request to server, obtain the code of REST service response;
5) be used for REST service response parameter type is converted to the code of method get User Age rreturn value type;
6) be used for the code that the result after type conversion is returned as method get User Age rreturn value.
The REST corresponding with the Service1 service interface that generates realizes that the example code of class is as follows:
The REST that code snippet 4.Service1 interface is corresponding realizes the code of class
Generating during client REST realizes the process of category code, the note that is used for defining the REST service features in the interface message that scanning is obtained is processed.Take example five as example, when the note for generating the REST service is JAX-RS note, the ULR address String rest ServiceURL=server Host+ "/users " that the REST that utilizes@PATH to explain@Path ("/users ") generation server serves+userId+ "/age "; Using PathParam to explain certain part that has indicated the URL value is the value of parameter, and parameter value has been put into URL; The mode of using@GET to explain to determine to generate the REST request (GET POST PUT DELETE), when client call REST serves, if there is@GET to explain on the discovery corresponding method, just use HTTP GET request in generating code, just use the get method in code.In like manner, if@POST explains, just use the post method, by that analogy.Other are similar to the disposal route principle of explaining, and repeat no longer one by one here.
The method that generates class file in example two, example three, example four and example five is to generate source code by the mode that first adopts the character string splicing, then according to source code, generates corresponding class file.Wherein, the implication of " character string splicing " is explained as follows:
The character string splicing, be a plurality of character strings connected into a character string.Huang is total in the java applet design,, by creating character string buffer area (String Buffer) object code, then uses such as following code:
code.append(“public”)
code.append(“class”)
code.append(“Service1ClientImpl{”)
Take example four as example, all character strings of the Service1 interface code in the code snippet of example 43 sequentially are spliced in character string cache object code, finally obtain the code snippet 3 in example four.
In the Java language program development, the content of text (Java source code) when the .java file is exploitation, it after the compiler compiling, generates binary class file (being the .class file).The * .java file that class file is corresponding with it is equivalent, and different is, in class file, the code in the .java file has been carried out binary compiling.Simultaneously, class file can be directly performed by Java Virtual Machine, and all .java source codes all need to be compiled into class file and just can be performed.
Adopt Java bytecode operative technique, need not to write source code, generation source code in example four and example five and the step of compiling are united two into one, it adopts instruction and the rule of java virtual machine, mode by binary data generates the code identical with example five (client) with example four (server) (implication of " identical " refers to: the source code that generates in example four or example five is the binary class file that generates by Java bytecode operative technique after compiling) here, directly generates class file.Because the step 1 step that will generate source code and compiling completes, saved the time that code transforms, improved efficiency.In embodiments of the present invention, by character string, splice or Java bytecode operative technique, generate the class file of REST service class, solved the employing manual type and write code complexity, inefficient problem, and the code that generates is accurate.
Based on same inventive concept, a kind of server also is provided in the embodiment of the present invention, the principle of dealing with problems due to this server is similar to the method for the generation REST service that the embodiment of the present invention provides, therefore the enforcement of this server can, referring to the enforcement of the method that generates the REST service, repeat part and repeat no more.
The structural representation of the server that Fig. 8 provides for the embodiment of the present invention.As shown in Figure 8, this server specifically comprises:
Scan module 801, each service interface for scanning java application DLL (dynamic link library) API, obtain interface message corresponding to each service interface;
Class file generation module 802, be used for for each service interface, and the interface message corresponding according to this service interface generates the class file of REST service class corresponding to this service interface;
REST service creation module 803, be used for for each REST service class, be used for defining the note of REST service features in interface message according to the class file of this REST service class and service interface corresponding to this REST service class, generate REST service corresponding to this REST service class.
Alternatively, one or more service interfaces of Java API are for establishing the service interface of authority;
Class file generation module 802 also is used for: established the service interface of authority for each, this code that is used for control of authority of having established the rights service interface has been placed in this has established the class file of REST service class corresponding to rights service interface;
Wherein, this code that is used for control of authority of having established the rights service interface is that the note that is used for control of authority that this that obtain by described scan module established interface message corresponding to the service interface of authority is converted to.
Alternatively, scan module 801 specifically is used for: the service interface of root node service interface for scanning of determining Java API; The service interface that will scan is scanned, obtain interface message corresponding to service interface; , if it is the method for interface that the interface message that the service interface that obtains is corresponding comprises the rreturn value type, according to the rreturn value of the method, determines the service interface that next will scan, and return to the step that the service interface that will scan is scanned; Otherwise end scanning process.
Alternatively, the service interface that will scan is scanned, obtaining interface message corresponding to service interface comprises: load the object of Java reflection class from the java class loader, use the method for Java reflection class object to obtain the interface message corresponding to service interface that will scan.
Alternatively, class file generation module 802 specifically is used for:, for each described service interface, generate the source code of REST service class corresponding to this service interface according to the interface message of this service interface that obtains; The source code of the REST service class corresponding according to this service interface, generate the class file of REST service class corresponding to this service interface.
Alternatively, class file generation module 802 specifically is used for: for each described service interface, the interface message corresponding according to this service interface, utilize Java bytecode operative technique, generates the class file of REST service class corresponding to this service interface.
REST service creation module 803 specifically is used for: the class file that loads each REST service class that generates to Java Virtual Machine is corresponding REST service class, and each REST service class that loading is obtained encapsulates in Wink Application object; For each REST service class,, by the Wink middleware, utilize the annotating information that is used for defining the REST service features in service interface corresponding to this REST service class, generate REST service corresponding to this REST service class.
Based on same inventive concept, a kind of client also is provided in the embodiment of the present invention, the principle of dealing with problems due to this client is similar to the method that the generation REST that the embodiment of the present invention provides realizes, therefore the enforcement of this client can, referring to the enforcement of the method that generates the REST realization, repeat part and repeat no more.
Fig. 9 is the structural representation of the client of the embodiment of the present invention.As shown in Figure 9, this client specifically comprises:
Scan module 901, be used for each service interface of scanning java application DLL (dynamic link library) API, obtains the interface message of each service interface, and wherein Java API is identical with the Java API that REST that this client will be called serves on the server of place;
Class file generation module 902, be used for for each service interface, generate REST corresponding to this service interface according to the interface message of this service interface and realize the class file of class, class file comprises the code of the client-requested REST service related parameters of determining according to the note that is used for defining the REST service features in the interface message of this service interface;
REST realizes generation module 903, is used for loading each REST that generates and realizes the class file of class, and the REST that generates client Java API realizes.
Alternatively, scan module 901 specifically is used for: the service interface of root node service interface for scanning of determining Java API; The service interface that will scan is scanned, obtain interface message corresponding to service interface; , if it is the method for interface that the interface message that the service interface that obtains is corresponding comprises the rreturn value type, according to the rreturn value of the method, determines the service interface that next will scan, and return to the step that the service interface that will scan is scanned; Otherwise end scanning process.
Alternatively, the service interface that scan is scanned, obtain interface message corresponding to service interface and comprise; Load the object of Java reflection class from the java class loader, use the method for Java reflection class object to obtain the interface message corresponding to service interface that will scan.
Alternatively, class file generation module 902 specifically is used for:, for each described service interface, generate REST corresponding to this service interface according to the interface message of this service interface that obtains and realize the source code of class; Realize the source code of class according to REST corresponding to described this service interface, generate the REST corresponding with this service interface and realize the class file of class.
Alternatively, class file generation module concrete 902 is used for: for each described service interface, the interface message corresponding according to this service interface, utilize Java bytecode operative technique, generates the REST corresponding with this service interface and realize the class file of class.
Those skilled in the art should understand, embodiments of the invention can be provided as method, system or computer program.Therefore, the present invention can adopt complete hardware implementation example, implement software example or in conjunction with the form of the embodiment of software and hardware aspect fully.And the present invention can adopt the form that wherein includes the upper computer program of implementing of computer-usable storage medium (including but not limited to magnetic disk memory, CD-ROM, optical memory etc.) of computer usable program code one or more.
The present invention describes with reference to process flow diagram and/or the block scheme of method, equipment (system) and computer program according to the embodiment of the present invention.Should understand can be by the flow process in each flow process in computer program instructions realization flow figure and/or block scheme and/or square frame and process flow diagram and/or block scheme and/or the combination of square frame.Can provide these computer program instructions to the processor of multi-purpose computer, special purpose computer, Embedded Processor or other programmable data processing device to produce a machine, make the instruction of carrying out by the processor of computing machine or other programmable data processing device produce to be used for the device of realizing in the function of flow process of process flow diagram or a plurality of flow process and/or square frame of block scheme or a plurality of square frame appointments.
These computer program instructions also can be stored in energy vectoring computer or the computer-readable memory of other programmable data processing device with ad hoc fashion work, make the instruction that is stored in this computer-readable memory produce the manufacture that comprises command device, this command device is realized the function of appointment in flow process of process flow diagram or a plurality of flow process and/or square frame of block scheme or a plurality of square frame.
These computer program instructions also can be loaded on computing machine or other programmable data processing device, make on computing machine or other programmable devices and to carry out the sequence of operations step to produce computer implemented processing, thereby be provided for realizing the step of the function of appointment in flow process of process flow diagram or a plurality of flow process and/or square frame of block scheme or a plurality of square frame in the instruction of carrying out on computing machine or other programmable devices.
Although described the preferred embodiments of the present invention, in a single day those skilled in the art obtain the basic creative concept of cicada, can make other change and modification to these embodiment.So claims are intended to all changes and the modification that are interpreted as comprising preferred embodiment and fall into the scope of the invention.
Obviously, those skilled in the art can carry out various changes and modification and not break away from the spirit and scope of the present invention the present invention.Like this, if within of the present invention these are revised and modification belongs to the scope of the claims in the present invention and equivalent technologies thereof, the present invention also is intended to comprise these changes and modification interior.