当前位置:网站首页>30:第三章:开发通行证服务:13:开发【更改/完善用户信息,接口】;(使用***BO类承接参数,并使用了参数校验)
30:第三章:开发通行证服务:13:开发【更改/完善用户信息,接口】;(使用***BO类承接参数,并使用了参数校验)
2022-07-04 12:48:00 【小枯林】
说明:
(1)本篇博客内容:开发【更改/完善用户信息,接口】;
目录
零:本篇博客合理性说明;(或者说是:【更改/完善用户信息,接口】是什么)
1.在【model】模型工程中,创建UpdateUserInfoBO类,以用于承接【更改/完善用户信息,接口】的参数;
2.在【api】接口工程的UserControllerApi接口中,定义【更改/完善用户信息,接口】;
3.在【user】用户微服务的UserController类中,去实现【更改/完善用户信息,接口】;
4.在【user】用户微服务的UserService接口中,定义一个【修改/完善用户信息,并且激活用户】的方法;;;在UserServiceImpl中,实现这个方法;
(1)在【user】用户微服务的UserService接口中,定义一个【修改/完善用户信息,并且激活用户】的方法
零:本篇博客合理性说明;(或者说是:【更改/完善用户信息,接口】是什么)
一:正式开发;
1.在【model】模型工程中,创建UpdateUserInfoBO类,以用于承接【更改/完善用户信息,接口】的参数;
UpdateUserInfoBO类:
package com.imooc.bo; import com.fasterxml.jackson.annotation.JsonFormat; import org.hibernate.validator.constraints.Length; import javax.validation.constraints.*; import java.util.Date; public class UpdateUserInfoBO { @NotBlank(message = "用户ID不能为空") private String id; @NotBlank(message = "用户昵称不能为空") @Length(max = 12, message = "用户昵称不能超过12位") private String nickname; @NotBlank(message = "用户头像不能为空") private String face; @NotBlank(message = "真实姓名不能为空") private String realname; @Email @NotBlank(message = "邮件不能为空") private String email; //判断字符串是否为空,最好使用@NotBlank;而判断integer,需要使用@NotNull @NotNull(message = "请选择一个性别") @Min(value = 0, message = "性别选择不正确") @Max(value = 1, message = "性别选择不正确") private Integer sex; @NotNull(message = "请选择生日日期") @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd") // 解决前端日期字符串传到后端后,转换为Date类型 private Date birthday; @NotBlank(message = "请选择所在城市") private String province; @NotBlank(message = "请选择所在城市") private String city; @NotBlank(message = "请选择所在城市") private String district; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getNickname() { return nickname; } public void setNickname(String nickname) { this.nickname = nickname; } public String getFace() { return face; } public void setFace(String face) { this.face = face; } public String getRealname() { return realname; } public void setRealname(String realname) { this.realname = realname; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public Integer getSex() { return sex; } public void setSex(Integer sex) { this.sex = sex; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public String getProvince() { return province; } public void setProvince(String province) { this.province = province; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } public String getDistrict() { return district; } public void setDistrict(String district) { this.district = district; } }
说明:
(1)这个BO类就是用于承接【更改/完善用户信息,接口】的参数,也就是下面这个表单的内容;;;所以,这个接口中的属性不是瞎写的;
(2)我们使用这个BO类承接参数的时候,也加了很多校验;
● 利用 @JsonFormat注解来解决时间问题,在【RESTful开发风格6:RESTful基本使用四:JSON序列化;(jackson组件,以及其中的 @JsonFormat注解来解决时间问题;)】第一次介绍过;
(3)其实,这个表单内容,在前端也做了校验;(这个很容易理解,以前自己写C#窗体程序的时候,也在前端的窗体上做过校验,类似)
2.在【api】接口工程的UserControllerApi接口中,定义【更改/完善用户信息,接口】;
/** * 【修改/完善用户信息,接口】 * @param updateUserInfoBO * @return */ @ApiOperation(value = "修改/完善用户信息", notes = "修改/完善用户信息", httpMethod = "POST") @PostMapping("/updateUserInfo") //设置路由,这个是需要前后端约定好的; public GraceJSONResult updateUserInfo(@RequestBody @Valid UpdateUserInfoBO updateUserInfoBO, BindingResult result);
说明:
(1)内容说明;
● 一旦校验失败,有错误信息后,我们就可以通过【BindingResult result】来获取对应的错误信息;所以,这儿在参数中,也引入了【BindingResult result】;
3.在【user】用户微服务的UserController类中,去实现【更改/完善用户信息,接口】;
/** * 【修改/完善用户信息,接口】 * * @param updateUserInfoBO * @param result * @return */ @Override public GraceJSONResult updateUserInfo(@Valid UpdateUserInfoBO updateUserInfoBO, BindingResult result) { //0.判断BindingResult中是否保存了验证失败的错误信息,如果有,说明前端的输入是有问题的; // 那么,我们就获取这个错误信息,并构建一个GraceJSONResult统一返回对象,返回; if (result.hasErrors()) { Map<String, String> map = getErrorsFromBindingResult(result);//所以,这个类需要继承BaseController return GraceJSONResult.errorMap(map); } //1. 执行更新操作 userService.updateUserInfo(updateUserInfoBO); return GraceJSONResult.ok(); }
说明:
(1)内容说明;
4.在【user】用户微服务的UserService接口中,定义一个【修改/完善用户信息,并且激活用户】的方法;;;在UserServiceImpl中,实现这个方法;
(1)在【user】用户微服务的UserService接口中,定义一个【修改/完善用户信息,并且激活用户】的方法
……………………………………………………
(2)在UserServiceImpl中,实现这个方法;
/** * 修改/完善用户信息,并且激活用户; * * @param updateUserInfoBO */ @Override public void updateUserInfo(UpdateUserInfoBO updateUserInfoBO) { //把前端传过来的updateUserInfoBO中的属性值,copy到一个AppUser对象中去; AppUser userInfo = new AppUser(); BeanUtils.copyProperties(updateUserInfoBO, userInfo); //重新设置其更新时间 userInfo.setUpdatedTime(new Date()); //设置其用户状态,把其状态设为1(即已激活); userInfo.setActiveStatus(UserStatus.ACTIVE.type); //和mybatis-plus的套路基本一样,我们使用updateByPrimaryKeySelective()方法(这个方法只去更新数据库表中那些userInfo中有的,,没有的不会动); // 而不使用updateByPrimaryKey();(这个方法会全部更新数据库表的内容,,userInfo没有的,就会设为空了), int result = appUserMapper.updateByPrimaryKeySelective(userInfo); if (result != 1) {//如果上面方法的返回值不为1,就表示这个更新操作出了问题,那么我们就抛出一个异常; GraceException.display(ResponseStatusEnum.USER_UPDATE_ERROR); } }
说明:
(1)内容说明;
三:测试;
声明:
我们可以在Swagger2页面上去测试,也可以使用Postman去测试;
测试:
(1)先全局install一下整个项目;
(2)然后,启动【user】微服务的主启动类;
(3)然后,访问页面去更新/完善用户信息;
边栏推荐
- Introduction to XML II
- Etcd storage, watch and expiration mechanism
- 上汽大通MAXUS正式发布全新品牌“MIFA”,旗舰产品MIFA 9正式亮相!
- C语言程序设计选题参考
- Three schemes to improve the efficiency of MySQL deep paging query
- Go zero micro service practical series (IX. ultimate optimization of seckill performance)
- C语言集合运算
- Haproxy high availability solution
- 逆向调试入门-PE结构-资源表07/07
- C语言小型商品管理系统
猜你喜欢
Flet tutorial 03 basic introduction to filledbutton (tutorial includes source code) (tutorial includes source code)
逆向调试入门-PE结构-资源表07/07
"Pre training weekly" issue 52: shielding visual pre training and goal-oriented dialogue
HAProxy高可用解决方案
Database lock table? Don't panic, this article teaches you how to solve it
ViewBinding和DataBinding的理解和区别
【AI系统前沿动态第40期】Hinton:我的深度学习生涯与研究心法;Google辟谣放弃TensorFlow;封神框架正式开源
Zhongang Mining: in order to ensure sufficient supply of fluorite, it is imperative to open source and save flow
ASP. Net core introduction I
聊聊支付流程的设计与实现逻辑
随机推荐
实时云交互如何助力教育行业发展
洞见科技解决方案总监薛婧:联邦学习助力数据要素安全流通
"Tips" to slim down Seurat objects
remount of the / superblock failed: Permission denied
"Pre training weekly" issue 52: shielding visual pre training and goal-oriented dialogue
Besides, rsync+inotify realizes real-time backup of data
C语言程序设计选题参考
Personalized online cloud database hybrid optimization system | SIGMOD 2022 selected papers interpretation
How real-time cloud interaction helps the development of education industry
提高MySQL深分页查询效率的三种方案
分布式BASE理论
C语言宿舍管理查询软件
【AI系统前沿动态第40期】Hinton:我的深度学习生涯与研究心法;Google辟谣放弃TensorFlow;封神框架正式开源
The old-fashioned synchronized lock optimization will make it clear to you at once!
Distributed base theory
Iptables foundation and Samba configuration examples
微服务入门
程序员转方向
嵌入式编程中五个必探的“潜在错误”
JVM series - stack and heap, method area day1-2