Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How external interfaces unify api addresses

Shulou Source: shulou.com Published: 2022-06-01 14:57:27 10月01日 Update

Today, I will talk to you about how to unify the api address of the external interface, which may not be well understood by many people. in order to make you understand better, the editor has summarized the following for you. I hope you can get something according to this article.

Common practices are

1. Use if or case statements to determine parameter values; advantages: simple and easy to understand

Disadvantages: too many interfaces, too many if or case branches, ugly code

two。 Dynamic proxy how to parse the parameters, I am not very clear about calling the function.

Does 3.spring implement this kind of functionality?

Can Spring achieve this kind of function?

Why not look at the documents? The categorization is very clear in the Spring MVC documentation. You only need to see the Request Mapping section: https://docs.spring.io/spring/docs/5.2.5.RELEASE/spring-framework-reference/web.html#mvc-ann-requestmapping

API is required to share a URL and call different methods with different parameters, right?

The frontend requests server-side API, and the parameters include the following forms: HTTP Method (GET, POST, PUT, DELETE...) Path Variable (variables in the URL path) Request Parameter (after the question mark, the most common way to pass parameters. This approach can fully meet the current problem.) The parameter Consumable Media Types in Header (whether the request body is JSON or XML) permission (login is not logged in, what is the role after login, access to different interfaces). Strictly speaking, this is not a parameter, but the server side to further determine the role of the user.

Can be thought of as parameters. Only Path Variable may change the URL path, so the other five forms are described below.

1. Same URL, different HTTP Method

It is the URL of / persons, calling the getPerson and add methods, respectively.

2, same URL, different Request Parameter

Both / pets, the URL,/pets?type=cat and / pets?type=dog, call different methods.

3. Different parameters in the same URL,Header

Both / pets, the URL,/pets?type=cat and / pets?type=dog, call different methods.

4, same URL, different Consumable Media Types

It is the URL of / pets. The requested data is in different formats and different methods are called.

5. The same URL has different permissions

Both / persons, the URL,Admin and User, call different methods. Admin can get all the data, and User can only get the data that it has permission.

The above Annotation can be combined freely. Some are also Class-level, but can be annotated on the Controller class.

After reading the above, do you have any further understanding of how the external interface unifies the api address? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Tags: Different parameters methods interfaces content data permissions login address unified clear functional common form document method server role path into one Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple vpn Shulou Technology Shulou Tech Info macOS