操作结果返回信息
package com.xibei.batis.utils; import lombok.Data; //操作结果返馈信息 @Data public class Result<T> { private Integer code; private String msg; private T data; }
操作返回结果信息
package com.xibei.batis.utils; public class ResultUtil { /*共用方法,成功操作信息*/ public static Result success(Object obj){ Result result = new Result(); result.setCode(0); result.setMsg("成功"); result.setData(obj); return result; } public static Result success() { return success(null); } /*共用方法:失败信息*/ public static Result error(Integer code,String msg) { Result result = new Result(); result.setCode(code); result.setMsg(msg); return result; } }
控制器实表表单验证
@PostMapping(value = "/add") public Result Save_dataobject(@Valid Girl girl, BindingResult bindingResult) { Result result = null; if(bindingResult.hasErrors()) { result = ResultUtil.error(-1,bindingResult.getFieldError().getDefaultMessage()); return result; } girl.setName(girl.getName()); girl.setCupsize(girl.getCupsize()); girl.setAge(girl.getAge()); int rows = girlService.InsertOneData(girl); if(rows >0) result= ResultUtil.success(girl); return result; }
实体类:
package com.xibei.batis.entities; import lombok.Data; import org.springframework.stereotype.Component; import javax.validation.constraints.Min; import javax.validation.constraints.NotNull; @Component @Data public class Girl { @NotNull(message = "必填项") private String name; private String cupsize; @Min(value = 20,message="你输入的数小于20") private Integer age; }
POSMAN操作结果