需求:在项目实际开发中,后端接口都按照一定的格式输出给前端调用,在返回中包括状态码和接口的主要作用等注释,再包含实际的data。
直接上代码:
package com.tjgl.gl_transdata.Model.Common;
import java.io.Serializable;
public class ResponseBean implements Serializable {
/**
* HTTP状态码
*/
private Integer code;
/**
* 返回信息
*/
private String msg;
/**
* 返回的数据
*/
private Object data;
public ResponseBean(int code, String msg, Object data) {
this.code = code;
this.msg = msg;
this.data = data;
}
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 Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
}
在实际使用中,接口都以这种形式返回:
/********************************************* controller示例****************************************************************/
@PostMapping("/getCulvertList")
@LogAnnotation(module = "桥梁涵洞", operator = "涵洞list")
public ResponseBean getCulvertList(){
LinkedHashMap res = operCharacteristicService.getCulvertList();
return new ResponseBean(200, "获取交通统计所有信息", res);
}
@RequestMapping("/runPythonTest")
@LogAnnotation(module = "视频python脚本", operator = "runPythonTest")
public ResponseBean runPythonTest(@RequestBody String paramStr){
try{
return new ResponseBean(200,"成功",operCharacteristicService.runPythonTest(paramStr));
}catch (Exception e){
logger.error(e.getMessage());
e.printStackTrace();
return new ResponseBean(400,"错误","");
}
}
返回: