一)RESTful简介
定义:RESTful是一种网络应用程序的设计风格和开发方式,基于HTTP,可以使用XML格式定义或JSON格式定义。
RESTful特点:
1、每一个URI都是一个唯一资源标识符;
2、客户端使用GET、POST、PUT、DELETE4个表示操作方式的动词对服务端资源进行操作:GET用来获取资源,POST用来新建资源(也可以用于更新资源),PUT用来更新资源,DELETE用来删除资源;
3、资源的表现形式是XML或HTML或JSON;
4、客户端与服务端之间的交互在请求之间都是无状态的;
二)SpringMVC对RESTful的支持
主要是通过注解来实现功能,如下:
@Controller:声明一个处理请求的控制器
@RequestMapping:请求映射地址到对应的方法,该注解又可以分为一下几种类型:
@GetMapping:获取资源。
@PostMpping:新建资源(也可以用于更新资源)。
@PutMapping:更新资源,主要是用来更新整个资源的。
@DeleteMapping:删除资源。
@PatchMapping:更新资源,主要是用来执行某项操作并更新资源的某些字段。
@ResponsrBody:将响应内容转换为JSON格式。
@RequestBody:请求内容转换为JSON格式。
@RestContrller:等同于@Controller+@ResponsrBody注解。
三)RESTful API统一返回值
第一步:创建一个枚举类,存储业务逻辑的code和msg
package com.oysept.bean;
/**
* 全局统一返回code与msg
* @author ouyangjun
*/
public enum CodeEnum {
RESULT_CODE_SUCCESS("S0000000", "SUCCESS"),
RESULT_CODE_FAIL("F0000000", "FAIL"),
;
private String code;
private String msg;
CodeEnum(String code, String msg) {
this.code = code;
this.msg = msg;
}
public String getCode() {return code;}
public void setCode(String code) {this.code = code;}
public String getMsg() {return msg;}
public void setMsg(String msg) {this.msg = msg;}
}
第二步:创建一个统一返回格式,包括状态码、返回消息、返回数据、返回时间(可自行扩展)
package com.oysept.bean;
import java.io.Serializable;
import java.util.Date;
/**
* 全局返回Result
* @author ouyangjun
*/
public class Result<T> implements Serializable {
private static final long serialVersionUID = 1L;
private String code;
private String msg;
private T data;
private Date time;
public Result() {}
public Result(CodeEnum codeenum, T data) {
this.code = codeenum.getCode();
this.msg = codeenum.getMsg();
this.data = data;
this.time = new Date();
}
public Result(String code, String msg, T data) {
this.code = code;
this.msg = msg;
this.data = data;
this.time = new Date();
}
public String getCode() {return code;}
public void setCode(String code) {this.code = code;}
public String getMsg() {return msg;}
public void setMsg(String msg) {this.msg = msg;}
public T getData() {return data;}
public void setData(T data) {this.data = data;}
public Date getTime() {return time;}
public void setTime(Date time) {this.time = time;}
}
第三步:创建一个实体类,用于传值
package com.oysept.bean;
import java.io.Serializable;
public class ParamsVO implements Serializable {
private static final long serialVersionUID = 1L;
private String id;
private String name;
public String getId() {return id;}
public void setId(String id) {this.id = id;}
public String getName() {return name;}
public void setName(String name) {this.name = name;}
}
四)RESTful GET传参与返回值
引入的类,下面GET和POST方法需要用到:
package com.oysept.controller;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.oysept.bean.CodeEnum;
import com.oysept.bean.ParamsVO;
import com.oysept.bean.Result;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotati