Disclosure of Invention
The embodiment of the invention provides a method, a device, computer equipment and a storage medium for uniformly processing response messages, which aim to solve the problem of inconsistent formats of response messages returned to a front end.
In a first aspect, an embodiment of the present invention provides a method for uniformly processing a response packet, including:
if detecting that the request is abnormal in the Filter layer, jumping the request to a preset abnormal Filter;
acquiring the abnormality type and abnormality 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 transmitting the generated response message to the front end;
if an abnormality of the request is detected to occur between the Service and the afterCompletion, processing the abnormality of the request 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 obtaining the abnormality type and the abnormality information of the request includes:
the dopilter method is called to obtain the exception type of the request and exception information from ServletRequest.
The further technical scheme is that the generating a response message with a preset format according to the abnormality type and the abnormality information of the request comprises:
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 response message template is provided with an abnormal type space and an abnormal information space, 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 comprises the following steps:
and filling the abnormal type and the abnormal information of the request into the abnormal type blank and the abnormal information blank in the response message template respectively.
The further technical scheme is that the preset annotation is 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 jump unit is used for jumping the request to a preset abnormal Filter if detecting that the request is abnormal in the Filter layer;
the acquisition unit is used for acquiring the abnormality type and the abnormality information of the request;
the first generation 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 abnormality of the request by a preset annotation if detecting that the request is abnormal 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 method when executing the computer program.
In a fourth aspect, embodiments of the present invention also provide a computer readable storage medium storing a computer program which, when executed by a processor, implements the above method.
The embodiment of the invention provides a method, a device, computer equipment and a storage medium for uniformly processing response messages. Wherein the method comprises the following steps: if detecting that the request is abnormal in the Filter layer, jumping the request to a preset abnormal Filter; acquiring the abnormality type and abnormality 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 transmitting the generated response message to the front end; if an abnormality of a request between Service and afterCompletion is detected, processing the abnormality of the request by a preset annotation, wherein the format of a 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 is ensured to be capable of correctly analyzing the response message.
Detailed Description
The following description of the embodiments of the present invention will be made clearly and fully with reference to the accompanying drawings, in which it is evident that the embodiments described are some, but not all embodiments of the invention. All other embodiments, which can be made by those skilled in the art based on the embodiments of the invention without making any inventive effort, are intended to be within the scope of the invention.
It should be understood that the terms "comprises" and "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 this specification 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 the present specification and the appended claims refers to any and all possible combinations of one or more of the associated listed items, and includes such combinations.
As used in this specification and the appended claims, the term "if" may be interpreted as "when..once" or "in response to a determination" or "in response to detection" depending on the context. Similarly, the phrase "if a determination" or "if a [ described condition or event ] is detected" may be interpreted in the context of meaning "upon determination" or "in response to determination" or "upon detection of a [ described condition or event ]" or "in response to detection of a [ described condition or event ]".
Referring to fig. 1 and 3, fig. 1 is a schematic flowchart of a method for uniformly processing response messages according to an embodiment of the present invention. FIG. 3 is a flow chart illustrating request processing according to an embodiment of the invention.
Fig. 1 is a flowchart of a method for uniformly processing a response message according to an embodiment of the present invention. As shown, the method includes the following steps S1-S4.
S1, if detecting that the request is abnormal in the Filter layer, jumping the request to a preset abnormal Filter.
In the embodiment of the invention, aiming at the defect that the annotation @ ExceptionHandler cannot capture the exception of the Filter, a Filter (a Filter) for processing errors (a type of dispatcherType. ERROR) is added by combining the characteristics of the Filter (the execution of the Filter is according to chained processing and different types of filters can process different requests), and the Filter is named as an error Filter for processing the exception which cannot be processed by the annotation @ ExceptionHandler and returning a response message with a uniform format.
In specific implementation, if an abnormality of a request occurs in a Filter layer, the request is skipped to a preset abnormality Filter (error Filter) so as to process the abnormality of the request by the abnormality Filter.
In a specific operation, when the SpringMVC item is started and loaded, the exception Filter (ErrorFilter) sets the dispatcherTypes as the dispatcherType, so that the SpringMVC automatically jumps to the exception Filter (ErrorFilter) when encountering the Filter layer exception.
S2, acquiring the abnormality type and abnormality information of the request.
In specific implementation, the exception type and the exception information of the request are obtained, so that a response message is generated according to the exception type and the exception information of the request.
In one embodiment, the step S2 specifically includes: the dopilter method is called to obtain the exception type of the request and exception information from ServletRequest. The dopilter method is a method in which a Filter (Filter) has a single method for processing requests and responses. The dopilter method accepts three input parameters: servlet Request, response, and Filter Chain objects. By calling the dopilter method, the exception type of the request and exception information can be obtained from ServletRequest.
S3, 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.
In specific implementation, a response message in a preset format is generated according to the abnormal type and the abnormal information of the request, and the generated response message is sent to the front end.
In one embodiment, the step S3 specifically includes the following steps: 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 a specific implementation, an abnormal type space and an abnormal information space are set in the response message template, and a response message with a preset format is generated according to a pre-stored response message template, the abnormal type of the request and the abnormal information, which specifically includes:
and filling the abnormal type and the abnormal information of the request into the abnormal type space and the abnormal information space in the response message template respectively, so that the response message with the uniform format can be generated.
S4, if the request is detected to be abnormal between Service and afterCompletion, processing the abnormality of the request by a preset annotation, wherein the format of a response message generated by the preset annotation is the preset format.
In the implementation, if an abnormality of a request is detected to occur between Service and afterCompletion, the abnormality of the request is processed by a preset annotation, wherein the format of a response message generated by the preset annotation is the preset format.
In one embodiment, the preset annotation is an annotation @ ExceptionHandler.
The exception of the request from Service to afterCompletion can be handed to @ ExceptionHandler 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, which comprises the steps of jumping a request to a preset abnormal Filter if detecting that the request is abnormal in a Filter layer; acquiring the abnormality type and abnormality 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 transmitting the generated response message to the front end; if an abnormality of a request between Service and afterCompletion is detected, processing the abnormality of the request by a preset annotation, wherein the format of a 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 is ensured to be capable of correctly analyzing the response message.
Fig. 4 is a schematic block diagram of an apparatus 70 for unified processing of response messages according to an embodiment of the present invention. As shown in fig. 4, the present invention further provides a device 70 for uniformly processing the response message, corresponding to the above method for uniformly processing the response message. The means 70 for uniformly processing the response message comprises means for performing the above-described method for uniformly processing the response message, which means may be arranged in a server. Specifically, referring to fig. 4, the apparatus 70 for unified processing of response messages includes a jumping unit 71, an obtaining unit 72, a first generating unit 73, and a processing unit 74.
A jumping unit 71, configured to, if an abnormality in the Filter layer is detected, jump the request to a preset abnormality Filter;
an acquisition unit 72 configured to acquire an abnormality type of the request and abnormality information;
a first generating unit 73, configured to generate a response message in a preset format according to the abnormality type and the abnormality information of the request, and send the generated response message to the front end;
and a processing unit 74, configured to process the request exception by a preset annotation if it is detected that the request exception occurs between Service and afterCompletion, where the format of the response message generated by the preset annotation is the preset format.
In one embodiment, as shown in fig. 5, the acquisition unit 72 includes a calling unit 721.
A calling unit 721, configured to call a dopilter method to obtain the exception type and the exception information of the request from ServletRequest.
In an embodiment, as shown in fig. 6, the first generating unit 73 includes a second generating unit 731.
The second generating unit 731 is configured to generate a response message in a preset format according to a pre-stored response message template, the abnormality type of the request, and the abnormality information.
In an embodiment, the response message template is provided with an anomaly type space and an anomaly information space, as shown in fig. 7, and the second generating unit 731 includes a filling unit 7311.
And a filling unit 7311, configured to fill the abnormality type and abnormality information of the request into an abnormality type space and an abnormality information space in the response message template, respectively.
It should be noted that, as will be clearly understood by those skilled in the art, the specific implementation process of the apparatus 70 and each unit for uniformly processing the response message may refer to the corresponding description in the foregoing method embodiment, and for convenience and brevity of description, the description is omitted here.
The means 70 for uniformly processing the response message may be implemented in the form of a computer program which is executable 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. The computer 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 formed by a plurality of servers.
With reference to FIG. 8, the computer device 500 includes a processor 502, memory, and a network interface 505 connected by a system bus 501, where the memory may include a non-volatile storage medium 503 and an internal memory 504.
The non-volatile storage medium 503 may store an operating system 5031 and a computer program 5032. The computer program 5032, when executed, causes the processor 502 to perform a method for unified processing of response messages.
The processor 502 is used to provide computing and control capabilities to support the operation of the overall computer device 500.
The internal memory 504 provides an environment for the execution of a computer program 5032 in the non-volatile storage medium 503, which computer program 5032, when executed by the processor 502, causes the processor 502 to perform a method for unified processing of response messages.
The network interface 505 is used for network communication with other devices. Those skilled in the art will appreciate that the architecture shown in fig. 8 is merely a block diagram of a portion of the architecture in connection with the present application and is not intended to limit the computer device 500 to which the present application is applied, and that a particular computer device 500 may include more or fewer components than shown, or may combine certain components, or have a different arrangement of components.
Wherein the processor 502 is configured to execute a computer program 5032 stored in a memory to implement the steps of:
if detecting that the request is abnormal in the Filter layer, jumping the request to a preset abnormal Filter;
acquiring the abnormality type and abnormality 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 transmitting the generated response message to the front end;
if an abnormality of the request is detected to occur between the Service and the afterCompletion, processing the abnormality of the request by a preset annotation, wherein the format of a response message generated by the preset annotation is the preset format.
In one embodiment, when implementing the step of obtaining the exception type and the exception information of the request, the processor 502 specifically implements the following steps:
the dopilter method is called to obtain the exception type of the request and exception information from ServletRequest.
In an embodiment, when the step of generating the response message in the preset format according to the exception type and the exception information of the request is implemented by the processor 502, 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 message template is provided with an abnormal type space and an abnormal information space, and when the processor 502 implements the step of generating the response message with the preset format according to the pre-stored response message template, the abnormal type of the request and the abnormal information, the specific implementation steps are as follows:
and filling the abnormal type and the abnormal information of the request into the abnormal type blank and the abnormal information blank in the response message template respectively.
It should be appreciated that in embodiments of the present application, the processor 502 may be a central processing unit (Central Processing Unit, CPU), the processor 502 may also be other general purpose processors, digital signal processors (Digital Signal Processor, DSP), application specific integrated circuits (Application Specific Integrated Circuit, ASIC), off-the-shelf Programmable gate arrays (FPGAs) or other Programmable logic devices, discrete gate or transistor logic devices, discrete hardware components, or the like. Wherein the general purpose processor may be a microprocessor or the processor may be any conventional processor or the like.
Those skilled in the art will appreciate that all or part of the flow in a method embodying the above described embodiments may be accomplished by computer programs instructing the relevant hardware. The computer program may be stored in a storage medium that 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 detecting that the request is abnormal in the Filter layer, jumping the request to a preset abnormal Filter;
acquiring the abnormality type and abnormality 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 transmitting the generated response message to the front end;
if an abnormality of the request is detected to occur between the Service and the afterCompletion, processing the abnormality of the request by a preset annotation, wherein the format of a response message generated by the preset annotation is the preset format.
In one embodiment, when the processor executes the computer program to implement the step of obtaining the type of abnormality and the abnormality information of the request, the processor specifically implements the following steps:
the dopilter method is called to obtain the exception type of the request and exception information from ServletRequest.
In one embodiment, when the processor executes the computer program to implement the step of generating the response message in a preset format according to the abnormality type and the abnormality information of the request, the processor specifically implements the following steps:
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, an anomaly type space and an anomaly information space are provided in the response message template, and when the processor executes the computer program to implement the step of generating a response message in a preset format according to a pre-stored response message template, the anomaly type of the request and the anomaly information, the processor specifically implements the following steps:
and filling the abnormal type and the abnormal information of the request into the abnormal type blank and the abnormal information blank in the response message template respectively.
The storage medium may be a U-disk, a removable hard disk, a Read-Only Memory (ROM), a magnetic disk, or an optical disk, or other various computer-readable storage media that can store program codes.
Those of ordinary skill in the art will appreciate that the elements and algorithm steps described in connection with the embodiments disclosed herein may be embodied in electronic hardware, in computer software, or in a combination of the two, and that the elements and steps of the examples have been generally described in terms of function in the foregoing description to clearly illustrate 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 solution. 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 several embodiments provided by the present invention, it should be understood that the disclosed apparatus and method may be implemented in other manners. For example, the device embodiments described above 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, multiple units or components may be combined or may be integrated into another system, or some features may be omitted, or not performed.
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 combined, divided and deleted according to actual needs. In addition, each functional unit in the embodiments of the present invention may be integrated in one processing unit, or each unit may exist alone physically, or two or more units may be integrated in one unit.
The integrated unit may be stored in a storage medium if implemented in the form of a software functional unit and sold or used as a stand-alone product. Based on such understanding, the technical solution of the present invention is essentially or a part contributing to the prior art, or all or part of the technical solution may be embodied in the form of a software product stored in a storage medium, comprising several instructions for causing a computer device (which may be a personal computer, a terminal, a network device, etc.) to perform all or part of the steps of the method according to the embodiments of the present invention.
In the foregoing embodiments, the descriptions of the embodiments are focused on, and for those portions of one embodiment that are not described in detail, reference may be made to the related descriptions of other embodiments.
It will be apparent to those skilled in the art that various modifications and variations can be made to the present invention without departing from the spirit or scope of the invention. Thus, it is intended that the present invention also include such modifications and alterations insofar as they come within the scope of the appended claims or the equivalents thereof.
While the invention has been described with reference to certain preferred embodiments, it will be understood by those skilled in the art that various changes and substitutions of equivalents may be made and equivalents will be apparent to those skilled in the art without departing from the scope of the invention. Therefore, the protection scope of the invention is subject to the protection scope of the claims.