restApi
package com.example.springboot2.controller;
import com.example.springboot2.pojo.Student;
import com.example.springboot2.util.JSONResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;
@RestController
@RequestMapping("stu")
@Slf4j
public class StuController {
@GetMapping("{stuId}/get")
public String getStu(@PathVariable("stuId") String stuId,
@RequestParam Integer id,
@RequestParam String name){
log.info("stuId="+stuId);
log.info("id="+id);
log.info("name="+name);
return "查询stuId";
}
@PostMapping("create")
public String createStu(@RequestBody Map<String, Object> map,
@RequestHeader("token") String token,
@CookieValue("clientId") String clientId,
HttpServletRequest request) {
log.info("token=" + token);
log.info("clientId=" + clientId);
log.info("map=" + map.toString());
String headerToken = request.getHeader("token");
log.info("token=" + headerToken);
return "新增Stu";
}
@PutMapping("update")
public String updateStu(){
return "修改Stu";
}
@DeleteMapping("delete")
public String deleteStu(){
return "删除Stu";
}
}
接收参数
@PathVariable
: path上的参数
@RequestParam
: query的参数


@RequestBody
: 接收请求体参数
@RequestHeader
: 接收请求头参数
@CookieValue
: 接收cookie
HttpServletRequest
:



响应数据
增加pojo
package com.example.springboot2.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
@Data
@ToString
@NoArgsConstructor
@AllArgsConstructor
public class Student {
public String name;
public Integer age;
}
增加JSONResult
工具类
package com.example.springboot2.util;
public class JSONResult {
private Integer status;
private String msg;
private Object data;
private String ok;
public static JSONResult build(Integer status, String msg, Object data) {
return new JSONResult(status, msg, data);
}
public static JSONResult build(Integer status, String msg, Object data, String ok) {
return new JSONResult(status, msg, data, ok);
}
public static JSONResult ok(Object data) {
return new JSONResult(data);
}
public static JSONResult ok() {
return new JSONResult(null);
}
public static JSONResult errorMsg(String msg) {
return new JSONResult(500, msg, null);
}
public static JSONResult errorUserTicket(String msg) {
return new JSONResult(557, msg, null);
}
public static JSONResult errorMap(Object data) {
return new JSONResult(501, "error", data);
}
public static JSONResult errorTokenMsg(String msg) {
return new JSONResult(502, msg, null);
}
public static JSONResult errorException(String msg) {
return new JSONResult(555, msg, null);
}
public static JSONResult errorUserQQ(String msg) {
return new JSONResult(556, msg, null);
}
public JSONResult() {
}
public JSONResult(Integer status, String msg, Object data) {
this.status = status;
this.msg = msg;
this.data = data;
}
public JSONResult(Integer status, String msg, Object data, String ok) {
this.status = status;
this.msg = msg;
this.data = data;
this.ok = ok;
}
public JSONResult(Object data) {
this.status = 200;
this.msg = "OK";
this.data = data;
}
public Boolean isOK() {
return this.status == 200;
}
public Integer getStatus() {
return status;
}
public void setStatus(Integer status) {
this.status = status;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
public String getOk() {
return ok;
}
public void setOk(String ok) {
this.ok = ok;
}
}
测试
controller
@GetMapping("getStudent")
public JSONResult getStudent(){
Student stu = new Student()
stu.setName("bbb")
stu.setAge(10)
Student stu2 = new Student("ccc",30)
log.debug(stu.toString())
log.debug(stu.toString())
log.debug(stu.toString())
log.debug(stu.toString())
return JSONResult.ok(stu)
// return JSONResult.errorMsg("调用接口错误")
}
响应的数据
