1、SpringBoot2.1.5中Get 接口请求的应用
package com.zyzx.devtool;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;
@RestController
public class DevtoolController {
private final Map<String, Object> params = new HashMap<String, Object>();
/**
* 功能描述: 测试restful 协议, 从路径中获取字段
* @param city_id
* @param user_id
* @return
*/
@GetMapping("/{city_id}/{user_id}")
public Object findObject(@PathVariable("city_id") String city_id, @PathVariable("user_id") String user_id){
params.clear();
params.put("city_id", city_id);
params.put("user_id", user_id);
return params;
}
}
用postman 测试
2 手动设置某些参数的默认值,比如页码,单页数据size等等
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;
@RestController
public class Devool1Controller {
private final Map<String, Object> params = new HashMap<String, Object>();
/**
* 功能描述:测试默认值
*
* @param page
* @param size
* @return
*/
@GetMapping(value = "/list_limit")
public Object listLimit(@RequestParam(defaultValue = "0", name = "page") int page,
@RequestParam(defaultValue = "10", name = "size") int size) {
params.clear();
params.put("page", page);
params.put("size", size);
return params;
}
}
3.我们测试一下通过bean 对象传参
package com.zyzx.devtool; public class User { private Integer id; private String username; private Integer age; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } @Override public String toString() { return "UserDao{" + "id=" + id + ", username='" + username + '\'' + ", age=" + age + '}'; } }