一、GetMapping
GetMapping的注解是调用的
@RequestMapping(
method = {RequestMethod.GET}
)
1、创建person对象
package com.soul.java.springboot.domain;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.Date;
public class Person {
private String name;
private String password;
private String phone;
private Date birthday;
public Person() {
}
public Person(String name, String password, String phone, Date birthday) {
this.name = name;
this.password = password;
this.phone = phone;
this.birthday = birthday;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
}
2、创建MethodControler
package com.soul.java.springboot.controller;
import com.soul.java.springboot.domain.Person;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
@RestController
public class MethodControler {
@GetMapping("json")
public Person json() {
return new Person("如花","000000","138 383 818 38",new Date());
}
3、启动程序后Postman访问
4、开发常遇到的问题
- 使密码不显示
在Person对象中的password属性上加 @JsonIgnore
import com.fasterxml.jackson.annotation.JsonIgnore;
@JsonIgnore
private String password;
修改后重新请求,密码已经不返回了
- 对生日进行格式化
在Person对象中的birthday属性上加 @JsonFormat(pattern = "YYYY-MM-dd HH:mm:ss")
import com.fasterxml.jackson.annotation.JsonFormat;
@JsonFormat(pattern = "YYYY-MM-dd HH:mm:ss")
private Date birthday;
修改后重新请求
- 使phone返回时显示account
import com.fasterxml.jackson.annotation.JsonProperty;
@JsonProperty("account")
private String phone;
修改后
- 当phone为空Null,不进行传递。将Controller返回值修改为
return new Person("如花", "000000", null, new Date());
import com.fasterxml.jackson.annotation.JsonInclude;
@JsonProperty("account")
@JsonInclude(JsonInclude.Include.NON_NULL) //当电话为null时就不传
private String phone;
效果
二、GetMapping接收参数
1、在MethodControler中添加方法,进行占位传递
//gradeId classId将从外面传进来 接收参数
@GetMapping(path = "/{grad_id}/{class_id}")
public Object find(@PathVariable("grad_id") String gradeId, @PathVariable("class_id") String classId) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("gradeid", gradeId);
map.put("classid", classId);
return map;
}
Postman进行请求
2、在MethodControler中添加方法
@GetMapping("/query")
public Object query(int from, int size) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("from", from);
map.put("size", size);
return map;
}
Postman进行请求,params中进行传参from 和size
3、在MethodControler中添加方法
@GetMapping("/query2")
public Object query2(@RequestParam(defaultValue = "0", name = "page") int from, int size) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("from", from);
map.put("size", size);
return map;
}
Postman进行请求,只传参size,因为from有一个默认值
因为给from加了一个page的名字,所以此时要给from传值,需写page,写from接收不到!
4、token权限的控制,在MethodControler中添加方法
@GetMapping("/header")
public Object header(@RequestHeader("token") String token, String id) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("token", token);
map.put("id", id);
if (token.equals("admin")) {
return "can do something";
} else {
return "no Permission";
}
postman进行请求,token从headers中进行传递,id中params中进行传递
5、从HttpServletRequest接收参数,在MethodControler中添加方法
@GetMapping("/request")
public Object request(HttpServletRequest request) {
String name = request.getParameter("name");
String password = request.getParameter("password");
Map<String, Object> map = new HashMap<String, Object>();
map.put("name", name);
map.put("password", password);
return map;
}
Postman请求,params中传递参数
三、PostMapping和DeleteMapping
1、PostMapping
@PostMapping("/login")
public Object login(String username, String password) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("username", username);
map.put("password", password);
return map;
}
从Body中传递参数
2、DeleteMapping
@DeleteMapping("/del")
public Object del(String id) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("id", id);
return map;
}