当前位置:网站首页>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)然后,访问页面去更新/完善用户信息;
边栏推荐
- 7 月数据库排行榜:MongoDB 和 Oracle 分数下降最多
- Oracle was named the champion of Digital Innovation Award by Ventana research
- Personalized online cloud database hybrid optimization system | SIGMOD 2022 selected papers interpretation
- C array supplement
- JVM系列——栈与堆、方法区day1-2
- Rsyslog configuration and use tutorial
- Database lock table? Don't panic, this article teaches you how to solve it
- 免费、好用、强大的轻量级笔记软件评测:Drafts、Apple 备忘录、Flomo、Keep、FlowUs、Agenda、SideNote、Workflowy
- 提高MySQL深分页查询效率的三种方案
- The old-fashioned synchronized lock optimization will make it clear to you at once!
猜你喜欢

逆向调试入门-PE结构-资源表07/07

A data person understands and deepens the domain model

分布式BASE理论

Redis —— How To Install Redis And Configuration(如何快速在 Ubuntu18.04 与 CentOS7.6 Linux 系统上安装 Redis)

Flet教程之 03 FilledButton基础入门(教程含源码)(教程含源码)

动画与过渡效果

CVPR 2022 | transfusion: Lidar camera fusion for 3D target detection with transformer

OpenHarmony应用开发之如何创建DAYU200预览器

光环效应——谁说头上有光的就算英雄

Dgraph: large scale dynamic graph dataset
随机推荐
源码编译安装MySQL
再说rsync+inotify实现数据的实时备份
Is the outdoor LED screen waterproof?
室外LED屏幕防水吗?
Personalized online cloud database hybrid optimization system | SIGMOD 2022 selected papers interpretation
Practice: fabric user certificate revocation operation process
Haproxy high availability solution
Golang sets the small details of goproxy proxy proxy, which is applicable to go module download timeout and Alibaba cloud image go module download timeout
Xilinx/system-controller-c/boardui/ unable to connect to the development board, the solution of jamming after arbitrary operation
Efficient! Build FTP working environment with virtual users
Meituan Ali's Application Practice on multimodal recall
Introduction to XML II
请问大佬们有遇到这个情况吗,cdc 1.4 连接MySQL 5.7 无法使用 timestamp
2022KDD预讲 | 11位一作学者带你提前解锁优秀论文
#yyds干货盘点# 解决名企真题:连续最大和
JVM系列——栈与堆、方法区day1-2
OPPO Find N2产品形态首曝:补齐各项短板
MySQL three-level distribution agent relationship storage
Xue Jing, director of insight technology solutions: Federal learning helps secure the flow of data elements
用fail2ban阻止密码尝试攻














