Analysis on the implementation of JSON view of custom spring MVC

scene

Our team is now facing the problem of multi terminal data interface docking. In order to solve this problem, we have defined the specification of interface docking,

The front end (Android, IOS, web front end) and the back end discussed the data format specification, and determined the JSON data format:

Code represents request processing status: 200 is normal processing, 300 is business exception processing, and 500 is system exception processing.

Data represents the data returned in the background.

Message the background prompt will return the error reason when it is normal or successful.

Here comes the problem

Wouldn't it be troublesome for everyone to wrap the return value of each JSON view,

At this time, AOP is on the stage. We can use the idea of AOP to wrap the request before the response is sent to the client after the request returns JSON.

Implementation steps

Enable AOP

Create slice

Analyze it

@The Component annotation indicates that the object is handed over to the spring container for instantiation

@Aspect indicates that this is an aspect class

@Around(value = "@annotation(org.springframework.web.bind.annotation.ResponseBody)")

Indicates that all methods annotated with @ ResponseBody are tangent points in this aspect, in other words, they will be intercepted.

be careful:

The proceedingjoinpoint parameter in the warp method can only use the subclass proceedingjoinpoint of joinpoint for surround notification,

Each connection point type can call the method of the proxy, obtain and change the return value. Otherwise, use joinpoint.

Case 1: assume that the function in conroller class does not need any return value

For example, when I update an entity object, I just need to return the update result. There is no need to fill in the data

Returned data format:

Implementation idea:

Get the return value type of this function in the processing function of the aspect processing class. If it is void, return the data in the specified format. The isreturnvoid () above makes such a judgment.

You only need to set the return value of the function to void:

Case 2: suppose the return value of the function in conroller class does not need wrapping

For example:

The return values of some front-end plug-ins and third-party docking (payment) are specified,

And downloading files, we are redundant,

Implementation idea:

Customize a @ void annotation:

Add this annotation to the method of the controller layer

Judge whether the function contains the annotation on this aspect processing class

It will not be processed and the original mold will be returned as is.

The isneedwrap () method in the jsonreturnaspect class handles this requirement.

summary

The above is the thought analysis of the JSON view of custom spring MVC introduced by Xiaobian. I hope it will be helpful to you. If you have any questions, please leave me a message and Xiaobian will reply to you in time. Thank you very much for your support for the programming tips website!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>