CN115455239B - Conversion method and device for JAVA class instance and target format data - Google Patents

Conversion method and device for JAVA class instance and target format data Download PDF

Info

Publication number
CN115455239B
CN115455239B CN202211407117.2A CN202211407117A CN115455239B CN 115455239 B CN115455239 B CN 115455239B CN 202211407117 A CN202211407117 A CN 202211407117A CN 115455239 B CN115455239 B CN 115455239B
Authority
CN
China
Prior art keywords
target
annotation
data
attribute
attribute member
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Active
Application number
CN202211407117.2A
Other languages
Chinese (zh)
Other versions
CN115455239A (en
Inventor
申志强
田玉兵
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Beijing Yicheng Interactive Network Technology Co ltd
Original Assignee
Beijing Yicheng Interactive Network Technology Co ltd
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by Beijing Yicheng Interactive Network Technology Co ltd filed Critical Beijing Yicheng Interactive Network Technology Co ltd
Priority to CN202211407117.2A priority Critical patent/CN115455239B/en
Publication of CN115455239A publication Critical patent/CN115455239A/en
Application granted granted Critical
Publication of CN115455239B publication Critical patent/CN115455239B/en
Active legal-status Critical Current
Anticipated expiration legal-status Critical

Links

Images

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F16/00Information retrieval; Database structures therefor; File system structures therefor
    • G06F16/80Information retrieval; Database structures therefor; File system structures therefor of semi-structured data, e.g. markup language structured data such as SGML, XML or HTML
    • G06F16/83Querying
    • G06F16/835Query processing
    • G06F16/8358Query translation
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F16/00Information retrieval; Database structures therefor; File system structures therefor
    • G06F16/80Information retrieval; Database structures therefor; File system structures therefor of semi-structured data, e.g. markup language structured data such as SGML, XML or HTML
    • G06F16/84Mapping; Conversion

Abstract

The embodiment of the invention provides a method and a device for converting JAVA class instances and target format data, wherein the method comprises the following steps: a process for converting an instance of JAVA classes into one of at least one target format of data; a process for converting an instance of said JAVA class into one of at least one target format of data, comprising: acquiring an instance and a target mode of the JAVA class; for each attribute member with target annotation modification in the JAVA class instance, acquiring a target annotation matched with the target mode from at least one target annotation modifying the attribute member as a current target annotation corresponding to the attribute member; and converting the attribute member into data of a target format corresponding to a conversion mode in the current target annotation according to the current target annotation corresponding to the attribute member.

Description

Conversion method and device for JAVA class instance and target format data
Technical Field
The invention relates to the field of data exchange, in particular to a conversion method and a conversion device for JAVA class instances and target format data.
Background
JSON (or JSON, javaScript Object notification) is a lightweight data interchange format. The method is easy for people to read and write, can exchange data among multiple languages, and has more realization of the multiple languages; and the Json format is widely used as a standard communication message in micro-service architecture in many financial and business application fields. There are many Json-supporting toolkits, and there are many Json-supporting toolkits for the Java language, such as: gson, fastJson, jackson, json-lib. Google's json parser Gson is a common tool class in Android/Java. The currently existing Json toolkit generally directly performs mechanical conversion, cannot meet the requirements of special service scenes, and is not flexible enough.
In the process of implementing the invention, the applicant finds that at least the following problems exist in the prior art:
the existing instances corresponding to JAVA classes only support data that is converted to one target format.
Disclosure of Invention
The embodiment of the invention provides a method and a device for converting a JAVA class instance and target format data, which solve the problem that the existing instance corresponding to JAVA class only supports data converted into one target format.
To achieve the above object, in one aspect, an embodiment of the present invention provides a method for converting JAVA class instances and target format data, including: a process for converting an instance of JAVA classes into one of at least one target format of data;
a process for converting an instance of said JAVA class into one of at least one target format of data, comprising:
acquiring an instance and a target mode of the JAVA class;
for each attribute member with target annotation modification in the JAVA class instance, acquiring a target annotation matched with the target mode from at least one target annotation modifying the attribute member as a current target annotation corresponding to the attribute member; and the number of the first and second electrodes,
converting the attribute member into data of a target format corresponding to a conversion mode in the current target annotation according to the current target annotation corresponding to the attribute member;
wherein the JAVA classes comprise: at least one attribute member having at least one target annotation modification; the target annotation is a pre-defined instance of an annotation class; the target annotation comprises: the switching mode; all target annotations having the same conversion pattern define conversion rules between instances of JAVA classes and data in a target format corresponding to the conversion pattern.
On the other hand, an embodiment of the present invention provides a conversion apparatus for JAVA class instances and target format data, including: a JAVA class instance to target format unit for implementing a process of converting an instance of a JAVA class to one of the at least one target format of data;
the JAVA class instance to object format unit comprises:
the first parameter acquisition module is used for acquiring the instances and the target modes of the JAVA classes;
a first target annotation obtaining module, configured to, for each attribute member having a target annotation modification in an instance of the JAVA class, obtain, from at least one target annotation modifying the attribute member, a target annotation matching the target pattern as a current target annotation corresponding to the attribute member;
the object format data generation module is used for converting the attribute members into the data of the object format corresponding to the conversion mode in the current object annotation according to the current object annotation corresponding to the attribute members;
wherein the JAVA classes comprise: at least one attribute member having at least one target annotation modification; the target annotation is a pre-defined instance of an annotation class; the target annotation includes: the switching mode; all target annotations having the same conversion pattern define conversion rules between instances of JAVA classes and data in a target format corresponding to the conversion pattern.
The technical scheme has the following beneficial effects: by using the custom annotation, attribute members in the JAVA class are modified, so that the JAVA class support is converted into various target formats, particularly various JSON formats, and the development efficiency is improved; further, a data verification method in the conversion process is provided, and the data correctness is ensured; further, multiple formatting support for data during conversion is supported, especially for time of day data.
Drawings
In order to more clearly illustrate the embodiments of the present invention or the technical solutions in the prior art, the drawings used in the description of the embodiments or the prior art will be briefly described below, it is obvious that the drawings in the following description are only some embodiments of the present invention, and for those skilled in the art, other drawings can be obtained according to the drawings without creative efforts.
FIG. 1 is a flow chart of a method for converting a JAVA class instance and target format data according to one embodiment of the present invention;
FIG. 2 is a diagram of the architecture of a conversion apparatus for JAVA class instances and target format data according to one embodiment of the present invention;
FIG. 3 is a flow diagram of an annotation information pooling process in accordance with one embodiment of the present invention;
FIG. 4 is a flowchart of the conversion of an instance of a JAVA class to Json target format data according to one embodiment of the present invention;
FIG. 5 is a flow chart of the conversion of Json target format data to JAVA class instances according to one embodiment of the present invention.
Detailed Description
The technical solutions in the embodiments of the present invention will be clearly and completely described below with reference to the drawings in the embodiments of the present invention, and it is obvious that the described embodiments are only a part of the embodiments of the present invention, and not all of the embodiments. All other embodiments, which can be derived by a person skilled in the art from the embodiments given herein without making any creative effort, shall fall within the protection scope of the present invention.
On one hand, as shown in fig. 1, an embodiment of the present invention provides a method for converting JAVA class instances and target format data, including: a process of converting an instance of a JAVA class into one of at least one target format of data;
a process for converting an instance of said JAVA class into one of at least one target format of data, comprising:
step S11: acquiring an instance and a target mode of the JAVA class;
step S12: for each attribute member with target annotation modification in the JAVA class instance, obtaining a target annotation matched with the target pattern from at least one target annotation modifying the attribute member as a current target annotation corresponding to the attribute member; and also,
step S13: converting the attribute member into data of a target format corresponding to a conversion mode in the current target annotation according to the current target annotation corresponding to the attribute member;
wherein the JAVA classes comprise: at least one attribute member having at least one target annotation modification; the target annotation is a pre-defined instance of an annotation class; the target annotation includes: the conversion mode; all target annotations having the same conversion pattern define conversion rules between instances of JAVA classes and data in the target format corresponding to the conversion pattern.
In some embodiments, an annotation class is customized in advance, and according to a conversion requirement from a JAVA class to a target format, one or more annotation attributes may be specifically defined in the annotation class, where an instance of the annotation class, that is, a target annotation, may include part or all of the annotation attributes in the annotation class, where the annotation attributes include a conversion mode; a transformation pattern is present in each target annotation to identify different target annotations; different target formats are identified through the annotation attributes and specific conversion constraint rules are provided for attribute members in the JAVA class. Each attribute member can have one or more target annotation modifications, and each target annotation can independently set the annotation attribute used by the target annotation member to realize the conversion of the attribute member to various target formats, wherein the conversion mode is an annotation attribute of the target annotation. The target annotations can be distinguished through the conversion mode, and the target annotations used by the conversion are selected through the matching of the target mode and the conversion mode in the target annotations, so that the current target annotations corresponding to the attribute members are determined.
The annotation class @ Json is explained below in the case where the target format is the Json format:
the implementation fragment of the @ Json annotation class is as follows, and a Json annotation interface is firstly constructed to describe the tag and the attribute:
@Documented
@Retention(RUNTIME)
@Target({TYPE,FIELD})
@Repeatable(Jsons.class)
public @interface Json {
string tagMode () default;/conversion mode for corresponding to a plurality of json formats
String Json Key () default;// Json's node Key, json node name, i.e. target Key name
......
MAX _ VALUE, maximum VALUE or maximum string length, i.e. the maximum VALUE of a range of VALUEs
borolan isConver () default true// switch on/off, i.e. change-over switch
......
}
In order to repeatedly define the package property according to the array in the container, the annotation container interface needs to be constructed, and the public @ interface Jsons comprises the following code segments:
@Documented
@Retention(RUNTIME)
@Target({TYPE,FIELD})
public @interface Jsons {
Json[] value();
}
according to the codes, compiling is completed, namely annotation can be performed on the attributes of the class which needs to be converted into Json by the special definition attributes, and the annotation sample codes are used as follows:
public class JsonTest {
@Json(tagMode = "mode1", jsonKey = "id", jsonType = "int")
@ Json (tagMode = "mode2", jsonnType = "String", request = true, min = 8, max = 8, regular = "[ 0-9\ - __ 8, }, $", regular Msg = "must be an 8-digit number")
String code;
......
}
The code column above shows an example of an attribute code (equivalent to attribute member) binary annotation translation for the JsonTest class (equivalent to JAVA class):
mode1 is converted, and the code attribute member is output as JSON (JSON or Json) target format, the corresponding key name of the code attribute member in the JSON target format is "id" (i.e. JSON Key = "id"), and "id" in the Json target format is numerical type (i.e. JSON type = "int"). A conversion mode2, which outputs the code attribute member to another json target format, and because the json key field is not defined in the target annotation corresponding to the conversion mode2, the key name of the code attribute member in the json target format is consistent with the name of the code attribute member in the json test class, namely, the code is still used as the key name in the json target format; mandatory switching and validation information (i.e., min = 8, max = 8, regular = "^ [0-9\ - _________ 8, } $", regular msg = "must be 8-digit numbers") is also defined in the target annotation for conversion mode2, requiring that both the code attribute members and their corresponding Json target formats for conversion mode2 cannot be empty and must be an 8-digit string.
In the above example, tagMode and jsonKey are annotation attributes in the @ Json annotation class, where @ Json (tagMode = "mode1",..) and @ Json (tagMode = "mode2",.) are two target annotations, and the conversion modes of the two target annotations are respectively mode1 and mode2, and each target annotation may contain the same or different annotation attributes when the annotation class is instantiated into each target annotation according to specific needs. In the above example, an attribute member code is defined in JAVA (i.e. JAVA or JAVA) class JsonTest, and two instances of the @ Json annotation class (i.e. target annotations) are used to modify the attribute member code, and as can be seen from the annotations, the attribute member code can be converted into two target formats corresponding to tagMode = "mode1" and tagMode = "mode2", respectively, in the target format corresponding to mode1, the key name of the attribute member code in the target format is set to "id" by annotating the attribute jsonKey, and the key value of the attribute member code in the target format is set to reshape by annotating the attribute jsonType = "int"; in the target format corresponding to mode2, the name of the attribute member in the JAVA class, that is, "code", is directly used in the target format as the key name in the target format without setting the value of jsonKey, the attribute member code is expressed in the target format in the form of a character String by jsonType = "String", and the authentication information of the attribute member code is also defined by min, max, and regular.
In the above embodiments and the following embodiments, the Json object format is used for description, but the object format used in the embodiments of the present invention is not limited to, and the object format in the embodiments of the present invention includes but is not limited to, instances of classes in Json, XML, or object-oriented programming language, and the like.
When the embodiment is used for converting the instances of the JAVA classes into one of the data in at least one target format, the instances of the JAVA classes which are expected to be converted and the expected target mode need to be provided; the target pattern is matched with a conversion pattern in the target annotation so as to determine the current target annotation, and the matching comprises an accurate matching and a default matching; the accurate matching means that the target mode is the same as the conversion mode; the default matching is to use the target note with the conversion mode value of null character string as the default selection when the exact matching can not be completed in all the target notes of the attribute member, or use the target note with the specified conversion mode as the default selection when the exact matching can not be completed in all the target notes of the attribute member; the resulting default selection is used as the current target annotation.
The method further comprises the following steps: for attribute members without target annotation modification in an instance of a JAVA class, converting the character names and data types defined by the attribute members in the JAVA class into corresponding data in a target format.
The embodiment of the invention has the following technical effects: by modifying the attribute members of the JAVA class by using the target annotation and converting the attribute members of the JAVA class into the target format corresponding to the target mode according to the target annotation and the target mode by using the method of the technical scheme of the invention, the conversion of the same JAVA class support to various target formats is realized. Furthermore, the application also provides the precise matching and default matching of the target mode and the conversion mode, so that the conversion mode which does not exist in the current JAVA class can be specified when the method is called, but the method can convert the target mode into the target annotation by using the default matching; when the calling end system needs other target formats, the calling end system does not need to be modified, and only a new target annotation needs to be added into the JAVA class, so that the target mode used by the call can be accurately matched with the newly added target annotation, and the modification closure is realized. The technical scheme of the invention realizes the conversion of the same JAVA class to various target formats, and improves the development efficiency and the maintenance convenience.
Further, the target annotation further comprises: a transfer switch, a target key name, a target data type, a formatting scheme, and/or a must-go switch;
wherein the target key names set in different target annotations modifying the same attribute member of the JAVA class are the same or different;
the converting the attribute member into the data of the target format corresponding to the conversion mode in the current target annotation according to the current target annotation corresponding to the attribute member includes:
acquiring an attribute member list of the JAVA class; the attribute member list includes: all attribute members with target annotation modification and data types thereof in the JAVA class;
for each attribute member in the attribute member list, if the change-over switch is arranged in the current target annotation corresponding to the attribute member and the value of the change-over switch is not changed, removing the attribute member and the data type thereof from the attribute member list to obtain an attribute member list to be changed;
for each attribute member in the attribute member list to be converted, acquiring the attribute member from the instance of the JAVA class, and converting the attribute member into intermediate data corresponding to the attribute member according to the target key name, the target data type and/or the formatting mode set in the current target annotation corresponding to the attribute member;
and converting the intermediate data into data in a target format corresponding to the current target annotation.
In some embodiments, the number and types of annotation attributes contained in different target annotations modifying the same attribute member may be the same or different, and the value of the same annotation attribute may also be the same or different between different target annotations. The attribute member list of the JAVA class can be retrieved from the JAVA class during each conversion, specifically, all attribute members with target annotation modification and corresponding data types of the JAVA class can be obtained through a reflection mechanism, and the obtained attribute members and the corresponding data types are organized into the attribute member list; or, for the execution efficiency of the subsequent conversion, when the JAVA class is read for the first time, all attribute members with target annotation modification and corresponding data types of the JAVA class may be acquired through a reflection mechanism, and the acquired attribute members and corresponding data types are stored in the attribute member cache in the form of an attribute member list, and the attribute member list corresponding to the JAVA class may be retrieved from the attribute member cache through the packet name + class name of the JAVA class. The intermediate data is used as transition data between the JAVA class and the target, and is converted into the intermediate data and then further converted into the target format under the condition that a conversion tool for the intermediate data and a JAVA class instance and/or a conversion tool for the intermediate data and the target format data exist, so that the existing tool can be fully utilized, and repeated development is avoided. In some requirement scenarios, different names need to be used in different target formats for the same attribute member, and for this requirement, multiple target annotations may be defined in the attribute member, and different target key names are used in each target annotation. For different requirements, part of attribute members may be unnecessary in some data in the target format, and at this time, by setting a switch in the corresponding target annotation and setting the value of the switch to be not converted, when converting the attribute members of the JAVA class to the data in the target format, the attribute members corresponding to the attribute members set to be not converted are removed from the attribute member list. The conversion is required for attribute members that have the switch set to convert or have no target annotation modification set to the switch. During specific conversion, converting the attribute member into intermediate data corresponding to the attribute member according to the target key name, the target data type and/or the formatting mode set in the current target annotation modifying the attribute member, wherein the intermediate data is used for caching a result obtained by the next conversion, or is used for utilizing an existing conversion tool from the intermediate data to a target format of a third party, for example, the intermediate data includes but is not limited to a Map-based data type in a JAVA language, and the Map-based data type includes but is not limited to Map and/or HashMap; and converting the attribute member into a data type based on Map corresponding to the attribute member according to the target key name, the target data type and/or the formatting mode set in the current target annotation modifying the attribute member, wherein when the target format is a Json format, the conversion from Map to Json by using the existing third party can be realized by using a tool including but not limited to a Gson toJson method and the like.
The embodiment of the invention has the following technical effects: the same or different target key names are used in the target annotations, so that the same attribute member in the JAVA class is allowed to be converted into different key names and key values of different data types, and the same JAVA class instance can adapt to the requirements under different application scenes. Furthermore, through the change-over switch, whether the attribute members in the JAVA class are changed in the data of each target format can be flexibly controlled; further, by generating intermediate data, such as intermediate data based on the Map data type when the target format is the Json format, existing third-party tools are fully utilized to convert the intermediate data into data in the target format.
Further, the obtaining, for each attribute member in the attribute member list to be converted, the attribute member from the instance of the JAVA class, and converting the attribute member into intermediate data corresponding to the attribute member according to the target key name, the target data type, and/or the formatting manner set in the current target annotation corresponding to the attribute member includes:
if the current target annotation corresponding to the attribute member is provided with the target key name, using the target key name as an intermediate data key name corresponding to the attribute member, and if the current target annotation corresponding to the attribute member is not provided with the target key name, using a character name used by the attribute member in the JAVA class as the intermediate data key name corresponding to the attribute member;
if the current target annotation corresponding to the attribute member is provided with the target data type, taking data obtained after the attribute member is converted into the target data type as an intermediate data value corresponding to the attribute member, and if the current target annotation corresponding to the attribute member is not provided with the target data type, taking data of the attribute member in the JAVA class instance as the intermediate data value corresponding to the attribute member; alternatively, the first and second electrodes may be,
if the formatting mode is set in the current target annotation corresponding to the attribute member, taking the data formatted by the attribute member according to the formatting mode as an intermediate data value corresponding to the attribute member;
wherein, the intermediate data corresponding to the attribute member comprises: and the intermediate data key name and the intermediate data value corresponding to the attribute member.
The following describes an embodiment of the present invention by taking a Json format (equivalent to a target format) to which JAVA class instances are converted as an example:
for the basic type in the JAVA class or the attribute member of the basic class, converting through assignment operation or the BenUtils toolkit of Apache;
the Java (i.e., java) base types are: the types which do not need to be split again from the perspective of business data are called basic conversion types during data conversion, the types which do not need to be split again during data conversion are treated as minimum elements during data conversion, and the types which do not need to be split continuously are all Java basic classes except for Java basic types.
The BeanUtils toolkit of Apache can be used for conversion of Java base types and base classes, and the BeanUtils of Apache realizes automatic conversion for String and base types and base classes; conversion can be performed by means of a beans.ConvertetUtils tool or automatically by means of a beans.setProperty assignment;
for String converting date and time type, common format of date and time can be listed separately, and the character String is converted into date and time format by automatically recognizing type of time format of character String date and time format. And the converter is constructed to register as a String-to-date-time converter through ConvertUtils, so that automatic conversion is realized.
For class attribute members that are date type, conversion to a string is required; the definition of the formatting mode of the annotation attribute can be searched in the current target annotation corresponding to the attribute member to obtain a date-time conversion format, if the definition of the formatting mode exists, the conversion is carried out according to the definition of the formatting mode, and if the definition of the formatting mode is not found, the conversion is carried out by adopting 'yyyyy-MM-dd HH: MM: ss' as a default format.
Description of the drawings: setproperty, when used by beans, setproperty, requires that classes must have a set method of properties. getProperty needs a get method that a class must have attributes, corresponding set and get methods must exist, otherwise abnormal information is thrown out, the get and set methods can be automatically increased by using similar plug-ins such as a lombok plug-in, and the code amount is reduced.
Further, the target annotation further comprises: verifying the information;
before converting the attribute member into data of a target format corresponding to a conversion mode in the current target annotation according to the current target annotation corresponding to the attribute member, the method further includes:
and verifying the attribute members according to the verification information in the current target annotations corresponding to the attribute members, and prompting errors discovered by verification.
In some embodiments, the value of the attribute member is verified through the verification information set in the target annotation, and the error found by verification is prompted, and the verification prompt information may also be defined in the target annotation. Thereby ensuring the correctness of the JAVA class instance and the target format data.
Further, the verification information includes: numerical ranges and/or regular expressions;
the verifying the attribute member according to the verification information in the current target annotation corresponding to the attribute member and prompting the error found by verification includes:
if the current target annotation corresponding to the attribute member is provided with a must-output switch, and the must-output switch is must-output, verifying whether the data of the attribute member is empty, and if the verification result is that the data of the attribute member is empty, throwing a first exception; and/or the presence of a gas in the gas,
if the numerical range is set in the current target annotation corresponding to the attribute member, judging whether the data of the attribute member is in the numerical range according to the data type of the attribute member, and if the data of the attribute member is not in the numerical range, throwing a second exception; and/or the presence of a gas in the atmosphere,
if the regular expression is set in the current target annotation corresponding to the attribute member, verifying the data of the attribute member according to the regular expression, and if the verification result is that the data of the attribute member does not accord with the regular rule of the regular expression, throwing a third exception;
wherein the data types include: a numeric value type and/or a string type;
if the current target annotation corresponding to the attribute member is provided with the numerical range, judging whether the data of the attribute member is in the numerical range according to the data type of the attribute member, and if the data of the attribute member is not in the numerical range, throwing a second exception, wherein the method comprises the following steps:
if the data type of the attribute member is a numerical value type, judging whether the numerical value of the data of the attribute member is in the numerical value range, and if the numerical value of the data of the attribute member is not in the numerical value range, throwing the second exception;
and if the data type of the attribute member is a character string type, judging whether the character length of the data of the attribute member is in the numerical range, and if the character length of the data of the attribute member is not in the numerical range, throwing the second exception.
The following is specifically described with respect to the verification of the conversion between JAVA class instances and Json target format:
the verification here is to verify the attribute members of the instances of JAVA classes (obtained by reflection);
the authentication information includes: a numerical range, a regular expression (i.e., regular), and/or regular verification information (i.e., regular msg); the numerical range consists of a minimum value (i.e., min) and a maximum value (i.e., max);
acquiring corresponding verification information of min, max, regular and regular Msg and a change-over switch (namely request) in the current target annotation;
if the verification information exists in the current target annotation matched with the target mode, verifying the data value of the attribute member of the Java (namely JAVA) class corresponding to the current target annotation:
if the value of the must-go switch in the current target annotation is must-go, but the data of the attribute member in the instance of the JAVA class is empty, throwing a first exception;
if the attribute members in the JAVA class instances are numerical type, judging whether the numerical value of the data of the attribute members in the JAVA class instances is in the numerical range of the minimum value min and the maximum value max, and if not, throwing a second exception;
if the attribute member in the JAVA class instance is a character string, judging whether the character length of the data of the attribute member in the JAVA class instance is within the numerical range of a minimum value min and a maximum value max, and if not, throwing a second exception;
using a regular expression in the current target annotation to perform regular judgment on data of the attribute members of the Java class instances, and throwing a third exception if the data of the attribute members of the Java class instances do not accord with the regular rule;
the first exception, the second exception, and the third exception may be set to the same or different exception information;
preferably, the first exception can be prompted by using 'the value of the attribute X cannot be null' as a template;
the second exception may be prompted for the template by "the value of attribute X must be in the range of (minValue) and (maxValue)" or "the length of attribute X must be in the range of (minLen) and (maxLen)";
regular verification information in the verification information may be used as a hint for the third exception.
Wherein, X is replaced by the name of the specific attribute member with abnormal verification; minValue and minLen are replaced with the value of the minimum value min in the verification information, and maxValue and maxLen are replaced with the value of the maximum value max in the verification information
Replacing the numerical value of (c);
the embodiment of the invention has the following technical effects: the data verification in the framework in the prior art can only perform one fixed verification aiming at the same attribute member, cannot provide the same attribute of the same class, and the verification of various different verification rules exists in different use scenes.
Further, the obtaining the instances and the target patterns of the JAVA classes includes:
when the instance of the JAVA class is obtained for the first time, all target annotations corresponding to all attribute members modified by the target annotations are obtained from the JAVA class to form annotation information corresponding to the JAVA class, and the annotation information is stored in a preset annotation cache;
the obtaining, for each attribute member having a target annotation modification in the instance of the JAVA class, a target annotation matching the target pattern from at least one target annotation modifying the attribute member as a current target annotation corresponding to the attribute member includes:
obtaining annotation information corresponding to the JAVA class from the annotation cache;
for each attribute member with target annotation modification in the instance of the JAVA class, finding a target annotation which is matched with the target pattern in at least one target annotation modifying the attribute member from the annotation information as a current target annotation corresponding to the attribute member;
wherein the annotation information comprises all attribute members having a target annotation modification and all target annotations that correspondingly modify the attribute members.
In some embodiments, in order to improve the efficiency of conversion between JAVA class instances and target format data, when a JAVA class instance is obtained for the first time or when a JAVA class is loaded for the first time, all target annotations defined in the JAVA class are pooled and cached in a preset annotation cache, and in the later use process, the required target annotations can be directly and quickly searched from the annotation cache, so that corresponding target annotations are not repeatedly obtained again from the JAVA class through mechanisms such as reflection and the like; the efficiency of reading from the cache is obviously higher than that of mechanisms such as reflection and the like, and the execution efficiency of the system can be improved.
The following is a description with specific examples:
setting the setting information of special conversion on the attribute members of the Java classes through target annotation, and reading the annotation information on the Java classes for reading and using during conversion when completing the conversion of one Java class supporting and a plurality of Json formats simultaneously; in order to improve efficiency, in a JVM (i.e. JAVA virtual machine), definition information of an annotation only needs to be stored once, and is not changed during the operation of the JVM, so that the annotation information pool for constructing the conversion class stores configuration information, and the configuration information does not need to be loaded again after being loaded once. As shown in fig. 3, the annotation loading pooling module reads the annotations of the Java class and converts the annotations into pooling information of the conversion schema definition, and the conversion schema definition information of the classes is saved in an information pool (i.e. a preset annotation cache). The annotation cache of the preset annotation cache comprises annotation information of one or more JAVA classes, and the annotation information of each JAVA class comprises: storing all target annotations corresponding to all attribute members with target annotation modification of the JAVA class in a key value pair mode; specifically, for each target annotation of any attribute member with target annotation modification in the JAVA class, the key name is the packet name + "-" + of the JAVA class + the class name + "-" + of the attribute member; the corresponding value is the specific content of the target annotation of the attribute member of the JAVA class.
To implement annotation information pooling, a Json conversion tool class may be constructed: jsonnMultpleConver tools, add static attribute definitions: private static LinkedHashMap jsonObjectMap and private static LinkedHashMap jsonConverMap;
jsonObjectMap: the system marks the scanned classes, which classes have annotation conversion definition and which classes do not have annotation conversion definition, stores all scanned class names and whether annotations exist or not, and avoids repeated scanning next time;
jsonConverMap: the system stores the result information, i.e. the information of the Json annotation (i.e. the target annotation) on the class attribute, in the Json ConverMap.
For the acquisition and loading of the annotation information, all classes under a specified package name can be scanned and acquired in the JVM starting process, and the annotation scanning acquisition can also be performed on the classes of classes when specific Java Class conversion is used; for the class that is scanned, the class that is not scanned is directly acquired and used for scanning for the first time.
The specific method for obtaining the annotation information of Class is the same, and firstly all attribute members of the specific Class are obtained through the reflection of Class; acquiring all annotations of each attribute member through the attribute members; and judging whether the annotation is a Json annotation, saving the annotation into a jsonConverMap if the annotation is a Json annotation, and saving the scanned class into a jsonObjectMap if the annotation is a Json annotation.
For each JAVA class, storing whether a target annotation is defined by an attribute member in the JAVA class or not in a jsonObjectMap through a key value pair, wherein the key name stored in the jsonObjectMap is as follows: the package name + "-" + class name, the key value is, whether there is a @ Json annotation; the jsonConverMap also stores the corresponding relation between the JAVA classes and the annotation information thereof in the form of key value pairs, and for each JAVA class, the key name in the jsonConverMap is as follows: a packet name + "-" + a class name of the JAVA class + "-" + an attribute name + "a mode name; the value is the target annotation information, and a thread lock mechanism is needed to be used when the information is stored, so that repeated scanning and repeated storage of multiple threads caused by operation under multiple threads are avoided.
Obtaining annotation information; the method comprises the steps that key names are directly obtained from jsonConverMaps, default annotation definition is adopted when a conversion mode is an empty character string, and @ Json annotation information of class attribute members cannot be obtained through names of specific conversion modes; the name of the conversion mode is acquired again by the mode name of the empty character string, if the definition of the annotation is acquired, the annotation is the default definition of the attribute, and all the definitions of the mode conversion do not define, and the default mode conversion is used for conversion.
The embodiment of the invention has the following technical effects: before the annotation information is used, all JAVA classes are scanned, for example, at the time of startup of the JVM, and an annotation cache is established for caching the annotation information of the JAVA classes with the annotation information. And when the annotation information needs to be read subsequently, the annotation is retrieved and acquired from the annotation cache, so that the annotation information is prevented from being acquired from JAVA classes repeatedly, and the efficiency of acquiring the annotation information is improved.
Further, the method further comprises: a process of converting one of the at least one target format data into an instance of a JAVA class;
acquiring target format raw data, the JAVA classes and a target mode; wherein the target format raw data is one of the at least one target format data;
obtaining annotation information corresponding to the JAVA class;
and converting the target format raw data into attribute members with target annotation modification in class instances created according to the JAVA classes according to the annotation information and the target mode.
In some embodiments, the annotation information corresponding to the JAVA class is obtained, a reflection mechanism may be used to obtain all target annotations and annotation information composed of respective corresponding attribute members from the JAVA class, or when an instance of the JAVA class is obtained for the first time, all target annotations corresponding to each attribute member modified by a target annotation are obtained from the JAVA class to constitute the annotation information corresponding to the JAVA class, and the annotation information is stored in a preset annotation cache, and when the annotation information corresponding to the JAVA class needs to be obtained, the annotation information of the JAVA class is retrieved from the annotation cache according to a packet name and a class name of the JAVA class; and then, according to the target mode, retrieving the target annotation matched with the target mode from one or more target annotations corresponding to the corresponding attribute members in the conversion from the annotation information corresponding to the JAVA class, searching data records corresponding to the attribute members from the target format raw data according to the attribute members of the JAVA class and the target annotations obtained by corresponding retrieval, and converting the data records corresponding to the attribute members in the target format raw data into numerical values of the attribute members in the class examples created according to the JAVA class according to the data types of the attribute members and conversion rules, defined in the retrieved target annotations, of the attribute members between the JAVA class and the target format.
The embodiment of the invention has the following technical effects: one or more predefined target annotations corresponding to the attribute members and the target annotations used in the conversion process by using the target mode matching are used, so that the conversion of the data in various target formats into class instances of JAVA classes is realized, and the requirement of mutual conversion of the data in various target formats of the same attribute member under different scenes is met.
Further, the converting the target format raw data into an attribute member with target annotation modification in a class instance created according to the JAVA class according to the annotation information and the target schema includes:
converting the target format raw data into temporary data; the temporary data comprises at least one group of KEY-VALUE pairs; each group of KEY-VALUE pairs corresponds to corresponding data in the target format raw data one by one;
and converting the KEY-VALUE pairs in the temporary data into attribute members with target annotation modification in class instances created according to the JAVA classes according to the target annotation matched with the target mode in the annotation information.
In some embodiments, the target format raw data is converted into temporary data, including but not limited to data stored in a data structure such as HashMap, and the temporary data may be data buffered in memory or in a file. Preferably, the temporary data is stored using a KEY-VALUE pair. The temporary data is used as transition data between the JAVA classes and the targets, and is firstly converted into the temporary data and then further converted into the target format under the condition that a conversion tool for the temporary data and a JAVA class instance exists and/or a conversion tool for the temporary data and the target format data exists, so that the existing tool can be fully utilized, and repeated development is avoided. As a specific embodiment, when the target format is the Json format, the Gson fromJson method can be directly used to convert Json into temporary data based on the Map data type.
Further, the target annotation includes: a conversion mode, a conversion switch, a target key name, a target data type, a formatting mode, and/or a must-input switch;
the converting the KEY-VALUE pairs in the temporary data into attribute members with target annotation modification in class instances created according to the JAVA classes according to the target annotation matching the target pattern in the annotation information includes:
creating a class instance from the JAVA classes;
acquiring an attribute member list of the JAVA class; the attribute member list includes: all attribute members with target annotation modification and data types thereof in the JAVA class;
for each attribute member in the attribute member list, acquiring a target annotation which modifies the attribute member and is matched with the target pattern from the annotation information as a reference annotation corresponding to the attribute member; and the number of the first and second electrodes,
if the conversion switch is arranged in the reference annotation corresponding to the attribute member and the value of the conversion switch is not converted, removing the attribute member and the data type thereof from the attribute member list to obtain an attribute member list to be converted;
for each attribute member in the attribute member list to be converted, acquiring a KEY corresponding to the attribute member in temporary data, finding a KEY-VALUE pair corresponding to the KEY in the temporary data, and converting the VALUE into data of the attribute member in the class instance according to the data type of the attribute member in the JAVA class;
wherein, the acquiring the KEY corresponding to the attribute member in the temporary data includes:
and if the target KEY name is set in the reference annotation corresponding to the attribute member, using the target KEY name as the KEY of the attribute member in the temporary data correspondingly, otherwise, using the character name of the attribute member in the JAVA class as the KEY of the attribute member in the temporary data correspondingly.
In some embodiments, the attribute member list of the JAVA class may be retrieved from the JAVA class at each conversion, and specifically, all attribute members having target annotation modification and corresponding data types of the JAVA class may be acquired through a reflection mechanism, and the acquired attribute members and corresponding data types are organized as the attribute member list; or, for the execution efficiency of the subsequent conversion, when the JAVA class is read for the first time, all attribute members with target annotation modification and corresponding data types of the JAVA class may be acquired through a reflection mechanism, and the acquired attribute members and corresponding data types are stored in the attribute member cache in the form of an attribute member list, and the attribute member list corresponding to the JAVA class may be retrieved from the attribute member cache through the packet name + class name of the JAVA class. Attribute members in the JAVA class are modified by one or more target annotations, a target mode is required to be matched with a conversion mode in each target annotation of the attribute members, and the target annotation obtained by matching is used as a temporary annotation; the data in different target formats aim at different scene requirements, JAVA class attribute members which do not need to be converted may exist in the data in some target formats, whether the attribute members corresponding to the current temporary annotation need to be converted between the JAVA class and the current original data in the target format can be judged by judging a conversion switch in the matched target annotation, namely the temporary annotation, and for the attribute members which do not need to be converted, an attribute member list to be converted is obtained by deleting the attribute members from the attribute member list.
The following description is specifically made with respect to the temporary data conversion attribute members based on the Map data type:
and adding a static public LinkedHashMap object ProMap attribute list for storing attribute members and data types of the classes and avoiding repeated use of reflection.
Creating a Map2Bean method as a method for converting Map into Class instance, where parameter 1.
public static < T > T Map2Bean (String mode, map, class < T > bearer Class) through Exception; wherein, the Map2Bean () method is used for converting Map into JAVA class instance; description of the parameters: the Map is data of a Map type needing to be converted; the benClass on is the Class of JAVA Class; exception is an Exception thrown when an error occurs; the map2Bean method, creates a Bean according to parameter 3,
establishing Bean (namely Class instance) according to Class of JAVA Class, and acquiring attribute member list information stored in objectProMap (equivalent to attribute member cache) according to packet name and Class name of JAVA Class; if the acquisition is null, the acquisition indicates the first time, all attribute members and the data types thereof are reflected out by acquiring Class according to an Object (an example of a JAVA Class), and the reflection results of the attribute members and the data types thereof are put into an Object ProMap for saving. And if the acquisition is not null, namely the object ProMap already stores the attribute member list information corresponding to the JAVA class, directly acquiring the attribute member list.
To implement annotation pooling, a Json conversion tool class is constructed: jsonnBlultipleConver tool class, add static attribute definition: private static LinkedHashMap jsonObjectMap and private static LinkedHashMap jsonConverMap;
jsonObjectMap: the system marks the scanned classes, which classes have annotation conversion definition and which classes do not have annotation conversion definition, stores all scanned class names and whether annotations exist or not, and avoids repeated scanning next time;
jsonConverMap: the system stores the result information, i.e. the information of the Json annotation on the class attribute, in the jsonConverMap.
For the acquisition and loading of the annotation information, all classes under a specific packet name can be scanned and acquired in the jvm starting process, and the Class classes can also be subjected to annotation scanning and acquisition when specific Java Class conversion is used; for the scanned classes, the direct acquisition is performed, and for the classes without scanning, the scanning is performed for the first time.
The specific method for obtaining the annotation information of Class is the same, and all attribute members of the specific Class are obtained through the reflection of the Class; acquiring all annotations of each attribute member through the attribute members; it is determined whether the annotation is a Json annotation, and if so, saved to a Json convertmap, and whether the annotation exists for the scanned class, saved to a Json objectmap.
The Key saved by jsonObjectMap is: the package name + "-" + class name, value is, whether there is a @ Json annotation; the Key of jsonConverMap is: a package name + "-" + class name + "-" + attribute name + "mode name"; the value is annotation information, and a thread lock mechanism is required to be used when the information is stored, so that repeated scanning and repeated storage of a plurality of threads caused by operation under multiple threads are avoided.
Obtaining annotation information; the method comprises the steps that Key acquisition is directly carried out from jsonConverMap, default annotation definition is adopted when a mode name is an empty character string, and @ Json annotation information of class attribute members cannot be acquired through a specific mode name; the annotation definition is acquired, if the annotation definition is acquired, the annotation is a default definition of the attribute, all the definitions of the schema conversion are not defined, and the default schema conversion is used for conversion.
And acquiring a mode annotation information list specified by a conversion mode matched with the target mode and default converted mode annotation information list information through the jsonConverMap.
Copying an attribute member list, removing attributes with isConver as false in annotation information (the attributes do not need to be converted), acquiring a value corresponding to a Key in a Map by using a Key according to a rule labeled by an annotation, converting the name of a directly used class attribute member without the annotation of the class attribute member by using the name as the Key, wherein the value is a basic type or a basic class, and directly carrying out assignment operation on the Bean through beans.
For non-basic types and basic types, attribute members are complex types (non-Map and List), a corresponding Key value is obtained according to annotation rules, a parameter mode is used, the Key value of the Map is also the Map, the Key value of the Map and the class corresponding to the complex types are used as an input parameter to call the Map2Bean for iteration, a returned result is directly subjected to assignment operation on the Bean through Bean.
And if the complex type is iterated through a Map2Bean method, the iterated Bean Key according to the Map is placed into the newly-built HashMap. The HashMap finished by the processing assigns the beans through the beans.
Directly establishing an ArrayList for the List, acquiring the itemized records of the List according to the annotation description rule, if the List is a basic type and a basic class, directly putting the List into the established ArrayList, if the List is not a member of the basic type and the basic class, calling map2Bean to be iteratively added into the ArrayList, and assigning the established ArrayList to the Bean through Bean Utils.
Therefore, mutual iterative nested conversion of the JAVA class instances and the maps is completed, and the conversion of the JAVA class instances and the maps has no layer limitation and belongs to deep conversion without hierarchy limitation.
On the other hand, as shown in fig. 2, an embodiment of the present invention provides a conversion apparatus for JAVA class instances and target format data, including: a JAVA class instance to target format unit for performing a process of converting an instance of a JAVA class to one of the at least one target format of data;
the JAVA class instance to target format unit 200 includes:
a first parameter obtaining module 201, configured to obtain an instance and a target mode of the JAVA class;
a first target annotation obtaining module 202, configured to, for each attribute member in the instance of the JAVA class that has a target annotation modification, obtain, from at least one target annotation modifying the attribute member, a target annotation matching the target pattern as a current target annotation corresponding to the attribute member;
a target format data generating module 203, configured to convert the attribute member into data in a target format corresponding to a conversion mode in the current target annotation according to the current target annotation corresponding to the attribute member;
wherein the JAVA classes comprise: at least one attribute member having at least one target annotation modification; the target annotation is a pre-defined instance of an annotation class; the target annotation includes: the conversion mode; all target annotations having the same conversion pattern define conversion rules between instances of JAVA classes and data in the target format corresponding to the conversion pattern.
Further, the target annotation further comprises: a transfer switch, a target key name, a target data type, a formatting scheme, and/or a must-go switch;
wherein the target key names set in different target annotations modifying the same attribute member of the JAVA class are the same or different;
the target format data generating module 203 includes:
the first acquisition member list module is used for acquiring an attribute member list of the JAVA class; the list of attribute members includes: all attribute members with target annotation modification and data types thereof in the JAVA class;
the first member filtering module is used for removing the attribute members and the data types thereof from the attribute member list to obtain an attribute member list to be converted if the conversion switch is arranged in the current target annotation corresponding to the attribute members and the value of the conversion switch is not converted aiming at each attribute member in the attribute member list;
an intermediate data generating module, configured to, for each attribute member in the attribute member list to be converted, obtain the attribute member from an instance of the JAVA class, and convert the attribute member into intermediate data corresponding to the attribute member according to the target key name, the target data type, and/or the formatting manner set in the current target annotation corresponding to the attribute member;
and the intermediate data to target data module is used for converting the intermediate data into data in a target format corresponding to the current target annotation.
Further, the intermediate data generation module includes:
an intermediate data key name determining module, configured to, if the target key name is set in the current target annotation corresponding to the attribute member, use the target key name as an intermediate data key name corresponding to the attribute member, and if the target key name is not set in the current target annotation corresponding to the attribute member, use a character name used by the attribute member in the JAVA class as an intermediate data key name corresponding to the attribute member;
a first intermediate numerical value determining module, configured to, if the current target annotation corresponding to the attribute member is provided with the target data type, convert the attribute member into data of the target data type, and use the data of the attribute member in the JAVA class instance as an intermediate data value corresponding to the attribute member, if the current target annotation corresponding to the attribute member is not provided with the target data type; alternatively, the first and second electrodes may be,
a second intermediate value determining module, configured to, if the formatting manner is set in the current target annotation corresponding to the attribute member, take data formatted by the attribute member according to the formatting manner as an intermediate data value corresponding to the attribute member;
wherein, the intermediate data corresponding to the attribute member comprises: and the intermediate data key name and the intermediate data value corresponding to the attribute member.
Further, the target annotation further comprises: verifying the information;
the device, still include:
and the member verification module is used for verifying the attribute members according to the verification information in the current target notes corresponding to the attribute members and prompting errors found by verification.
Further, the verification information includes: numerical ranges and/or regular expressions;
the member verification module includes:
the input verification module is used for verifying whether the data of the attribute member is empty or not if a must-input switch is arranged in the current target note corresponding to the attribute member and the must-input switch is must-input, and throwing a first exception if the data of the attribute member is empty according to a verification result; and/or the presence of a gas in the gas,
a value range verification module, configured to determine, according to the data type of the attribute member, whether the data of the attribute member is within the value range if the value range is set in the current target annotation corresponding to the attribute member, and throw a second exception if the data of the attribute member is not within the value range; and/or the presence of a gas in the gas,
the regular verification module is used for verifying the data of the attribute members according to the regular expression if the regular expression is set in the current target annotation corresponding to the attribute members, and throwing a third exception if the verification result is that the data of the attribute members do not accord with the regular rule of the regular expression;
wherein the data types include: a numeric value type and/or a string type;
the numerical range verification module comprises:
a value verification module, configured to determine whether the value of the data of the attribute member is within the value range if the data type of the attribute member is a value type, and throw the second exception if the value of the data of the attribute member is not within the value range;
and the character length verification module is used for judging whether the character length of the data of the attribute member is in the numerical range or not if the data type of the attribute member is a character string type, and throwing the second exception if the character length of the data of the attribute member is not in the numerical range.
Further, the first parameter obtaining module 201 includes:
the system comprises an annotation loading pooling module, a cache module and an annotation processing module, wherein the annotation loading pooling module is used for acquiring all target annotations corresponding to all attribute members modified by the target annotations from the JAVA classes to form annotation information corresponding to the JAVA classes when the instances of the JAVA classes are acquired for the first time, and storing the annotation information in a preset annotation cache;
the first target annotation acquisition module 202 includes:
the first annotation information acquisition module is used for acquiring annotation information corresponding to the JAVA class from the annotation cache;
a second target annotation obtaining module, configured to, for each attribute member that has a target annotation modification in the JAVA class instance, find, from the annotation information, a target annotation that matches the target pattern in at least one target annotation that modifies the attribute member as a current target annotation corresponding to the attribute member;
wherein the annotation information comprises all attribute members having a target annotation modification and all target annotations that correspondingly modify the attribute members.
Further, the apparatus further comprises: a target format data-to-JAVA class instance unit for implementing a process of converting one of the at least one target format data into an instance of JAVA classes;
the target format data-to-JAVA class instance unit comprises:
the second parameter acquisition module is used for acquiring the target format raw data, the JAVA classes and the target mode; wherein the target format raw data is one of the at least one target format data;
the second annotation information acquisition module is used for acquiring annotation information corresponding to the JAVA class;
and the target data-to-JAVA instance member module is used for converting the target format raw data into an attribute member with target annotation modification in the class instance created according to the JAVA class according to the annotation information and the target mode.
Further, the target data to JAVA instance member module includes:
the temporary data generation module is used for converting the target format raw data into temporary data; the temporary data comprises at least one group of KEY-VALUE pairs; each group of KEY-VALUE pairs corresponds to corresponding data in the target format raw data one by one;
and the temporary data-to-JAVA instance member module is used for converting the KEY-VALUE pairs in the temporary data into attribute members with target annotation modification in class instances created according to the JAVA classes according to the target annotations matched with the target patterns in the annotation information.
Further, the target annotation comprises: a conversion mode, a conversion switch, a target key name, a target data type, a formatting scheme, and/or a must-go switch;
the temporary data-to-JAVA instance member module comprises:
the class instance creating module is used for creating class instances according to the JAVA classes;
the second member list acquisition module is used for acquiring an attribute member list of the JAVA class; the list of attribute members includes: all attribute members with target annotation modification and data types thereof in the JAVA class;
a reference annotation obtaining module, configured to, for each attribute member in the attribute member list, obtain, from the annotation information, a target annotation that modifies the attribute member and matches the target pattern, as a reference annotation corresponding to the attribute member; and the number of the first and second electrodes,
the second member filtering module is used for removing the attribute members and the data types thereof from the attribute member list to obtain an attribute member list to be converted if the conversion switches are arranged in the reference annotations corresponding to the attribute members and the values of the conversion switches are not converted;
an attribute member assignment module, configured to, for each attribute member in the attribute member list to be converted, obtain a KEY corresponding to the attribute member in temporary data, find a KEY-VALUE pair corresponding to the KEY in the temporary data, and convert the VALUE into data of the attribute member in the class instance according to a data type of the attribute member in the JAVA class;
the module for acquiring the KEY corresponding to the attribute member in the temporary data specifically includes:
and if the target KEY name is set in the reference annotation corresponding to the attribute member, using the target KEY name as the KEY of the attribute member in the temporary data correspondingly, otherwise, using the character name of the attribute member in the JAVA class as the KEY of the attribute member in the temporary data correspondingly.
The embodiment of the invention has the following technical effects: by using the custom annotation, the attribute members in the JAVA class are modified, so that the support of one JAVA class is converted into a plurality of target formats, particularly a plurality of JSON formats, and the development efficiency is improved; further, a data verification method in the conversion process is provided, and data correctness is ensured; further, the data is formatted in the conversion process, and particularly, a plurality of formatting supports for date time data are supported.
The above technical solutions of the embodiments of the present invention are described in detail below with reference to specific application examples, and reference may be made to the foregoing related descriptions for technical details that are not described in the implementation process.
The following describes the technical solution of the present invention with the interconversion between the JAVA class instance and the Json target format as a specific embodiment:
the inventor finds that through the use analysis of the currently existing Json toolkit, the Json toolkit in the prior art generally directly performs the conversion of comparison machines, and the following functional defects mainly exist:
1. the name (or character name) of the attribute member of the java class needs to correspond to the key name in the corresponding json target format data, and if the name of the attribute member of the java class is different from the key name in the json target format data, conversion cannot be realized;
2. the data type of the attribute member of the java class and the data type of the corresponding data in the json target format need to correspond, if the data type of the attribute member of the java class and the data type of the corresponding data in the json target format do not correspond, the conversion cannot be realized (note: the basic type converted by json is generally a numerical value type, a character type and a date type, a date conversion format needs to be set, and only one format is supported in the conversion);
3. the attribute members of the java class are numerical values or date-time types, and when the attribute members are converted into data in the json target format, the problem that the output formats in different scenes are different may exist, such as the date type, the year, month and day format is required to be output in the first scene, the year, month, day, time, minute and second format is required to be output in the second scene, and even a message has both the year, month, day format and sometimes minute and second format.
4. The json target format and the conversion of the JAVA class do not support the verification of data and the verification of data format, which are the basic type verification of the conversion.
5. In the case that attribute members in data which do not need to be converted into the Json target format exist in the Java class in part of scenes, and fields in part of the Json target format do not need to be converted into the attribute members of the Java class, the Json conversion tool can not be used.
In view of the above-listed functional defects, the simple json toolkit conversion function cannot meet the requirements of special service scenarios, and is not flexible enough, so that further expansion and improvement are needed.
For example: for example, when performing transfer transactions, the transfers typically have the following fields: transferring out the account, transferring into the name of the account person, transferring out the amount of money, and transferring into the epilogue. Transfers typically have multiple channels: large channel, small channel, unionpay system, super internet bank, agriculture and telecommunications bank, in-line transfer and the like.
Some transfer channels are listed above, the channels are payment systems established at different periods of the bank, each channel is assumed to be converted into Json message format through preposition, but the Json formats of transfer messages of the payment systems are different (equivalent to data which needs to be converted into different target formats (Json) in different scenes), and messages of large-amount channels are assumed: the transfer amount Json key is amt; message of the small channel: the Json key of the transfer amount is money; message of the UnionPay channel: the Json key for the transfer amount is unity. As can be seen from the above, the key of the transfer amount in the Json field of each channel is different, but is a business meaning.
If an online banking system is constructed at present, transfer information is packaged by using a transfer information class TransAccountDto, and meanwhile, the transfer information needs to be connected with a plurality of payment systems in an abutting mode to realize transfer of different channels. If a general json conversion tool is used, it is difficult to convert the TransAccountDto instantiation class into the json message format of transfer of multiple channels to realize the message butt joint of multiple channels. If the correspondence of Json messages of the multiple payment channels needs to be realized, multiple java classes corresponding to Json formats of the channels corresponding to the Json of all the channels need to be created by using the general Json conversion tool, then the attribute information is assigned to the java class corresponding to the Json of all the channels through TransAccountDto, and then the general Json tool is used for conversion so as to meet the requirements of Json formats of interfaces of different payment channels.
Through the analysis of the online banking service scene, if one Java class can be simultaneously converted with multiple Json formats, the code amount compiled by the code can be greatly simplified, the mapping Java classes corresponding to the Json formats one by one are reduced, the labor is saved, the code structure is simplified, and the maintenance amount of the code is reduced.
The following describes how to implement a method for simultaneously supporting one Java class and converting multiple Json formats:
to realize simultaneous support of one Java class and conversion of multiple Json formats, and to solve the existing problems, at least the following information is required:
the method comprises the following steps that corresponding relation information of names of attribute members in the Java class and Json Key is obtained, so that the problem that the attribute members in the Java class do not correspond to the Json Key is solved;
the data type of the attribute member in the Java class corresponds to Json target data type corresponding information so as to solve the problem that the Java data type and the Json data type do not correspond to each other;
the method comprises the steps that data of attribute members in a Java class need to be specified and converted into a Json data format, and conversion from Java to Json is supported, wherein the Json data format can be specified and converted;
the data verification rules are used for verifying the data format and validity by calling a data verification function during data conversion;
in order to distinguish different Json format conversions, identification of different Json conversion formats is required to support that one Java class can be converted into a plurality of Json formats.
Through the analysis, the technical scheme of the invention is realized by adding the @ Json conversion annotation to the attribute member of the Java class, and after Java8, the function of repeatable annotation is added, namely the same annotation can be repeated on the attribute member of the Java, and the tag Mode attribute (equivalent to the conversion mode) of the annotation is used as the mark of the Json conversion format mode to support the simultaneous support of one Java class and the conversion of a plurality of Json formats. The @ Json notes attribute list is as in Table 1 below.
Figure 446403DEST_PATH_IMAGE001
TABLE 1 @ Json notes Attribute List
The definition information of how java and json are converted is defined through annotations and attributes thereof; how to complete the conversion between the JAVA classes and the Json target format on the basis of the JAVA classes and the @ Json annotation is described according to functions required to be completed in the conversion process as follows:
(I) definition of Java Annotation class
Firstly, realizing simultaneous support of one Java class and conversion of multiple Json formats, needing to realize repeatable annotation of attributes of the Java class, and setting a mode of converting the attributes of the Java class into Json. The Json tag is used here to annotate,
here, the implementation fragment of the @ Json annotation class is that a Json annotation interface is first constructed:
public @interface Json {
string tagMode () default;/conversion mode, corresponding to various json formats
String Json Key () default;// Json's Key name, json node name
......
MAX _ VALUE, maximum VALUE or maximum string length
borolan isConver () default true// convert or not
......
}
To describe the tag and the attribute, in order to repeatedly define the attribute which is also required to be packaged according to the array in the container, an annotation container interface is required to be constructed:
public @interface Jsons {
Json[] value();
}
according to the codes, compiling is completed, namely annotation can be performed on the attributes of the class which needs to be converted into Json by the special definition attributes, and the annotation sample codes are used as follows:
public class JsonTest {
@Json(tagMode = "mode1", jsonKey = "id", jsonType = "int")
@ Json (tagMode = "mode2", jsonnType = "String", request = true, min = 8, max = 8, regular = "[ 0-9\ - __ 8, }, $", regular Msg = "must be an 8-digit number")
String code;
......
}
The above code column shows an example of annotation conversion by the attribute code of the JsonTest class:
and converting the mode1, namely outputting the code attribute to a Json format, converting the key of the Json into an id, wherein the numerical type of the id in the Json is a shaping numerical type.
And converting the mode2, outputting the code attribute to a json format, keeping the key of the json consistent, and verifying that the code field cannot be empty and a character string with 8-bit numbers is necessary.
The @ Json (tagMode = "mode1", jsonKey = "id", jsonType = "int") and @ Json (tagMode = "mode2", jsonType = "String", request = true, min = 8, max = 8, regular = "[ 0-9\ # \\u__________ 8, } $", regular msg = "must be an 8-digit number") in the above example are two different target annotations, respectively, the conversion modes are mode1 and mode2, respectively; the two target annotations correspond to data in two Json target formats.
(II) Annotation Loading pooling
Setting the setting information of special conversion on the attribute member of the Java class through the target annotation through the annotation function module, and reading the annotation information on the Java class for reading and using during conversion when one Java class simultaneously supports conversion of multiple Json formats; in order to improve efficiency, in a JVM (i.e. JVM), definition information of annotations only needs to be stored once, and does not change during the operation of the JVM, so configuration information is stored by constructing an annotation information pool (i.e. annotation cache) of JAVA classes with conversion, and after loading once, the configuration information does not need to be loaded again, as shown in fig. 3, an annotation loading pooling module reads all target annotations of JAVA classes and converts the target annotations into pooling information corresponding to conversion modes, and conversion mode definition information of a plurality of classes is stored in the information pool.
To implement annotation information pooling, a Json conversion tool class is constructed: jsonnBlultipleConver tool class, add static attribute definition: private static LinkedHashMap jsonObjectMap and private static LinkedHashMap jsonConverMap;
jsonObjectMap: the system marks the scanned classes, which classes have annotation conversion definition and which classes do not have annotation conversion definition, stores all scanned class names and whether annotations exist or not, and avoids repeated scanning next time;
jsonConverMap: the system stores the result information, i.e. the information of the Json annotation on the class attribute, in the jsonConverMap.
For the acquisition and loading of the annotation information, all classes under the appointed package name can be scanned and acquired in the jvm starting process, and the Class classes can be subjected to annotation scanning and acquisition when specific Java Class conversion is used; for the scanned classes, the direct acquisition is performed, and for the classes without scanning, the scanning is performed for the first time.
The specific method for obtaining the annotation information of Class is the same, and firstly all attribute members of the specific Class are obtained through the reflection of Class; acquiring all annotations of each attribute member through the attribute members; and judging whether the annotation is a Json annotation, saving the annotation into a jsonConverMap if the annotation is a Json annotation, and saving the scanned class into a jsonObjectMap if the annotation is a Json annotation.
The Key saved by jsonObjectMap is: the package name + "-" + class name, value is, whether there is a @ Json annotation; the Key of jsonConverMap is: a package name + "-" + class name + "-" + attribute name + "mode name"; the value is target annotation information, and a thread lock mechanism is required to be used when the information is stored, so that repeated scanning and repeated storage of a plurality of threads caused by operation under multiple threads are avoided.
Obtaining annotation information; the method comprises the steps that Key acquisition is directly carried out from jsonConverMap, default annotation definition is adopted when a mode name is an empty character string, and @ Json target annotation information of class attribute members cannot be acquired through a specific mode name; the annotation definition is acquired, if the annotation definition is acquired, the annotation is the default definition of the attribute, and all the annotation definition which does not define the mode conversion is converted by using the default mode conversion.
(III) type conversion
The Java base types are: the types which do not need to be split again from the perspective of business data are called basic conversion types during data conversion, the types which do not need to be split again during data conversion are treated as minimum elements during data conversion, and the types which do not need to be split continuously are all Java basic classes except for Java basic types.
The BeanUtils toolkit of Apache can be used for conversion of Java base types and base classes, and the BeanUtils of Apache realizes automatic conversion for String and base types and base classes; conversion can be performed by means of a beans.ConvertetUtils tool or automatically by means of a beans.setProperty assignment; in the basic type and the basic class, the conversion of date and time to the string format requires the automatic conversion by registering the corresponding converter.
For String converting date and time type, common format of date and time can be listed separately, and the character String is converted into date and time format by automatically recognizing type of time format of character String date and time format. And the converter is constructed to register as a String-to-date time converter through ConvertUtils.
For the date-time type conversion character string, a converter of the date-time conversion character may be constructed for the date-time type with "yyyy-MM-dd HH: MM: ss" as a default format, and automatic conversion is realized by convertedutils.
For the fact that the attribute members of the JAVA class are of a date type and need to be converted into character strings, a date-time conversion format defined by 'pattern' of annotations of the attributes is obtained through JsonnMulitpleConver pooling information, if format definitions exist, conversion is carried out according to the format definitions, and if the conversion format is not defined, conversion is carried out by adopting 'yyyy-MM-dd HH: MM: ss' as a default format.
Description of the drawings: setmethod requires that classes must have properties when using beans. getProperty needs a get method that a class must have attributes, corresponding set and get methods must exist, otherwise abnormal information is thrown out, the get and set methods can be automatically increased by using similar plug-ins such as a lombok plug-in, and the code amount is reduced.
(IV) data verification
The invocation of the data verification module uses the JsonMULTIPLEConver tool class verification method:
public static void verifiBean(String mode, Object obj) throws Exception;
the verifiBean () method is used for verifying the data validity of the Json annotation class; description of the entrance parameters: mode is the target mode name; obj is a JAVA class instance needing to be verified, wherein the verification is to verify the attribute members of the obj Java class, and the attribute members of the support class verify Json annotation definition; exception is an error Exception.
Acquiring corresponding conversion verification information such as request, min, max, regular Msg and the like in the target annotation of the attribute member of the Java class through JsonnMultpleConver pooling information, and verifying the attribute member capable of acquiring the verification information:
must lose (must lose switch): if the data of the attribute member of the Java class instance is must be input and the data is empty, throwing the exception is equivalent to throwing the first exception. Numerical ranges: if the data of the Java class instance attribute member is a numerical value type, judging whether the numerical value is in the numerical value range of the minimum value and the maximum value; and if the data of the Java class instance attribute member is a character string, judging whether the character length is in a defined range. And (3) regular verification: and taking out the regular expression to carry out regular judgment on the data of the Java class instance attribute members.
For error handling, the exception throwing the error information is used here, and the error exception information is specifically as follows:
1. for input-necessary (i.e. input-necessary switch), if the input-necessary switch is input-necessary and the data of the Java class instance attribute member is null, prompting: the attribute "X" value may not be null; (corresponding to the first anomaly);
2. numerical ranges: if the data of the Java class instance attribute member as a numerical type or a character string exceeds a numerical range, prompting: the attribute "X" value must be within the range of (minimum) and (maximum) or the attribute "X" length must be within the range of (minimum length) and (maximum length); (corresponding to the second anomaly);
3. regular verification: and prompting regular error prompt information defined by the target annotation when the regular verification judges that the data of the Java class instance attribute members do not conform to the regular expression. (corresponding to the third anomaly).
Wherein X is replaced with the name of the specific attribute member where the verification exception occurred; the minimum value and the minimum length are replaced by the value of the minimum value min in the verification information, and the maximum value and the maximum length are replaced by the value of the maximum value max in the verification information;
description of the drawings: the technical scheme of the invention supports the respective definition and verification of the verification under various scenes through the definition of a conversion mode.
(V), JAVA class and Map conversion
1. Firstly, a static public LinkedHashMap objectProMap attribute list is added in a JsonMeultpleConver tool class and used for storing attribute members of the class and data types of the attribute members, so that repeated use of reflection is avoided, and the method is also one of pooling modes.
2. Creating a static public bean2Map method as a tool for converting class instances into maps, wherein parameters 1 comprise a target mode name and 2 comprise an object class, and returning to the maps;
public static Map bean2Map(String mode, Object obj) throws Exception;
wherein, the bean2Map () method is used for converting the JAVA class instance into Map; description of the entry parameters: mode is the target schema name, obj is the JAVA class instance with the attribute member modified by the instance of the Json annotation class (i.e., the target annotation); exception is an Exception thrown when an error occurs;
the bean2Map method is used for acquiring a stored object ProMap attribute list according to the packet name and the class name; if the acquisition is null, the first loading is indicated, all attribute members are reflected out by acquiring Class according to the Object, and the attribute members and the type reflection result are put into the Object ProMap for storage. The attribute member list is not obtained directly for the null, not for the first time.
And acquiring a current converted mode annotation information list and default converted mode annotation information list information through the jsonConverMap.
Copying an attribute member list, removing attributes with iscoverr false in annotation information (the attributes do not need to be converted), creating a HashMap, taking values from attribute members of an Object through BeanUtils.getProperty, converting non-empty attribute values through beanatils.ConoverUtils according to the type marked by the jsonType, storing the converted values into the Map according to a jsonKey, directly putting the used attribute names without jsonKey information, directly putting the obtained annotations without jsonType attributes into the HashMap without numerical conversion, directly putting the original type values of class attributes into the HashMap, and putting the class attribute member names serving as the obtained attribute member values into the HashMap for the attribute members without annotations.
For complex type (non-Map and List) attribute members that are not members of the base type and base class as other classes, the bean2Map method is recalled for iteration and the results are placed in HashMap.
And if the complex type is iterated through a bean2Map method, the iterated Map is put into the HashMap according to the annotation rule. And (4) putting the processed HashMap into the HashMap at the upper layer according to the annotation description rule.
Directly establishing an ArrayList for the array or List set type, acquiring the item-by-item records of the List of the Bean, directly putting the ArrayList into the array if the array or List set type is a basic type and a basic type, calling Bean2Map to iteratively append the ArrayList to the array if the array or List set type is not a basic type or a basic type and the member of the array is a complex type (non-Map and List), and putting the final array List into the HashMap at the upper layer according to the annotation description rule.
To this end, the converted Map is internally composed of a base type or base class, and Map and List, and the nested Map is internally also composed of a base type or base class, and Map and List, and the elements of List are also composed of a base type or base class, and Map and List.
Creating a Map2Bean method as a method for converting Map into Class instance, where parameter 1.
public static < T > T Map2Bean (String mode, map, class < T > bearer Class) through Exception; wherein, the Map2Bean () method is used for converting the Map into a JAVA class instance; description of the parameters: the Map is data of Map type needing to be converted; the benClass on is a JAVA Class; exception is an Exception thrown in error; the map2Bean method comprises the steps of creating beans according to the parameter 3; if the acquisition is null, the acquisition indicates the first time, all attribute members and types are reflected out by acquiring Class according to the Object, and the attribute members and the type reflection result are put into the Object ProMap for storage. The attribute member list is not directly obtained for null.
The mode annotation information list specified by the current mode and the default converted mode annotation information list information are obtained through jsonConverMap.
Copying an attribute member list, removing attributes with iscovers as false in annotation information (the attributes do not need to be converted), acquiring a value corresponding to a Key in a Map by using a Key according to a rule labeled by the annotation, converting the name of a class attribute member which has no annotation for the class attribute member and directly uses the name of the class attribute member as the Key, wherein the value is of a basic type or a basic class, and directly carrying out assignment operation on the Bean through the beans.
For non-basic types and basic types, attribute members are complex types (non-Map and List), corresponding Key values are obtained according to annotation rules, parameters are used, key values of Map are also Map, classes corresponding to the complex types are used as input parameters to call Map2Bean for iteration, results are returned, and assignment operation is directly carried out on the Bean through beans.
And if the complex type is iterated through a Map2Bean method, the iterated Bean Key according to the Map is placed into the newly-built HashMap. The HashMap finished by the processing is assigned to the beans through beans.
Directly establishing an ArrayList for the List, acquiring the itemized records of the List according to the annotation description rule, if the List is a basic type and a basic class, directly putting the List into the established ArrayList, if the List is not a member of the basic type and the basic class, calling map2Bean to be iteratively added into the ArrayList, and assigning the established ArrayList to the Bean through Bean Utils.
So far, the mutual iterative nested transformation of the class and the Map is completed, and the item transformation of such transformation beans and Map has no layer limitation and belongs to the deep transformation without the hierarchical limitation.
(VI), json and Map interconvertions
Because the mutual deep conversion of the class and the Map is processed and the conversion is completely carried out according to the annotation information of the conversion mode, the Map and the class of the conversion result are in accordance with the annotation conversion rule. A plurality of Json conversion tools can complete the mutual conversion of Map and Json, and the Json toolkit of Gson can be directly used for conversion. Json converts Map, and the method of FromJson of Gson is directly used, wherein fromJson (jsonStr, map. Class) is used, and jsonStr is a Json character string and returns the Map determined by Map. Class. Map to Json, directly using Gson's toJson method, toJson (Map); and the Map is the Map which needs to be converted into the Json character string, and the result is returned to the Json character string.
(VII) JAVA class instance conversion to Json
According to the basic tool module, a static public method bean2Json method is added to a JsonnMultippleConver tool class, a reference 2 is a conversion mode name, a reference 2 is a Java class example needing conversion, and a returned result is a Json character string.
public static String bean2Json(String mode, Object obj) throws Exception;
The bean2Json () method is used for converting an instance obj of a JAVA class into a Json target format character string according to a target mode specified by a mode, and throwing Exception when conversion is wrong;
calling a Java instance to convert to Json requires determining which conversion mode and different conversion modes are used, acquiring corresponding conversion mode definition information, and after determining which conversion mode, completing conversion of the Java class to Json target format data through the process shown in fig. 4. In the process of converting the instances of JAVA classes into Json target format data as shown in fig. 4, after the attribute members in the instances of JAVA classes are verified to be correct, attribute members in the instances of JAVA classes are converted into intermediate data based on Map by using a Map and class interconversion tool and full type conversion under the rule defined by the target annotation matched with the target pattern, and then the intermediate data based on Map is converted into data in Json target format by using a Json and Map conversion tool.
(eight), json to JAVA class instances
Similarly, according to the basic tool module, adding a static common method Json2Bean method in a JsonnMultipeleConver tool Class, wherein the reference 1 is a conversion mode name, the reference 2 is a Json character string needing conversion, and the reference 2 is a Class corresponding to a java Class; the result is returned as an instance of the Java class that needs to be converted. public static < T > T Json2Bean (String mode, string Json Str, class < T > benClass) browse Exception, wherein, the Json2Bean () method is used for converting the target annotation matched with the Json target format into the instance of JAVA Class specified by the Ben Class according to the mode target mode, and throws out the Exception when the conversion is wrong; calling the Json to convert into the Java instance also needs to determine which conversion mode is adopted and different conversion modes, corresponding conversion mode definition information is obtained, and after determining which conversion mode is adopted, json data realizes the conversion of the corresponding Java instance through the conversion process shown in FIG. 5. In the process shown in fig. 5, data in the Json target format is converted into temporary data based on Map by a Json and Map conversion tool, type conversion is performed under the conversion rule defined by the target annotation matched with the target pattern to convert the temporary data based on Map into data of the attribute member in the JAVA class instance, and data verification is performed on the data of the attribute member obtained by conversion according to the verification information in the target annotation matched with the target pattern.
The technical scheme of the invention is further explained by the application example in the bank system as follows:
description of tools
Therefore, an interface for repeatedly annotating Java attributes is constructed, and a method for performing interconversion between JAVA classes and multiple JSON formats is established. The JsonMULTIPLEConver tool class provides the following static public methods:
public static void verifiBean(String mode, Object obj) throws Exception;
public static <T> T map2Bean(String mode, Map map, Class<T> beanClass) throws Exception;
public static Map bean2Map(String mode, Object obj) throws Exception;
public static String bean2Json(String mode, Object obj) throws Exception;
public static <T> T json2Bean(String mode, String jsonStr, Class<T> beanClass) throws Exception;
(II) the following concrete explanation is made by taking the online banking example
In the example of invoking a payment system transfer by online banking, the information of the transfer is packaged with a transfer information class TransAccountDto, the attribute members of the transfer information of TransAccountDto see Table 2:
attribute Member Meaning of Chinese Field type Remark information
transcode Trade serial number String 9 bit character value
channelcode Channel numbering String 4 is a numeric character
outaccountid Roll-out account String 12 digit numeric character
outaccountname Roll-out account name String Not less than 4 characters
bankid Row identification code String 3-digit digital character
cityid City code String Non-essential input, 4-digit character
tranbankid Line number String 12 digit numeric character
tranbankname Line name String Must not be empty
inaccountid Transfer to account String Minimum length of 8 bits and maximum length of 18 bits
inaccountname Transferred account name String Not less than 4 characters
transamt Transfer money finance BigDecimal Different channel restrictions are different
starttime Time of occurrence Date Type of time and date
notice Account transfer postscript String May be empty by up to 20 characters
......
TABLE 2 Attribute membership sheet for transfer information
The following is a transfer information class code segment to which no target annotation has been added;
// transfer information class
public class TransAccountDto {
String transaction serial number
String channel code// channel number
String outaccountid;/roll-out account
String outaccountname// roll-out account name
String bankid// transfer to row class
String cityid// transition to city coding
String intrabankid// transfer row number
String tranbanknname// transfer into row name
String inaccount// transfer to account
String inaccountname// transfer account name
BigDecimal tranexamt// transfer amount
Date start time, time of occurrence
String node// appendix
......
}
In the transfer scene, because the payment channels are different, the messages of the transfer are different, and aiming at the channels related in the text: the large channel, the small channel, the unionpay system, super internet bank, the nong letter bank, transfer accounts in the line, defines the channel code respectively and does: 001 for large channel, and 001 for small channel: 002, union pay system: 003, super-net silver: 004, silver, nong-xin: 005, inline transfer: 000.
1. the name of an attribute member in Java is different from the Key (i.e., key name) corresponding to Json, and there is a problem that conversion is required:
for example, the transfer amount attribute member transamt in the Java class has different keys in the Json target format data in different scenes: for messages of a large channel: the transfer amount Key is amt; message of the small channel: key of transfer amount is money; message of the UnionPay channel: key of transfer amount is unitamt;
in the face of this problem, the following comments may be made on the attribute membership transfer amount;
@ Json (tagMode = "0001", jsonnKey = "amt", request = true, min = 5000)// grand amount
@ Json (tagMode = "0002", jsonKey = "money", request = true, min =0.01, max = 5000)// decimal
@ Json (tagMode = "0003", jsonKey = "unitamt", request = true, min = 0.01)// silver union
BigDecimal tranexamt// transfer amount
Large usage of the conversion mode parameters: "0001", small amount: using the conversion mode parameters: "0002", the Unionpay uses the conversion mode parameters: 0003, respectively calling JsonMULTIPLECONVER. Beans 2Json methods, wherein the keys for transferring the transfer amount of Json are respectively defined keys, so that the problem that the keys in java attribute names Json are different is solved.
2. For the problem that the Java data type corresponds to the Json target data type:
the transaction serial number is a numeric character, and if the serial number is required to be a long type for the transfer in the row, the requirement of the transfer in the row can be met by the following definition:
@ Json (tagMode = "0000", jsonnType = "long", request = true)// Unionpay
String transform// transaction serial number
Also in-line transfer use switching mode parameters: and 0000 calls a JsonMULTIPLECONVER (beacon 2Json) method, so that the automatic conversion of the data types of the transcodes can be realized, and the conversion of other conversion modes is not influenced.
3. And outputting dynamic change of Json format in different scenes when the java attribute is a numerical value or date-time type:
this scenario is also obvious, for example, super webbank requires time to take milliseconds, and other channels require time to take seconds, which can be solved by the following annotation definition.
@ Json (tagMode = "", pattern = "yyyyy-MM-dd HH: MM: ss")// default format
@ Json (tagMode = "004", pattern = "yyyyy-MM-dd HH: MM: ss. SSS.)// super-net silver
Date start time// occurrence time
the conversion mode name of tagMode is empty character string, which represents default mode, and the annotation definition of the mode is used by default under the condition that other modes are not defined, wherein the annotation definition is that the format of the occurrence time of super Internet bank is annual month day hour minute second millisecond, and other channels are annual month day hour minute second format, and the expected format conversion can be realized by the above annotation.
4. The verification is supported in the process of java class attribute and Json conversion, and different scene verification rules are different:
in this example, the validation rules for the transfer amounts of the large and small amounts are significantly different,
@ Json (tagMode = "0001", jsonnKey = "amt", request = true, min = 50000)// grand amount
@ Json (tagMode = "0002", jsonKey = "money", request = true, min =0.01, max = 50000)// decimal
@ Json (tagMode = "0003", jsonKey = "unitamt", request = true, min = 0.01)// silver union
BigDecimal transamt, transfer amount
By the definition, the method meets the requirements of various channels on verification of the account amount,
5. in the process of Java class attribute and Json conversion, the Java class attribute of part of scenes or Json Key does not need conversion:
in the transfer scenario of super internet bank, the line name and line number are not needed. The following comments are made to super webbings:
@ Json (tagMode = "0004", isConver = "false")// super-wire silver
String tranbankid// transfer into Row number
@ Json (tagMode = "0004", isconverer = "false")// super-screen silver
String tranbanknname// transfer into row name
Josn converted by using the method of json multippleconverter. Beans 2json is called by using the mode "0004" and has no line name and line number fields, so that the expected effect is achieved.
Further, the target format in the technical solution of the present invention is not limited to Json, XML or other JAVA class; namely, the technical scheme of the invention can also realize that one java class instance is converted into a plurality of java class instances.
Such as: class A instances, class B instances, class C instances; the A type is a service model type, the B type is a communication message type, the C type is a database entity type, and only the target annotation based on the B type is defined on the A service model type (namely, A _ B mutual conversion mode), and the target annotation based on the C type is defined on the A service model type (namely, A _ C mutual conversion mode); and the Map is used as an intermediate conversion medium, so that the problem of converting one JAVA class instance into a plurality of class instances can be realized. Firstly, A is converted into a Map corresponding to B by using an A _ B mutual conversion mode, and then the Map is converted into B. Similarly, the A is converted into the Map corresponding to the C by using the A _ C conversion mode, and then the Map is converted into the C. And vice versa.
It should be understood that the specific order or hierarchy of steps in the processes disclosed is an example of exemplary approaches. Based upon design preferences, it is understood that the specific order or hierarchy of steps in the processes may be rearranged without departing from the scope of the present disclosure. The accompanying method claims present elements of the various steps in a sample order, and are not intended to be limited to the specific order or hierarchy presented.
In the foregoing detailed description, various features are grouped together in a single embodiment for the purpose of streamlining the disclosure. This method of disclosure is not to be interpreted as reflecting an intention that the claimed embodiments of the subject matter require more features than are expressly recited in each claim. Rather, as the following claims reflect, invention lies in less than all features of a single disclosed embodiment. Thus, the following claims are hereby expressly incorporated into the detailed description, with each claim standing on its own as a separate preferred embodiment of the invention.
The previous description of the disclosed embodiments is provided to enable any person skilled in the art to make or use the present invention. To those skilled in the art; various modifications to these embodiments will be readily apparent, and the generic principles defined herein may be applied to other embodiments without departing from the spirit or scope of the disclosure. Thus, the present disclosure is not intended to be limited to the embodiments shown herein but is to be accorded the widest scope consistent with the principles and novel features disclosed herein.
What has been described above includes examples of one or more embodiments. It is, of course, not possible to describe every conceivable combination of components or methodologies for purposes of describing the aforementioned embodiments, but one of ordinary skill in the art may recognize that many further combinations and permutations of various embodiments are possible. Accordingly, the embodiments described herein are intended to embrace all such alterations, modifications and variations that fall within the scope of the appended claims. Furthermore, to the extent that the term "includes" is used in either the detailed description or the claims, such term is intended to be inclusive in a manner similar to the term "comprising". Furthermore, any use of the term "or" in the specification of the claims is intended to mean a "non-exclusive or".
Those of skill in the art will further appreciate that the various illustrative logical blocks, units, and steps described in connection with the embodiments disclosed herein may be implemented as electronic hardware, computer software, or combinations of both. To clearly illustrate the interchangeability of hardware and software, various illustrative components, elements, and steps have been described above generally in terms of their functionality. Whether such functionality is implemented as hardware or software depends upon the particular application and design requirements of the overall system. Skilled artisans may implement the described functionality in varying ways for each particular application, but such implementation decisions should not be interpreted as causing a departure from the scope of the present embodiments.
The above-mentioned embodiments, objects, technical solutions and advantages of the present invention are further described in detail, it should be understood that the above-mentioned embodiments are only examples of the present invention, and are not intended to limit the scope of the present invention, and any modifications, equivalent substitutions, improvements and the like made within the spirit and principle of the present invention should be included in the scope of the present invention.

Claims (10)

1. A method for converting JAVA class instances and object format data, comprising: a process for converting an instance of JAVA classes into one of at least one target format of data;
a process for converting an instance of said JAVA class into one of at least one target format of data, comprising:
acquiring an instance and a target mode of the JAVA class;
for each attribute member with target annotation modification in the JAVA class instance, acquiring a target annotation matched with the target mode from at least one target annotation modifying the attribute member as a current target annotation corresponding to the attribute member; and also,
converting the attribute member into data of a target format corresponding to a conversion mode in the current target annotation according to the current target annotation corresponding to the attribute member;
wherein the JAVA classes comprise: at least one attribute member having at least one target annotation modification; the target annotation is a pre-defined instance of an annotation class; the target annotation comprises: the conversion mode; all target annotations having the same conversion pattern define conversion rules between instances of JAVA classes and data in a target format corresponding to the conversion pattern.
2. The method for converting JAVA class instances and target format data as recited in claim 1, wherein said target annotation further comprises: a transfer switch, a target key name, a target data type, a formatting scheme, and/or a must-input switch;
wherein the target key names set in different target annotations modifying the same attribute member of the JAVA class are the same or different;
the converting the attribute member into the data of the target format corresponding to the conversion mode in the current target annotation according to the current target annotation corresponding to the attribute member includes:
acquiring an attribute member list of the JAVA class; the attribute member list includes: all attribute members with target annotation modification and data types thereof in the JAVA class;
for each attribute member in the attribute member list, if the change-over switch is arranged in the current target annotation corresponding to the attribute member and the value of the change-over switch is not changed, removing the attribute member and the data type thereof from the attribute member list to obtain an attribute member list to be changed;
for each attribute member in the attribute member list to be converted, acquiring the attribute member from an instance of the JAVA class, and converting the attribute member into intermediate data corresponding to the attribute member according to the target key name, the target data type and/or the formatting mode set in the current target annotation corresponding to the attribute member;
and converting the intermediate data into data in a target format corresponding to the current target annotation.
3. The method for converting JAVA class instance and target format data according to claim 2, wherein said obtaining the attribute member from the JAVA class instance for each attribute member in the list of attribute members to be converted, and converting the attribute member into the intermediate data corresponding to the attribute member according to the target key name, the target data type and/or the formatting manner set in the current target annotation corresponding to the attribute member comprises:
if the current target annotation corresponding to the attribute member is provided with the target key name, using the target key name as an intermediate data key name corresponding to the attribute member, and if the current target annotation corresponding to the attribute member is not provided with the target key name, using a character name used by the attribute member in the JAVA class as the intermediate data key name corresponding to the attribute member;
if the current target annotation corresponding to the attribute member is provided with the target data type, converting the attribute member into data after the target data type as an intermediate data value corresponding to the attribute member, and if the current target annotation corresponding to the attribute member is not provided with the target data type, using the data of the attribute member in the JAVA class instance as the intermediate data value corresponding to the attribute member; alternatively, the first and second electrodes may be,
if the formatting mode is set in the current target annotation corresponding to the attribute member, taking the data formatted by the attribute member according to the formatting mode as an intermediate data value corresponding to the attribute member;
wherein, the intermediate data corresponding to the attribute member comprises: and the intermediate data key name and the intermediate data value corresponding to the attribute member.
4. The method for converting JAVA class instances and object format data of claim 2,
the target annotation further comprises: verifying the information;
before converting the attribute member into data of a target format corresponding to a conversion mode in the current target annotation according to the current target annotation corresponding to the attribute member, the method further includes:
and verifying the attribute members according to the verification information in the current target annotations corresponding to the attribute members, and prompting errors discovered by verification.
5. The method of converting JAVA class instances and target format data as recited in claim 4, wherein said authentication information comprises: a range of values and/or regular expressions;
the verifying the attribute member according to the verification information in the current target annotation corresponding to the attribute member and prompting the error found by verification includes:
if the current target annotation corresponding to the attribute member is provided with a must-output switch, and the must-output switch is must-output, verifying whether the data of the attribute member is empty, and if the verification result is that the data of the attribute member is empty, throwing a first exception; and/or the presence of a gas in the atmosphere,
if the numerical range is set in the current target annotation corresponding to the attribute member, judging whether the data of the attribute member is in the numerical range according to the data type of the attribute member, and if the data of the attribute member is not in the numerical range, throwing a second exception; and/or the presence of a gas in the atmosphere,
if the regular expression is set in the current target annotation corresponding to the attribute member, verifying the data of the attribute member according to the regular expression, and if the verification result is that the data of the attribute member does not accord with the regular rule of the regular expression, throwing a third exception;
wherein the data types include: a numeric value type and/or a string type;
if the current target annotation corresponding to the attribute member is provided with the numerical range, judging whether the data of the attribute member is in the numerical range according to the data type of the attribute member, and if the data of the attribute member is not in the numerical range, throwing a second exception, wherein the method comprises the following steps:
if the data type of the attribute member is a numerical value type, judging whether the numerical value of the data of the attribute member is in the numerical value range, and if the numerical value of the data of the attribute member is not in the numerical value range, throwing the second exception;
and if the data type of the attribute member is a character string type, judging whether the character length of the data of the attribute member is in the numerical range, and if the character length of the data of the attribute member is not in the numerical range, throwing the second exception.
6. The method for converting instances and object format data of JAVA classes as claimed in claim 1, wherein said obtaining instances and object patterns of said JAVA classes comprises:
when the instance of the JAVA class is obtained for the first time, all target annotations corresponding to all attribute members modified by the target annotations are obtained from the JAVA class to form annotation information corresponding to the JAVA class, and the annotation information is stored in a preset annotation cache;
the obtaining, for each attribute member having a target annotation modification in the instance of the JAVA class, a target annotation matching the target pattern from at least one target annotation modifying the attribute member as a current target annotation corresponding to the attribute member includes:
obtaining annotation information corresponding to the JAVA class from the annotation cache;
for each attribute member with target annotation modification in the instance of the JAVA class, finding a target annotation which is matched with the target pattern in at least one target annotation modifying the attribute member from the annotation information as a current target annotation corresponding to the attribute member;
wherein the annotation information comprises all attribute members having a target annotation modification and all target annotations that correspondingly modify the attribute members.
7. The method for converting JAVA class instances and object format data as recited in claim 6, further comprising: a process of converting one of the data in the at least one target format into an instance of a JAVA class;
a process for converting one of said at least one target format data into an instance of a JAVA class, comprising:
acquiring target format raw data, the JAVA classes and a target mode; wherein the target format raw data is one of the at least one target format data;
obtaining annotation information corresponding to the JAVA class;
and converting the target format raw data into attribute members with target annotation modification in the class instances created according to the JAVA classes according to the annotation information and the target mode.
8. The method for converting instances of JAVA classes and object format data of claim 7,
the converting the target format raw data into an attribute member with target annotation modification in a class instance created according to the JAVA class according to the annotation information and the target schema includes:
converting the target format raw data into temporary data; the temporary data comprises at least one group of KEY-VALUE pairs; each group of KEY-VALUE pairs corresponds to corresponding data in the target format raw data one by one;
and converting the KEY-VALUE pairs in the temporary data into attribute members with target annotation modification in the class instances created according to the JAVA classes according to the target annotations matched with the target patterns in the annotation information.
9. The method for converting JAVA class instances and object format data of claim 8,
the target annotation comprises: a conversion mode, a conversion switch, a target key name, a target data type, a formatting mode, and/or a must-input switch;
the converting the KEY-VALUE pairs in the temporary data into attribute members with target annotation modification in class instances created according to the JAVA classes according to the target annotation matching the target pattern in the annotation information includes:
creating a class instance from the JAVA classes;
acquiring an attribute member list of the JAVA class; the attribute member list includes: all attribute members with target annotation modification and data types thereof in the JAVA class;
for each attribute member in the attribute member list, acquiring a target annotation which modifies the attribute member and is matched with the target pattern from the annotation information as a reference annotation corresponding to the attribute member; and the number of the first and second electrodes,
if the conversion switch is arranged in the reference annotation corresponding to the attribute member and the value of the conversion switch is not converted, removing the attribute member and the data type thereof from the attribute member list to obtain an attribute member list to be converted;
for each attribute member in the attribute member list to be converted, acquiring a KEY corresponding to the attribute member in temporary data, finding a KEY-VALUE pair corresponding to the KEY in the temporary data, and converting the VALUE into data of the attribute member in the class instance according to the data type of the attribute member in the JAVA class;
wherein, the acquiring the KEY corresponding to the attribute member in the temporary data includes:
and if the target KEY name is set in the reference annotation corresponding to the attribute member, using the target KEY name as the KEY of the attribute member in the temporary data correspondingly, otherwise, using the character name of the attribute member in the JAVA class as the KEY of the attribute member in the temporary data correspondingly.
10. An apparatus for converting JAVA class instances and object format data, comprising: a JAVA class instance to target format unit for implementing a process of converting an instance of a JAVA class to one of the at least one target format of data;
the JAVA class instance to object format unit comprises:
the first parameter acquisition module is used for acquiring the instances and the target modes of the JAVA classes;
a first target annotation obtaining module, configured to, for each attribute member having a target annotation modification in an instance of the JAVA class, obtain, from at least one target annotation modifying the attribute member, a target annotation matching the target pattern as a current target annotation corresponding to the attribute member;
the object format data generation module is used for converting the attribute members into the data of the object format corresponding to the conversion mode in the current object annotation according to the current object annotation corresponding to the attribute members;
wherein the JAVA classes comprise: at least one attribute member having at least one target annotation modification; the target annotation is a pre-defined instance of an annotation class; the target annotation comprises: the switching mode; all target annotations having the same conversion pattern define conversion rules between instances of JAVA classes and data in the target format corresponding to the conversion pattern.
CN202211407117.2A 2022-11-10 2022-11-10 Conversion method and device for JAVA class instance and target format data Active CN115455239B (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN202211407117.2A CN115455239B (en) 2022-11-10 2022-11-10 Conversion method and device for JAVA class instance and target format data

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN202211407117.2A CN115455239B (en) 2022-11-10 2022-11-10 Conversion method and device for JAVA class instance and target format data

Publications (2)

Publication Number Publication Date
CN115455239A CN115455239A (en) 2022-12-09
CN115455239B true CN115455239B (en) 2023-01-06

Family

ID=84295796

Family Applications (1)

Application Number Title Priority Date Filing Date
CN202211407117.2A Active CN115455239B (en) 2022-11-10 2022-11-10 Conversion method and device for JAVA class instance and target format data

Country Status (1)

Country Link
CN (1) CN115455239B (en)

Families Citing this family (1)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN115586904B (en) * 2022-12-12 2023-04-07 云筑信息科技(成都)有限公司 JSON data format description-based dynamic generation method for Java beans

Citations (6)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN106302442A (en) * 2016-08-12 2017-01-04 广州慧睿思通信息科技有限公司 A kind of network communication packet analytic method based on Java language
CN111159215A (en) * 2019-12-06 2020-05-15 深圳和而泰家居在线网络科技有限公司 Mapping method and device of Java class and relational database and computing equipment
CN111562907A (en) * 2020-04-09 2020-08-21 深圳壹账通智能科技有限公司 Conversion method and system of user-defined interface data
CN111651221A (en) * 2020-07-14 2020-09-11 迈普通信技术股份有限公司 Annotation internationalization method and device, storage medium and electronic equipment
CN113326311A (en) * 2021-06-25 2021-08-31 深圳前海微众银行股份有限公司 Data conversion method and device
WO2022002030A1 (en) * 2020-06-29 2022-01-06 北京沃东天骏信息技术有限公司 Data processing method, apparatus, and device, and computer readable storage medium

Patent Citations (6)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN106302442A (en) * 2016-08-12 2017-01-04 广州慧睿思通信息科技有限公司 A kind of network communication packet analytic method based on Java language
CN111159215A (en) * 2019-12-06 2020-05-15 深圳和而泰家居在线网络科技有限公司 Mapping method and device of Java class and relational database and computing equipment
CN111562907A (en) * 2020-04-09 2020-08-21 深圳壹账通智能科技有限公司 Conversion method and system of user-defined interface data
WO2022002030A1 (en) * 2020-06-29 2022-01-06 北京沃东天骏信息技术有限公司 Data processing method, apparatus, and device, and computer readable storage medium
CN111651221A (en) * 2020-07-14 2020-09-11 迈普通信技术股份有限公司 Annotation internationalization method and device, storage medium and electronic equipment
CN113326311A (en) * 2021-06-25 2021-08-31 深圳前海微众银行股份有限公司 Data conversion method and device

Non-Patent Citations (1)

* Cited by examiner, † Cited by third party
Title
java中自定义注解类并加以应用;小鱼吃猫;《https://www.cnblogs.com/Lyn4ever/p/11594533.html》;20190926;全文 *

Also Published As

Publication number Publication date
CN115455239A (en) 2022-12-09

Similar Documents

Publication Publication Date Title
CN115455239B (en) Conversion method and device for JAVA class instance and target format data
CN101572696A (en) Method and device for validating data on webpage form
CN110309099A (en) Interface managerial method, device, equipment and computer readable storage medium
CN112907234A (en) Decision engine implementation method based on dynamic configuration rules
CN102123099B (en) Message processing system and method
CN109886076B (en) Invoice storage method
CN103235757B (en) Several apparatus and method that input domain tested object is tested are made based on robotization
CN110275703A (en) Assignment method, device, computer equipment and the storage medium of key-value pair data
CN113191130A (en) Bank electronic flow data preprocessing method
CN111400187B (en) Parameter dynamic verification system and method based on customized data source
CN202143092U (en) Message processing system
CN115907938A (en) Financial management system
CN116308333A (en) Method, system, device and storage medium for determining payment channel
CN111639478B (en) Automatic data auditing method and system based on EXCEL document
US11775757B2 (en) Automated machine-learning dataset preparation
CN114387429A (en) Vehicle property right processing method, device, equipment and medium based on RPA and AI
CN114297240A (en) Method, device, equipment and medium for making payment voucher document based on RPA and AI
JP4662733B2 (en) Transfer information processing apparatus and transfer information processing method
CN116959018B (en) OCR-based intelligent checking method, system and equipment
CN117494702B (en) Data pushing method and system combining RPA and AI
CN116719871B (en) Data interaction method, device and system of transaction management system
CN114359567A (en) Feature data extraction method and device
CN114820204A (en) Financial report processing method and system and financial business processing system
CN116996589A (en) Transaction message generation method, device, equipment and storage medium
CN115080194A (en) Business method transaction detection method and system

Legal Events

Date Code Title Description
PB01 Publication
PB01 Publication
SE01 Entry into force of request for substantive examination
SE01 Entry into force of request for substantive examination
GR01 Patent grant
GR01 Patent grant