如何基於SpringBoot返回一個json對象

開發過程中,接口是必不可少的,那麼提前約定數據格式就成了必不可少的步驟。一般情況下大家都是用json格式來傳遞數據,今天就用spring boot來實現一下返回json對象的步驟。

1、首先創建一個spring boot項目,在spring boot項目下寫一個實體類,隨便寫幾個屬性意思意思。

 
public class Employee {
private String name;
private String passowod;
private String age;
private String gender;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassowod() {
return passowod;
}
public void setPassowod(String passowod) {
this.passowod = passowod;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
}

2、把controller寫起來,如果習慣了寫springmvc,那麼寫出來的代碼就是這樣的了,主要依賴的@Controller、@ResponseBody註解。

@Controller
@RequestMapping("/employee")
public class EmployeeController {
@RequestMapping("/getEmployee")

@ResponseBody
public Employee getEmployee() {
Employee employee = new Employee();
employee.setAge("30");
employee.setGender("男");
employee.setName("傑克");
employee.setPassowod("123456");
return employee;
}
}

3、在springboot下寫法就簡單了一些,用的是@RestController,這個註解就可以替代@Controller+@ResponseBody這兩個註解的作用。

@RestController
public class EmployeeController {
@RequestMapping("/getEmployee")
public Employee getEmployee() {
Employee employee = new Employee();
employee.setAge("30");
employee.setGender("男");
employee.setName("傑克");
employee.setPassowod("123456");
return employee;
}
}

4、執行後得到的頁面上就展示出了employee對象。

{"name":"傑克","passowod":"123456","age":"30","gender":"男"}

5、而我們要返回json格式,就要先了解http請求的返回的狀態碼都有哪些,瞭解了這些狀態碼才可以更好的把後端狀態返回給前端。

HTTP返回的狀態碼也分以下幾大類狀態。

1xx 信息提示

2xx 成功

3xx 重定向

4xx 客戶端錯誤

5xx 服務器錯誤

我們常見的主要幾個狀態

200 成功

401 登錄失敗

404 未找到

500 內部服務器錯誤

我們給前端返回的json對象,這個時候返回的狀態碼也是參照http狀態碼。返回json對象的時候要把狀態碼封裝進去,同時把employee也要封裝進去,那就要一個通用類來響應前端請求。例子中自定義了個操作成功的狀態碼,我們也可以定義其他的狀態碼。

public class ResultJson {
private int code;
private String msg;
private T data;
/**
* 操作成功
*/
public static final ResultJson SUCCESS_RESULT = new ResultJson(0, "操作成功!!!");
/**
* 系統異常

*/
public static final ResultJson SYSTEM_ERROR_RESULT = new ResultJson(1, "系統異常, 請稍後重試!!!");
/**
* 登錄異常
*/
public static final ResultJson LOGIN_ERROR_RESULT = new ResultJson(2, "登錄信息已失效, 請重新登錄!!!");
/**
* 請求參數異常
*/
public static final ResultJson PARAM_ERROR_RESULT = new ResultJson(3, "請求參數異常, 請重試!!!");
/**
* 操作失敗
*/
public static final ResultJson FAIL_RESULT = new ResultJson(4, "操作失敗, 請重試!!!");
/**
* 默認錯誤編碼
*/
public static final int ERROR = 9;

public ResultJson() {
}
public ResultJson(T data) {
this.code = ResultJson.SUCCESS_RESULT.getCode();
this.msg = ResultJson.SUCCESS_RESULT.getMsg();
this.data = data;
}
public ResultJson(int code, String msg) {
this.code = code;
this.msg = msg;
}
public ResultJson(int code, String msg, T data) {
this.code = code;
this.msg = msg;
this.data = data;
}
public ResultJson(ResultJson param, T data) {
this.code = param.getCode();
this.msg = param.getMsg();
this.data = data;
}
public static ResultJson error(String message) {
return (ResultJson) new ResultJson(ERROR, message);

}
public static ResultJson data(T data) {
return (ResultJson) new ResultJson(SUCCESS_RESULT, data);
}
public int getCode() {
return code;
}
public void setCode(int 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;
}
}

6、通過這個響應通用類,傳啥都不是問題,提前寫好返回常量還是很有用的,比如ResultJson.SUCCESS_RESULT表示成功,ResultJson.SYSTEM_ERROR_RESULT 表示系統異常,等等。

7、接下來寫一個返回json的方法,新方法和之前的getEmployee類似,就是把目前的對象給它加上一層殼子,讓他完全符合json格式。

@RequestMapping("/getEmployeeJson")
public ResultJson getEmployeeJson() {
Employee employee = new Employee();
employee.setAge("30");
employee.setGender("男");
employee.setName("傑克");
employee.setPassowod("123456");
return ResultJson.data(employee);
}

8、此時返回的結果就不一樣啦,返回的信息有標記的狀態碼,也有文字提醒,還是返回的數據,完美。

{"code":0,"msg":"操作成功!!!","data":{"name":"傑克","passowod":"123456","age":"30","gender":"男"}}

最後要說的簡單功能寫出來用文字描述出來還是挺困難的,會用是一方面,寫是另外一方面。


分享到:


相關文章: