Query all news under a certain category restful specification URL how to write

there is a news system in the project, which is divided into international news, domestic news and industry news. It is stored in the database table bigclass, and international news is divided into financial news, sports news and entertainment news. Domestic news and industry news are also divided into many small categories, which are stored in the database table smallclass, all the news content is stored in the news table, and the news table has a smallclass_id associated with the table smallclass. If you need to query all the news in a small category, you can write:

http://www.example.com/api/smallclass/5/news

but what should I do if I want to query all the news under a certain category? There is no direct relationship between the news table and the bigclass table.

May.22,2021

this has nothing to do with restful. If news and bigclass are not related, there must be a relationship between smallclass and bigclass, so that news and bigclass can be associated through smallclass.

if you want to write according to the restful specification, url should look something like this:

http://www.example.com/api/bigclass/:bigclass_id/smallclass/:smallclass_id/news

or pass bigclass to the server as query, for example:

http://www.example.com/api/smallclass/:smallclass_id/news?bigclass_id=123

I don't quite understand what you mean. On the surface, it looks very simple. Look up the bigclass data, then write:

http://www.example.com/api/bigclass

but it feels like there is something wrong with your restful design. According to the restful design specification, you are accessing the resource news, not the category bigclass or smallclass.
so your news query URL should look like this:

http://www.example.com/api/news?bigclass=XXX&smallclass=YYY

in the future, if you want to return all news categories, the URL of the category you get should look like this:

http://www.example.com/api/smallclass

there are two kinds:

  • GET / api/v1/news?bigclass= {big_class_id}
  • GET / api/v1/bigclasses/ {big_class_id} / news

as for the database part, just associate it.

Resources RESTful API Design Guide-Ruan Yifeng's web log $6, Filter information (Filtering)
Menu