@RestController publicclassHelloController { @RequestMapping("/hello") public String hello(){ System.out.println("Hello World ~"); return"Hello World ~"; } }
//增删改 成功响应(不需要给前端返回数据) publicstatic Result success(){ returnnewResult(1,"success",null); } //查询 成功响应(把查询结果做为返回数据响应给前端) publicstatic Result success(Object data){ returnnewResult(1,"success",data); } //失败响应 publicstatic Result error(String msg){ returnnewResult(0,msg,null); } }
改造Controller:
@RestController publicclassResponseController { //响应统一格式的结果 @RequestMapping("/hello") public Result hello(){ System.out.println("Hello World ~"); //return new Result(1,"success","Hello World ~"); return Result.success("Hello World ~"); }
//响应统一格式的结果 @RequestMapping("/getAddr") public Result getAddr(){ Addressaddr=newAddress(); addr.setProvince("广东"); addr.setCity("深圳"); return Result.success(addr); }
//响应统一格式的结果 @RequestMapping("/listAddr") public Result listAddr(){ List<Address> list = newArrayList<>();