Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize uniform format return of API Interface designed by Java

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail how to achieve uniform format return of API interface designed by Java. The content of the article is of high quality, so the editor will share it with you for reference. I hope you will have some understanding of the relevant knowledge after reading this article.

Today, with the prevalence of distributed and micro-services on the mobile Internet, most of the projects now adopt the micro-service framework, which is separated from the front and back ends.

(digression: the responsibilities of the front and rear end are becoming more and more clear. Now the front end is called the big front end, and the technology stack and biosphere have become very mature. In the past, the back end staff looked down on the front end staff, but now the back end staff need to re-recognize the front end. The front end is very systematic.)

The general overall architecture of the general system is as follows:

To be clear, some partners will reply that this architecture is too simple, too low, no gateways, no caches, no message middleware.

Because Lao Gu mainly introduces the API interface, we focus on it, and the friends of other modules supplement it on their own.

Interface interaction

The front-end and the back-end interact. The front-end requests the URL path according to the agreement and passes in the relevant parameters. The back-end server receives the request, carries on the business processing, and returns the data to the front-end.

In view of the restful style of the URL path and the requirements of the common request headers for passing parameters (such as app_version,api_version,device, etc.), Lao Gu will not introduce it here. Friends can understand it for themselves, which is relatively simple.

How does the back-end server return the data to the front end?

Return format

When the backend returns to the frontend, we usually use the JSON body method, which is defined as follows:

{# return status code code:integer, # return information description message:string, # return value data:object}

CODE status code

Code returns the status code. Generally, friends can add whatever they need when developing.

If the API wants to return a user permission exception, let's add a status code of 101. Next time, if we want to add a data parameter exception, add a status code of 102. Although this can satisfy the business as usual, the status code is too messy.

We should be able to refer to the status code returned by the HTTP request

The following are common HTTP status codes: 200-request successful 301-resources (web pages, etc.) permanently transferred to other URL 404-requested resources (web pages, etc.) do not exist 500-internal server error

And then define the status code in the enumeration.

Control layer Controller

We will process the business request at the controller layer and return it to the front end, taking the order order as an example

Then let's transform Controller.

This and our usual code is the same, very intuitive, directly return the order object, this is not perfect.

So what is the implementation plan?

Realization scheme

Friends how to achieve is not a little bit of ideas, in this process, we need to do a few things

Define an annotation @ ResponseResult, indicating that the value returned by this interface needs to be wrapped

Intercept a request to determine whether it needs to be annotated by @ ResponseResult

The core step is to implement the interfaces ResponseBodyAdvice and @ ControllerAdvice, determine whether the return value needs to be wrapped, and if so, rewrite the return value of the Controller interface.

Annotation class

Used to mark the return value of the method, whether it needs to be wrapped

The core idea of this code is to get this request, whether it needs to return a value wrapper, and set an attribute tag.

Rewrite the return body

How to do the overall exception handling, space reasons, Lao Gu will not introduce here, as long as the train of thought is clear, self-transformation on the line.

Rewrite Controller

Add @ ResponseResult annotation to the controller class or method body, so it's ok, so it's simple. To return to the completion of the design idea, is not both concise and elegant.

Is there any other room for optimization in this scheme? of course there is.

For example, every request needs to be reflected. Whether the method of the request needs to be wrapped can actually be used as a cache and does not need to be parsed every time.

About Java design API interface how to achieve unified format return to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report