Home > Back-end >  RESTFUL API design and JAVA backend implementation of some doubt
RESTFUL API design and JAVA backend implementation of some doubt

Time:11-05

I recently in the API of the RESTFUL style, but in the process of research, have a few problems.

Such as by id can I obtain a user's information, I can design the API
 GET [host] : [port]/API/v1/users/{id} 

The background, I can use a @ PathVariable receive

I want to get all users,
I can design the API
 GET [host] : [port]/API/v1/users 


So in the meantime, I have another demand, I want to by name for user information, how do I design the API?
If I so design API
 
GET [host] : [port]/API/v1/users/{name}
Or
GET [host] : [port]/API/v1/users? Name={name}

This is not possible, because Spring is not allowed to repeat the url,
GET [host] : [port]/API/v1/users/{id}
And
GET [host] : [port]/API/v1/users/{name}
Url is the same, it is not allowed.

GET [host] : [port]/API/v1/users? Name={name}
And
GET [host] : [port]/API/v1/users
The same

So the question comes, what should I design for a particular field is equal to a value of API?

Or, I give up the Controller of automatic assignment function, receives a url all parameters, access to the content, and then judge calls the corresponding business?


This is I see a big design API, so like this & gt;=, & lt;=,=-=+, the background how to obtain, string to judge, if I could only use the Controller gain=the value of the

CodePudding user response:

Can't change the name, this don't tangle [host] : [port]/API/v1/username/{name} indicate the need to find the key parameters, the third fourth specify parameter values

CodePudding user response:

So whether id and the name can be incorporated into a [host] : [port]/API/v1/{searchkey}/{searchvalue}

CodePudding user response:

Why the parameter value in the URL, directly passed as a parameter not quick zai
Two interfaces
GET [host] : [port]/API/v1/users/id
Corresponding getById (id)

GET [host] : [port]/API/v1/users/name
Corresponding getByName (String name)

CodePudding user response:

refer to the second floor DXF2020 response:
id and the name just can be merged into one [host] : [port]/API/v1/{searchkey}/{searchvalue}


Much of this writing, if the query field, the Controller layer must be large section of the business, this and servlet writing also do not have what distinction, I think this is not good,



reference 1st floor DXF2020 response:
will not be able to change the name, this don't tangle [host] : [port]/API/v1/username/{name} to indicate the need to find the key parameters, the third fourth specify parameter values


The writing should be ok, I didn't think so before, learned, thank you very much

CodePudding user response:

The
reference 3 floor KeepSayingNo response:
why parameter value in the URL, directly passed as a parameter not quick zai
Two interfaces
GET [host] : [port]/API/v1/users/id
Corresponding getById (id)

GET [host] : [port]/API/v1/users/name
Corresponding getByName (String name)


This kind of

This & gt;=, & lt;=,=-=+, backstage access, bosses have any idea
  • Related