RESTful request

The difference between a RESTful request and a normal request

查询 controller/getUser?id=1 controller/user/1 get
添加 controller/createUser?name=xxx&age=23 controller/user post
删除 controller/deletUser?id=1 controller/user/1 delete
修改 controller/updateUser?id=1&name=xxx controller/user put


1.RESTful is to describe resources with url, and parameters are obtained from url.
2. Use the http method to describe the behavior, use the http status code to indicate different results, and decide what method to execute for post or put through the http request.

3. Use json to interact data.

Usage of PathVariable

@PathVariable Get the parameters in the URL resource, you can use regular expressions to control the parameter type.

@GetMapping("/{id:\\d+}") // id: regular expression, "\\d+" is a positive integer 
 public User addUser(@PathVariable(name = "id") Integer idxxx)

To receive the passed parameters through the object , you need to configure the @RequestBody annotation

@PostMapping
    public User createUser(@Valid @RequestBody User user)
@JsonView control object's output parameters
1. Use an interface to declare multiple views.

2. Specify the view on the get method of the value object.

3. Specify the view on the controller method.

The object settings are as follows
 
  
public class Animal {

    public interface DetailView extends SimpleView{} //DetailView输出包含SimpleView的内容

    public interface SimpleView{} //输出除password以外的内容

    private String name;

    private Integer age;

    private String password;

    @JsonView(SimpleView.class)
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @JsonView(SimpleView.class)
    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @JsonView(DetailView.class)
    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}
controller设置如下
@GetMapping("/{id}")
@JsonView(Animal.SimpleView.class) //设置为Animal.DetailView.class则输出password信息
public Animal getAnimal(@PathVariable Integer id){
     Animal animal = new Animal();
     animal.setAge(33);
     animal.setName("cat");
     animal.setPassword("sdfd");
     return animal;
 }




Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325608383&siteId=291194637