Disclosure of Invention
The embodiment of the invention provides a method, a device, computer equipment and a storage medium for uniformly processing response messages, and aims to solve the problem that the formats of the response messages returned to a front end are inconsistent.
In a first aspect, an embodiment of the present invention provides a method for uniformly processing response packets, where the method includes:
if the request is detected to be abnormal on a Filter layer, skipping the request to a preset abnormal Filter;
acquiring the exception type and exception information of the request;
generating a response message in a preset format according to the abnormal type and the abnormal information of the request, and sending the generated response message to the front end;
if the request is detected to be abnormal from Service to afterCompletion, processing the request abnormality by a preset annotation, wherein the format of a response message generated by the preset annotation is the preset format.
The further technical scheme is that the acquiring of the exception type and exception information of the request comprises:
and calling a doFilter method to acquire the exception type and exception information of the request from the ServeltRequest.
A further technical solution is that the generating a response packet in a preset format according to the exception type and the exception information of the request includes:
and generating a response message in a preset format according to a pre-stored response message template, the abnormal type of the request and the abnormal information.
The further technical scheme is that an abnormal type space and an abnormal information space are arranged in the response message template, and the response message with a preset format is generated according to the pre-stored response message template, the abnormal type of the request and the abnormal information, and the method comprises the following steps:
and filling the abnormal type and the abnormal information of the request into an abnormal type space and an abnormal information space in the response message template respectively.
The further technical scheme is that the preset annotation is an annotation @ ExceptionHandler.
In a second aspect, an embodiment of the present invention further provides an apparatus for uniformly processing a response packet, where the apparatus includes:
the skipping unit is used for skipping the request to a preset exception Filter if the request is detected to be abnormal on the Filter layer;
an obtaining unit, configured to obtain an exception type and exception information of the request;
the first generating unit is used for generating a response message in a preset format according to the abnormal type and the abnormal information of the request and sending the generated response message to the front end;
and the processing unit is used for processing the request exception by a preset annotation if the request exception is detected between Service and afterCompletion, wherein the format of a response message generated by the preset annotation is the preset format.
In a third aspect, an embodiment of the present invention further provides a computer device, which includes a memory and a processor, where the memory stores a computer program, and the processor implements the above method when executing the computer program.
In a fourth aspect, the present invention also provides a computer-readable storage medium, which stores a computer program, and the computer program can implement the above method when being executed by a processor.
The embodiment of the invention provides a method and a device for uniformly processing response messages, computer equipment and a storage medium. Wherein the method comprises the following steps: if the request is detected to be abnormal on a Filter layer, skipping the request to a preset abnormal Filter; acquiring the exception type and exception information of the request; generating a response message in a preset format according to the abnormal type and the abnormal information of the request, and sending the generated response message to the front end; if the request is detected to be abnormal from Service to afterCompletion, the preset annotation is used for processing the request abnormality, wherein the format of the response message generated by the preset annotation is the preset format, so that the response message with a uniform format can be returned to the front end, the workload of the front end is reduced, and the front end can correctly analyze the response message.
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 some, not all, embodiments of the present invention. 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.
It will be understood that the terms "comprises" and/or "comprising," when used in this specification and the appended claims, specify the presence of stated features, integers, steps, operations, elements, and/or components, but do not preclude the presence or addition of one or more other features, integers, steps, operations, elements, components, and/or groups thereof.
It is also to be understood that the terminology used in the description of the invention herein is for the purpose of describing particular embodiments only and is not intended to be limiting of the invention. As used in the specification of the present invention and the appended claims, the singular forms "a," "an," and "the" are intended to include the plural forms as well, unless the context clearly indicates otherwise.
It should be further understood that the term "and/or" as used in this specification and the appended claims refers to and includes any and all possible combinations of one or more of the associated listed items.
As used in this specification and the appended claims, the term "if" may be interpreted contextually as "when", "upon" or "in response to a determination" or "in response to a detection". Similarly, the phrase "if it is determined" or "if a [ described condition or event ] is detected" may be interpreted contextually to mean "upon determining" or "in response to determining" or "upon detecting [ described condition or event ]" or "in response to detecting [ described condition or event ]".
Referring to fig. 1 and fig. 3, fig. 1 is a schematic flow chart of a method for uniformly processing response messages according to an embodiment of the present invention. FIG. 3 is a flowchart illustrating request processing according to an embodiment of the invention.
Fig. 1 is a flowchart illustrating a method for uniformly processing a response packet according to an embodiment of the present invention. As shown, the method includes the following steps S1-S4.
S1, if the request is detected to be abnormal on the Filter layer, jumping the request to a preset abnormal Filter.
In the embodiment of the invention, aiming at the defect that the annotation @ exceptinonHandler can not capture the Filter exception, a Filter (Filter) for processing errors (type is DispatcterType.ERROR) is added by combining the characteristics of the Filter (the execution of the Filter is processed according to a chain, and different types of filters can process different requests), and the Filter (Filter) is named as ErrorFilter, is used for processing the exception which can not be processed by the annotation @ exceptinonHandler, and returns a response message with a uniform format.
In a specific implementation, if it is detected that an exception occurs between Service and afterCompletion of a request, a preset annotation is used to process the exception of the request, wherein a format of a response message generated by the preset annotation is the preset format.
In one embodiment, the preset annotation is the annotation @ exceptinonhandler.
The exception from Service to artifact can be sent to @ exception handler for processing, and then a response message is returned to the terminal. The format of the response message is the preset format.
The invention provides a method for uniformly processing response messages, wherein if a request is detected to be abnormal on a Filter layer, the request is jumped to a preset abnormal Filter; acquiring the exception type and exception information of the request; generating a response message in a preset format according to the abnormal type and the abnormal information of the request, and sending the generated response message to the front end; if the request is detected to be abnormal from Service to afterCompletion, the preset annotation is used for processing the request abnormality, wherein the format of the response message generated by the preset annotation is the preset format, so that the response message with a uniform format can be returned to the front end, the workload of the front end is reduced, and the front end can correctly analyze the response message.
Fig. 4 is a schematic block diagram of anapparatus 70 for uniformly processing response messages according to an embodiment of the present invention. As shown in fig. 4, the present invention further provides adevice 70 for uniformly processing response messages, corresponding to the above method for uniformly processing response messages. The apparatus for uniformly processingresponse messages 70 includes a unit for performing the above-described method for uniformly processing response messages, and the apparatus may be configured in a server. Specifically, referring to fig. 4, theapparatus 70 for uniformly processing response messages includes a jumpingunit 71, an obtainingunit 72, afirst generating unit 73, and aprocessing unit 74.
A skippingunit 71, configured to skip the request to a preset exception Filter if it is detected that the request is abnormal on the Filter layer;
an acquiringunit 72, configured to acquire an exception type and exception information of the request;
afirst generating unit 73, configured to generate a response packet in a preset format according to the exception type and the exception information of the request, and send the generated response packet to the front end;
theprocessing unit 74 is configured to, if it is detected that an exception occurs between Service and afterCompletion of the request, process the exception of the request with a preset annotation, where a format of a response packet generated by the preset annotation is the preset format.
In one embodiment, as shown in fig. 5, the obtainingunit 72 includes a calling unit 721.
The invoking unit 721 is configured to invoke the doFilter method, so as to obtain the exception type and the exception information of the request from the ServletRequest.
In an embodiment, as shown in fig. 6, thefirst generating unit 73 includes asecond generating unit 731.
Thesecond generating unit 731 is configured to generate a response packet in a preset format according to a pre-stored response packet template, the exception type of the request, and the exception information.
In an embodiment, the response message template has an abnormal type space and an abnormal information space, as shown in fig. 7, thesecond generating unit 731 includes afilling unit 7311.
Afilling unit 7311, configured to fill the exception type and the exception information of the request into an exception type space and an exception information space in the response message template, respectively.
It should be noted that, as can be clearly understood by those skilled in the art, the specific implementation process of theapparatus 70 for uniformly processing a response packet and each unit may refer to the corresponding description in the foregoing method embodiment, and for convenience and conciseness of description, no further description is provided herein.
The means 70 for uniformly processing the response messages may be implemented in the form of a computer program which can be run on a computer device as shown in fig. 8.
Referring to fig. 8, fig. 8 is a schematic block diagram of a computer device according to an embodiment of the present application. Thecomputer device 500 may be a terminal or a server, where the terminal may be an electronic device with a communication function, such as a smart phone, a tablet computer, a notebook computer, a desktop computer, a personal digital assistant, and a wearable device. The server may be an independent server or a server cluster composed of a plurality of servers.
Referring to fig. 8, thecomputer device 500 includes aprocessor 502, memory, and anetwork interface 505 connected by asystem bus 501, where the memory may include anon-volatile storage medium 503 and aninternal memory 504.
Thenon-volatile storage medium 503 may store anoperating system 5031 and acomputer program 5032. Thecomputer programs 5032, when executed, cause theprocessor 502 to perform a method for uniformly processing response messages.
Theprocessor 502 is used to provide computing and control capabilities to support the operation of theoverall computer device 500.
Theinternal memory 504 provides an environment for the operation of thecomputer program 5032 in thenon-volatile storage medium 503, and when thecomputer program 5032 is executed by theprocessor 502, theprocessor 502 can be enabled to perform a method for uniformly processing the response message.
Thenetwork interface 505 is used for network communication with other devices. Those skilled in the art will appreciate that the configuration shown in fig. 8 is a block diagram of only a portion of the configuration relevant to the present teachings and does not constitute a limitation on thecomputer device 500 to which the present teachings may be applied, and that aparticular computer device 500 may include more or less components than those shown, or combine certain components, or have a different arrangement of components.
Wherein theprocessor 502 is configured to run thecomputer program 5032 stored in the memory to implement the following steps:
if the request is detected to be abnormal on a Filter layer, skipping the request to a preset abnormal Filter;
acquiring the exception type and exception information of the request;
generating a response message in a preset format according to the abnormal type and the abnormal information of the request, and sending the generated response message to the front end;
if the request is detected to be abnormal from Service to afterCompletion, processing the request abnormality by a preset annotation, wherein the format of a response message generated by the preset annotation is the preset format.
In an embodiment, when the step of obtaining the exception type and the exception information of the request is implemented, theprocessor 502 specifically implements the following steps:
and calling a doFilter method to acquire the exception type and exception information of the request from the ServeltRequest.
In an embodiment, when theprocessor 502 implements the step of generating the response packet in the preset format according to the exception type and the exception information of the request, the following steps are specifically implemented:
and generating a response message in a preset format according to a pre-stored response message template, the abnormal type of the request and the abnormal information.
In an embodiment, the response packet template is provided with an abnormal type space and an abnormal information space, and when theprocessor 502 implements the step of generating the response packet with the preset format according to the pre-stored response packet template, the abnormal type of the request, and the abnormal information, the following steps are specifically implemented:
and filling the abnormal type and the abnormal information of the request into an abnormal type space and an abnormal information space in the response message template respectively.
It should be understood that, in the embodiment of the present Application, theProcessor 502 may be a Central Processing Unit (CPU), and theProcessor 502 may also be other general purpose processors, Digital Signal Processors (DSPs), Application Specific Integrated Circuits (ASICs), Field-Programmable gate arrays (FPGAs) or other Programmable logic devices, discrete gate or transistor logic devices, discrete hardware components, and the like. Wherein a general purpose processor may be a microprocessor or the processor may be any conventional processor or the like.
It will be understood by those skilled in the art that all or part of the flow of the method implementing the above embodiments may be implemented by a computer program instructing associated hardware. The computer program may be stored in a storage medium, which is a computer-readable storage medium. The computer program is executed by at least one processor in the computer system to implement the flow steps of the embodiments of the method described above.
Accordingly, the present invention also provides a storage medium. The storage medium may be a computer-readable storage medium. The storage medium stores a computer program. The computer program, when executed by a processor, causes the processor to perform the steps of:
if the request is detected to be abnormal on a Filter layer, skipping the request to a preset abnormal Filter;
acquiring the exception type and exception information of the request;
generating a response message in a preset format according to the abnormal type and the abnormal information of the request, and sending the generated response message to the front end;
if the request is detected to be abnormal from Service to afterCompletion, processing the request abnormality by a preset annotation, wherein the format of a response message generated by the preset annotation is the preset format.
In an embodiment, when the processor executes the computer program to implement the step of obtaining the exception type and the exception information of the request, the following steps are specifically implemented:
and calling a doFilter method to acquire the exception type and exception information of the request from the ServeltRequest.
In an embodiment, when the processor executes the computer program to implement the step of generating the response packet in the preset format according to the exception type and the exception information of the request, the following steps are specifically implemented:
and generating a response message in a preset format according to a pre-stored response message template, the abnormal type of the request and the abnormal information.
In an embodiment, the response packet template is provided with an abnormal type space and an abnormal information space, and when the processor executes the computer program to implement the step of generating the response packet with the preset format according to the pre-stored response packet template, the requested abnormal type and the abnormal information, the following steps are specifically implemented:
and filling the abnormal type and the abnormal information of the request into an abnormal type space and an abnormal information space in the response message template respectively.
The storage medium may be a usb disk, a removable hard disk, a Read-Only Memory (ROM), a magnetic disk, or an optical disk, which can store various computer readable storage media.
Those of ordinary skill in the art will appreciate that the elements and algorithm steps of the examples described in connection with the embodiments disclosed herein may be embodied in electronic hardware, computer software, or combinations of both, and that the components and steps of the examples have been described in a functional general in the foregoing description for the purpose of illustrating clearly the interchangeability of hardware and software. Whether such functionality is implemented as hardware or software depends upon the particular application and design constraints imposed on the implementation. 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 invention.
In the embodiments provided in the present invention, it should be understood that the disclosed apparatus and method may be implemented in other ways. For example, the above-described apparatus embodiments are merely illustrative. For example, the division of each unit is only one logic function division, and there may be another division manner in actual implementation. For example, various elements or components may be combined or may be integrated into another system, or some features may be omitted, or not implemented.
The steps in the method of the embodiment of the invention can be sequentially adjusted, combined and deleted according to actual needs. The units in the device of the embodiment of the invention can be merged, divided and deleted according to actual needs. In addition, functional units in the embodiments of the present invention may be integrated into one processing unit, or each unit may exist alone physically, or two or more units are integrated into one unit.
The integrated unit, if implemented in the form of a software functional unit and sold or used as a stand-alone product, may be stored in a storage medium. Based on such understanding, the technical solution of the present invention essentially or partially contributes to the prior art, or all or part of the technical solution can be embodied in the form of a software product, which is stored in a storage medium and includes instructions for causing a computer device (which may be a personal computer, a terminal, or a network device) to execute all or part of the steps of the method according to the embodiments of the present invention.
In the above embodiments, the descriptions of the respective embodiments have respective emphasis, and for parts that are not described in detail in a certain embodiment, reference may be made to related descriptions of other embodiments.
It will be apparent to those skilled in the art that various changes and modifications may be made in the present invention without departing from the spirit and scope of the invention. Thus, while the invention has been described with respect to the above-described embodiments, it will be understood that the invention is not limited thereto but may be embodied with various modifications and changes.
While the invention has been described with reference to specific embodiments, the invention is not limited thereto, and various equivalent modifications and substitutions can be easily made by those skilled in the art within the technical scope of the invention. Therefore, the protection scope of the present invention shall be subject to the protection scope of the claims.