前后端接口设计

2,107 阅读2分钟

背景

现在的应用程序,一般都是 1.前后端分离 2.跨设备

所以,前后端接口设计的时候,主要是满足这两点。

后台返回数据

格式

json格式的字符串。

字段说明

字段	字段名字	作用	说明	数据类型
status	状态	请求状态是否成功	成功200,其他失败	int
errorCode	错误码	业务错误代码		string
errorMsg	错误消息	描述业务错误		string
resultData	结果数据	业务数据		json

示例

{
status: 200
errorCode: “”
errorMsg: “”
resultData: {
id:XXX
name:XXX
age:XXX
}
}

总结

前后端接口的定义要适用不同客户端,比如web、app等。所以,必须包含以下几个字段 1.请求状态 2.错误代码和错误消息 3.业务数据

源码实现

响应数据

package com.wz.bpm.bean;

public class RespBean {
    private Integer status=200;
    private String errorCode="";
    private String errorMsg="";
    public String getErrorMsg() {
		return errorMsg;
	}

	public void setErrorMsg(String errorMsg) {
		this.errorMsg = errorMsg;
	}

	public Object getResultData() {
		return resultData;
	}

	public void setResultData(Object resultData) {
		this.resultData = resultData;
	}

	private Object resultData;

    private RespBean() {
    }

    public static RespBean build() {
        return new RespBean();
    }

    public static RespBean ok(String errorMsg, Object resultData) {
        return new RespBean(200, errorMsg, resultData);
    }

    /**
     * 
     * @param errorMsg 描述信息
     * @return
     */
    public static RespBean ok(String errorMsg) {
        return new RespBean(200, errorMsg, null);
    }
    /**
     * 描述信息默认是成功
     * @return
     */
    public static RespBean ok() {
        return new RespBean(200, "成功", null);
    }

    public static RespBean error(String errorMsg, Object resultData) {
        return new RespBean(500, errorMsg, resultData);
    }

    /**
     * 
     * @param errorMsg 描述信息
     * @return
     */
    public static RespBean error(String errorMsg) {
        return new RespBean(500, errorMsg, null);
    }
    /**
     * 描述信息默认是失败
     * @return
     */
    public static RespBean error() {
        return new RespBean(500, "失败", null);
    }

    private RespBean(Integer status, String errorMsg, Object resultData) {
        this.status = status;
        this.errorMsg = errorMsg;
        this.resultData = resultData;
    }

    public RespBean(int i, Object resultData) {
    	this.status = i;
        this.resultData = resultData;
	}

	public Integer getStatus() {

        return status;
    }

    public RespBean setStatus(Integer status) {
        this.status = status;
        return this;
    }

   

    
    /**
     * 服务器响应数据
     * @param i。成功1,失败其他。
     */
    public static RespBean printResult(int i) {
    	if (i==1) {
    		return RespBean.ok();
		} else {
			return RespBean.error();
		}
    }

	public String getErrorCode() {
		return errorCode;
	}

	public void setErrorCode(String errorCode) {
		this.errorCode = errorCode;
	}

	/**
	 * 
	 * @param resultData
	 * @return
	 */
	public static RespBean ok(Object resultData) {
		return new RespBean(200,resultData);
	}
}

控制器

package com.wz.bpm.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
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.annotation.RestController;

import com.wz.bpm.bean.Employee;
import com.wz.bpm.bean.RespBean;
import com.wz.bpm.service.EmployeeService;


@RestController
@RequestMapping("/employee")
public class EmployeeController {
    
    @Autowired
    private EmployeeService employeeService;
    
    @RequestMapping(value="/page",method=RequestMethod.GET)
    public RespBean getEmployeesByPage(
    		@RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(defaultValue = "") String keyword,
            Long companyId, Long departmentId,Long workCityId, Long positionId,Integer workStatus
            ){
    	List<Employee> employees =  employeeService.getEmployeesByPage(page, pageSize, keyword,companyId, departmentId, workCityId,  positionId, workStatus);
    	Long count = employeeService.getCount();
    	Map<String,Object> map = new HashMap<>();
    	map.put("employees", employees);
    	map.put("count", count);
    	return RespBean.ok(map);
    }
    
    @RequestMapping(method=RequestMethod.POST)
    public RespBean addEmployee(Employee employee) {
    	int i = employeeService.addEmployee(employee);
    	if (i==1) {
    		return RespBean.ok();
		} else {
			return RespBean.error();
		}
    	 
    }
    
    @RequestMapping(method=RequestMethod.DELETE)
    public RespBean deleteEmployee(Long id) {
    	int i = employeeService.deleteEmployee(id);
    	return RespBean.printResult(i);
    }
    
    @RequestMapping(method=RequestMethod.GET)
    public RespBean getEmployee(Long id) {
    	Employee employee = employeeService.getEmployee(id);
    	return RespBean.ok(employee);
    }
    
    @RequestMapping(method=RequestMethod.PUT)
    public RespBean updateEmployee(Employee employee) {
    	int i = employeeService.updateEmployee(employee);
    	return RespBean.printResult(i);
    }
    
    
}

参考

juejin.cn/post/684490…