[3] springboot learn fast. Parameter accepts a variety of ways

Foreword

In development, we often encounter a variety of parameter passing mode. Here, we come to summarize, springboot can use way to take arguments.

URL parameters

This parameter, in combination url, shaped like url / {id}. In springboot, accept such parameters may be used PathVariable annotation, as follows:

@GetMapping(value = "/param/{id}")
public Object pathVariable(@PathVariable(value = "id") Integer id) {
 return id;
}
复制代码

test

Form parameter type

Such parameters of the form: name = xxx & age = xxx. May be spliced ​​in the url, such as the get method, it may be placed http requestBody inside. This parameter can be used RequestParam notes accepted, such as:

@PostMapping(value = "/param")
public Object queryString(@RequestParam(value = "username") String name,
String remark) {
  Map<String, String> map = new HashMap<>();
  map.put("name", name);
   map.put("remark", remark);
  return map;
}
复制代码

RequestParam annotation used herein, the username parameter receives the name, and it does not use the second parameter RequestParam annotation remark, because if the request parameters and methods consistent with the parameters, it may be omitted RequestParam annotation.

Tests are as follows:

The first: the request directly to the parameters on the url

Url parameters on request

The second: the request parameter on the body

Click on the code postman to view the curl of the format, it can be seen that name = xxx & age = xxx in the form of

json type

This type is the most popular style in the restful, almost all systems use json form of data transfer.

We first define a Person class, used to make parameter accepts categories, including field names to be consistent with the key json parameter.

public static class Person {
 private String username;
 private String remark;
 public String getUsername() {
 return username;
 }
 public void setUsername(String username) {
 this.username = username;
 }
 public String getRemark() {
 return remark;
 }
 public void setRemark(String remark) {
 this.remark = remark;
 }
}
复制代码

We can use annotations to accept json parameter RequestBody

@PostMapping(value = "/param/json")
public Object json(@RequestBody Person person) {
 return person;
}
复制代码

test

If we do not want to define an object to accept parameters, you can use Map to receive.

@PostMapping(value = "/param/map")
public Object map(@RequestBody Map data) {
 System.out.println(data.get("username"));
 System.out.println(data.get("remark"));
 return data;
}
复制代码

Accept the request header

Some interfaces require a transfer request header parameter, such as the use of the system authentication token, token head generally carried in the request, or on a cookie (cookie also request the head). We can use annotations to accept RequestHeader, as follows:

@GetMapping(value = "/param/header")
public Object header(@RequestHeader(value = "token") String token) {
 return token;
}
复制代码

Accept a cookie parameters

Accept the cookie parameter, you can use annotations to accept CookieValue

 @GetMapping(value = "/param/cookie")
 public Object cookie(@CookieValue(name = "token") String token) {
 return token;
 }
复制代码

Reproduced in: https: //juejin.im/post/5d05e5f9f265da1b6c5f74b4

Guess you like

Origin blog.csdn.net/weixin_34252686/article/details/93183791