How to design an elegant interface RESTFUL

show me the code and talk to me , do it more clear to say
I am a Boolean bl, I share your support is the driving force!

First, the introduction of

Interface design is the daily operations of our developers. When we interfaces to the front-end staff, whether a kind of sword scabbard illusion. After delivery interfaces, our development work done on stage. However, if we do not have a standardized interface design, the result would happen then? Let Photo feel.

Two, REST

In 2000, a young man (Roy Thomas Fielding) in his doctoral thesis presents REST. REST is a style of software architecture World Wide Web. Why is not the standard style of it? Personal understanding might say the standard is a bit excessive. The young man can not do. Subsequently this style was popularized, across the sea, it is well known to the general public. On the basis of REST, resulting in RESTFUL. What is RESTFUL? Simply put, in line with REST-style interface is RESTFUL.

Three, RESTFUL

A variety of interfaces. As the eyes of a thousand thousand people of Lu Xun. What look like RESTFUL interfaces like?

3.1 HTTP methods

HTTP there are several basic ways. We use some of these conventions specifications.

method effect
GET retrieve data
POST Insert data
PUT update data
delight delete data

From the table, if we can clearly see when we get data about the interface, then we use the GET method.
If we can clearly see that when our interface is about inserting data, we use the POST method.
If we can clearly see that when our interface is about to update data, we use the PUT method.
If we can clearly see when we are about to delete the data interface, so we use DELECT method.

3.2 nouns

In the above we already know what the interface methods need at what time, so now we come to the second step into the design of the interface.

We look at online website interface is kind of how.

The figure we can see that there is a v1, he represents the version number, so we can write on the version number, with v1, v2, v3, etc. are shown in the interface design time.

We found him interfaces are nouns. So we know RESTFUL interface is a noun. For example, we design an interface to obtain data, so we can design

/v1/list 

Above the interface is to get all the data.

When we need a data list, we can design

/v1/list/1 

Above the interface retrieves data list is No. 1, we can get the No. 2, No. 3 data, etc., as long as the change to digital.

3.3 portfolio

Combine the above two steps, we can design the CRUD interface to a restful.

interface method effect
/v1/list GET Get a list
/v1/list POST Add list
/v1/list PUT update list
/v1/list delight Delete List

3.4 Application

The following is the source of the demo

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
@Slf4j
public class LsbRestfulApplication {

    public static void main(String[] args) {
        SpringApplication.run(LsbRestfulApplication.class, args);
    }

    /**
     * 得到所有列表
     * @return
     */
    @RequestMapping(value = "/v1/list",method = RequestMethod.GET)
    public String getList(){
        log.info("得到列表");
        return "得到列表";
    }

    /**
     * 得到列表中的一条
     * @param name
     * @return
     */
    @RequestMapping(value = "/v1/list/{name}",method = RequestMethod.GET)
    public String getListone(@PathVariable("name") String name){
        log.info("得到列表"+name);
        return "得到列表"+name;
    }

    /**
     * 往列表中的增加一条数据
     * @return
     */
    @RequestMapping(value = "/v1/list",method = RequestMethod.POST)
    public String addList(){
        log.info("增加一个列表1");
        return "增加一个列表1";
    }

    /**
     * 更新列表中的一条数据
     * @return
     */
    @RequestMapping(value = "/v1/list/{name}",method = RequestMethod.PUT)
    public String updateListOne(@PathVariable("name") String name){
        log.info("更新列表"+name);
        return "更新列表"+name;
    }
    
    /**
     * 删除所有列表
     * @return
     */
    @RequestMapping(value = "/v1/list",method = RequestMethod.DELETE)
    public String delList(){
        log.info("删除一个列表");
        return "删除一个列表";
    }
}

Fourth, a city dating site

https://github.com/buerbl

Signal: Emperor And The Assassin

Guess you like

Origin www.cnblogs.com/chenzhuantou/p/12210940.html