@RequestMapping("/update") public Result login() { List<String> arr =new ArrayList<>(); arr.add("123"); arr.add("456"); return Result.success("获取成功",arr); }
public class Result { public Result(int code,String meg,Object data) { this.code=code; this.meg=meg; this.data=data; } private int code; private String meg; private Object data; public Object getData() { return data; } public void setData(Object data) { this.data = data; } public int getCode() { return code; } public void setCode(int code) { this.code = code; } public String getMeg() { return meg; } public void setMeg(String meg) { this.meg = meg; } public static Result success(String meg,Object data) { return new Result(200,meg,data); } public static Result error(String meg,Object data) { return new Result(-1,meg,data); } @Override public String toString() { return "Result{" + "code=" + code + ", meg='" + meg + '\'' + ", data=" + data + '}'; } }
如果返回的数据是 对象 或者 集合 就会自动转换成JSON格式
public class User { private String user; private String password; public String getUser() { return user; } public void setUser(String user) { this.user = user; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Override public String toString() { return "User{" + "user='" + user + '\'' + ", password='" + password + '\'' + '}'; } }
@RequestMapping("/update") public User login(User user){ return user; }
如果是列表就会变成JSON数组形式
@RequestMapping("/update") public List<User> login(){ List<User> list = new ArrayList<User>(); User user = new User(); for(int i=0;i<100;i++) { user.setUser("123456"); user.setPassword("dpc123"); list.add(user); } return list; }