前后端分离Springboot统一数据返回格式【简单明了】

1、结构:

1个启动类+1个控制层类+1个实体类+3个数据处理工具类(有一个枚举)

前后端分离Springboot统一数据返回格式【简单明了】_第1张图片

2、内容:

pom.xml


	1.8



	
		org.springframework.boot
		spring-boot-starter-web
	
	
	
		com.alibaba
		fastjson
		1.2.47
	

App.java

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class App {
	public static void main(String[] args) {
		SpringApplication.run(App.class, args);
	}
}

DataResult.java

import java.util.Date;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.fastjson.JSONObject;
import com.liandao.xcl.entity.User;
import com.liandao.xcl.util.BaseResponse;

@RestController
public class DataResult {
	@PostMapping("/datass")
	public BaseResponse data(){
		JSONObject json=new JSONObject(true);//加true解决乱序问题
		json.put("User1", new User("张三", "zhangsan", new Date()));
		json.put("User2", new User("李四", "lisi", new Date()));
		json.put("User3", new User("王五", "wangwu", new Date()));
		return BaseResponse.resultmsgd(json);
	}
}

User.java

import java.util.Date;

import com.fasterxml.jackson.annotation.JsonFormat;

public class User {
private String username;
private String password;
@JsonFormat(pattern="yyyy-MM-dd")
private Date createdate;
//get set
public String getUsername() {
	return username;
}
public void setUsername(String username) {
	this.username = username;
}
public String getPassword() {
	return password;
}
public void setPassword(String password) {
	this.password = password;
}
public Date getCreatedate() {
	return createdate;
}
public void setCreatedate(Date createdate) {
	this.createdate = createdate;
}
//构造方法
public User() {
	super();
}
public User(String username, String password, Date createdate) {
	super();
	this.username = username;
	this.password = password;
	this.createdate = createdate;
}
}

BaseResponse.java

public class BaseResponse {
	private ResStatus resstatus;
	private T resdata;
	//get set
	public ResStatus getResstatus() {
		return resstatus;
	}
	public void setResstatus(ResStatus resstatus) {
		this.resstatus = resstatus;
	}
	public T getResdata() {
		return resdata;
	}
	public void setResdata(T resdata) {
		this.resdata = resdata;
	}
	//构造方法
	public BaseResponse() {
		super();
	}
	public BaseResponse(Integer code, String msg,T resdata) {
		super();
		this.resstatus = new ResStatus(code,msg);
		this.resdata = resdata;
	}
	//静态方法
	public static BaseResponse resultall(Integer code, String msg,T resdata){
		return new BaseResponse(code, msg, resdata);
	}
	public static BaseResponse resultmsg(){
		return new BaseResponse(ResResult.success.getCode(), ResResult.success.getMsg(), null);
	}
	public static BaseResponse resultmsgd(T resdata){
		return new BaseResponse(ResResult.success.getCode(), ResResult.success.getMsg(), resdata);
	}
}

ResResult.java(枚举)

public enum ResResult {
	success(0,"成功访问"),
	;
	private Integer code;
	private String msg;
	//get set
	public Integer getCode() {
		return code;
	}
	public void setCode(Integer code) {
		this.code = code;
	}
	public String getMsg() {
		return msg;
	}
	public void setMsg(String msg) {
		this.msg = msg;
	}
	private ResResult(Integer code, String msg) {
		this.code = code;
		this.msg = msg;
	}
}

ResStatus.java

public class ResStatus {
	private Integer code;
	private String msg;
	//get set
	public Integer getCode() {
		return code;
	}
	public void setCode(Integer code) {
		this.code = code;
	}
	public String getMsg() {
		return msg;
	}
	public void setMsg(String msg) {
		this.msg = msg;
	}
	//构造方法
	public ResStatus() {
		super();
	}
	public ResStatus(Integer code, String msg) {
		super();
		this.code = code;
		this.msg = msg;
	}
}

 

3、测试:

前后端分离Springboot统一数据返回格式【简单明了】_第2张图片

你可能感兴趣的:(springboot)