javaEEday05-请求响应-响应-@ResponseBody&统一响应结果
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
package com.inmind.controller;
|
||||
|
||||
import com.inmind.pojo.Address;
|
||||
import com.inmind.pojo.Result;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
public class ResponseController {
|
||||
|
||||
@RequestMapping("/hello")
|
||||
public Result hello(){
|
||||
System.out.println("hello world");
|
||||
return Result.success("hello world");
|
||||
}
|
||||
|
||||
@RequestMapping("/getAddr")
|
||||
public Result getAddr(){
|
||||
Address address = new Address();
|
||||
address.setCity("常州");
|
||||
address.setProvince("江苏");
|
||||
return Result.success(address);
|
||||
}
|
||||
|
||||
@RequestMapping("/listAddr")
|
||||
public Result listAddr(){
|
||||
ArrayList<Address> list = new ArrayList<>();
|
||||
list.add(new Address("江苏", "常州"));
|
||||
list.add(new Address("浙江", "杭州"));
|
||||
return Result.success(list);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package com.inmind.pojo;
|
||||
|
||||
/**
|
||||
* 统一响应结果封装类
|
||||
*/
|
||||
public class Result {
|
||||
private Integer code ;//1 成功 , 0 失败
|
||||
private String msg; //提示信息
|
||||
private Object data; //数据 date
|
||||
|
||||
public Result() {
|
||||
}
|
||||
public Result(Integer 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;
|
||||
}
|
||||
|
||||
public static Result success(Object data){
|
||||
return new Result(1, "success", data);
|
||||
}
|
||||
public static Result success(){
|
||||
return new Result(1, "success", null);
|
||||
}
|
||||
public static Result error(String msg){
|
||||
return new Result(0, msg, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Result{" +
|
||||
"code=" + code +
|
||||
", msg='" + msg + '\'' +
|
||||
", data=" + data +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user