xml版本模板链接:https://pan.baidu.com/s/1foZrxFjQwf8YBpqVriN2Pg
提取码:3ldi
注解版本模板链接:https://pan.baidu.com/s/1mxY9QMletUlzcsZ2VfqfQg
提取码:g5gh
在这里需要进行格式统一:
- 我们的结果分为三块:将这三个信息封装到一起,就可以实现格式的统一
- 这个操作的操作结果是什么样
- 这个操作操作完也没有数据,对应的数据是什么
- 这个操作过来了过后,后台对前台还有什么交代
-
Result.java实体类
package com.yy.controller.results; public class Result { // 操作结果编码 private Integer code; // 操作数据结果 private Object data; // 消息 private String message; //code状态码的构造方法 public Result(Integer code) { this.code = code; } //在这个构造方法里面,添加状态码和数据 public Result(Integer code, Object data) { this.code = code; this.data = data; } @Override public String toString() { return "Result{" + "code=" + code + ", data=" + data + ", message='" + message + ''' + '}'; } public Integer getCode() { return code; } public void setCode(Integer code) { this.code = code; } public Object getData() { return data; } public void setData(Object data) { this.data = data; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } } -
code.java实体类
package com.yy.controller.results; public class Code { // 这里面的编码根据业务结果来进行的 // 操作结果编码 public static final Integer SAVE_OK = 20011; public static final Integer UPDATE_OK = 20021; public static final Integer DELETE_OK = 20031; public static final Integer GET_OK = 20041; public static final Integer SAVE_ERROR = 20010; public static final Integer UPDATE_ERROR = 20020; public static final Integer DELETE_ERROR = 20030; public static final Integer GET_ERROR = 20040; // 系统错误编码 // 操作权限编码 // 校验结果编码 } -
在controller进行数据封装UserController.java
package com.yy.controller; import com.github.pagehelper.PageInfo; import com.yy.controller.results.Code; import com.yy.controller.results.Result; import com.yy.domain.User; import com.yy.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; //rest风格 @RestController//相对于两个:@Controller加一个@ResponseBody @RequestMapping("/user")//公共的映射路径,前置统用路径 public class UserController { @Autowired//按类型自动装配 //使用传递过来的业务层接口 private UserService userService; @PostMapping//新增用户发请求使用 public Result save(User user){ boolean flag = userService.save(user); return new Result(flag ? Code.SAVE_OK:Code.SAVE_ERROR); } @PutMapping//修改使用put提交 public Result update(User user){ boolean flag = userService.update(user); return new Result(flag ? Code.UPDATE_OK:Code.UPDATE_ERROR); } @DeleteMapping("/{uuid}")//删除使用delete提交 //这个uuid传递个下面方法的参数里面的uuid,是从路径中取的变量,所以加上@PathVariable(路径变量) public Result delete(@PathVariable Integer uuid){ boolean flag = userService.delete(uuid); return new Result(flag ? Code.DELETE_OK:Code.DELETE_ERROR); } @GetMapping("/{uuid}") public Result get(@PathVariable Integer uuid){ User user = userService.get(uuid); //模拟出现异常,使用条件控制,便于测试结果 // if (uuid == 10 ) throw new BusinessException("查询出错啦,请重试!",Code.GET_ERROR); return new Result(null != user ?Code.GET_OK: Code.GET_ERROR,user); } @GetMapping("/{page}/{size}") public Result getAll(@PathVariable Integer page, @PathVariable Integer size){ PageInfoall = userService.getAll(page, size); return new Result(null != all ?Code.GET_OK: Code.GET_ERROR,all); } @PostMapping("/login")//这个是登录的,但是与上面的添加是一样的使用 @PostMapping, // 所以在这里需要添加一个区分的名称,不然的话会与上面的save区分不出来 //发送get请求是明文,所以使用post请求, public Result login(String userName,String password){//这个会将表单中的值配给这个方法的 User user = userService.login(userName,password); return new Result(null != user ?Code.GET_OK: Code.GET_ERROR,user); } }



