当前位置:网站首页>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)然后,访问页面去更新/完善用户信息;
边栏推荐
- "Pre training weekly" issue 52: shielding visual pre training and goal-oriented dialogue
- 诸神黄昏时代的对比学习
- 2022年中国移动阅读市场年度综合分析
- 高效!用虚拟用户搭建FTP工作环境
- 基于STM32+华为云IOT设计的酒驾监控系统
- N++ is not reliable
- Cann operator: using iterators to efficiently realize tensor data cutting and blocking processing
- ASP. Net core introduction I
- C#基础补充
- Database lock table? Don't panic, this article teaches you how to solve it
猜你喜欢

一次 Keepalived 高可用的事故,让我重学了一遍它

"Pre training weekly" issue 52: shielding visual pre training and goal-oriented dialogue
Three schemes to improve the efficiency of MySQL deep paging query

Xue Jing, director of insight technology solutions: Federal learning helps secure the flow of data elements

How real-time cloud interaction helps the development of education industry

Samsung's mass production of 3nm products has attracted the attention of Taiwan media: whether it can improve the input-output rate in the short term is the key to compete with TSMC

7 月数据库排行榜:MongoDB 和 Oracle 分数下降最多

室外LED屏幕防水吗?

The only core indicator of high-quality software architecture

OpenHarmony应用开发之如何创建DAYU200预览器
随机推荐
C#基础深入学习二
Solution: how to delete the information of Jack in two tables with delete in one statement in Oracle
ASP.NET Core入门一
JVM series - stack and heap, method area day1-2
Go zero micro service practical series (IX. ultimate optimization of seckill performance)
.NET 使用 redis
Talk about the design and implementation logic of payment process
2022G3锅炉水处理考试题模拟考试题库及模拟考试
Introduction to XML II
XML入门一
XML入门三
Deploy halo blog with pagoda
c#数组补充
SQL语言
How real-time cloud interaction helps the development of education industry
Xilinx/system-controller-c/boardui/ unable to connect to the development board, the solution of jamming after arbitrary operation
2022KDD预讲 | 11位一作学者带你提前解锁优秀论文
Personalized online cloud database hybrid optimization system | SIGMOD 2022 selected papers interpretation
Practice: fabric user certificate revocation operation process
Besides, rsync+inotify realizes real-time backup of data














