Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
234 views
in Technique[技术] by (71.8m points)

java - Which naming pattern should i use for basic CRUD?

I'm creating a RestFul API for the first time using spring framework and now im a bit confused about the common labels used to create, read, update and delete. I want to follow a pattern for an easy maintenance in the code. Is there any rule or naming pattern for the labels that I should follow?

Im thinking about:

/service        -> return every services
/service/new    -> create new service
/service/update -> update service
/service/delete -> delete service
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use the HTTP verb to control what you want to do with the resouces:

GET:    /services        -> returns all elements
GET:    /services/{id}   -> returns element with id
POST:   /services        -> creates a new object, pass the object in the body
PUT:    /services/{id}   -> updates element with id, pass updated values in body
DELETE: /services/{id} -> delete element with id

I strongly recommend you use query params for paging in GET: /services, return a default number on page 1 if it's not listed.

A full request could look like: http://www.example.com/services?page=5&count=10


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...