当前位置:网站首页>30: Chapter 3: develop Passport Service: 13: develop [change / improve user information, interface]; (use * * * Bo class to accept parameters, and use parameter verification)
30: Chapter 3: develop Passport Service: 13: develop [change / improve user information, interface]; (use * * * Bo class to accept parameters, and use parameter verification)
2022-07-04 13:55:00 【Small withered forest】
explain :
(1) The content of this blog : Development 【 change / Improve user information , Interface 】;
Catalog
(2) stay UserServiceImpl in , Implement this method ;
zero : Rationality of this blog ;( Or rather, :【 change / Improve user information , Interface 】 What is it? )
One : Formal development ;
1. stay 【model】 In model engineering , establish UpdateUserInfoBO class , To undertake 【 change / Improve user information , Interface 】 Parameters of ;
UpdateUserInfoBO class :
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 = " user ID Can't be empty ") private String id; @NotBlank(message = " User nickname cannot be empty ") @Length(max = 12, message = " The user's nickname cannot exceed 12 position ") private String nickname; @NotBlank(message = " User avatar cannot be empty ") private String face; @NotBlank(message = " The real name cannot be empty ") private String realname; @Email @NotBlank(message = " The message cannot be empty ") private String email; // Determines if the string is empty , Best use @NotBlank; And judgment integer, Need to use @NotNull @NotNull(message = " Please choose a gender ") @Min(value = 0, message = " Wrong sex choice ") @Max(value = 1, message = " Wrong sex choice ") private Integer sex; @NotNull(message = " Please select a birthday date ") @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd") // After the front-end date string is transferred to the back-end , Convert to Date type private Date birthday; @NotBlank(message = " Please select your city ") private String province; @NotBlank(message = " Please select your city ") private String city; @NotBlank(message = " Please select your city ") 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; } }
explain :
(1) This BO Class is used to undertake 【 change / Improve user information , Interface 】 Parameters of , That is the content of the following form ;;; therefore , The attributes in this interface are not written in vain ;
(2) We use this BO When class accepts parameters , Also added a lot of checks ;
● utilize @JsonFormat Annotation to solve the problem of time , stay 【RESTful Development style 6:RESTful Basic use IV :JSON serialize ;(jackson Components , And some of them @JsonFormat Annotation to solve the problem of time ;)】 I introduced it for the first time ;
(3) Actually , The content of this form , The front end is also verified ;( This is easy to understand , I used to write it myself C# When the form program , It has also been verified on the front-end form , similar )
2. stay 【api】 Interface Engineering UserControllerApi Interface , Definition 【 change / Improve user information , Interface 】;
/** * 【 modify / Improve user information , Interface 】 * @param updateUserInfoBO * @return */ @ApiOperation(value = " modify / Improve user information ", notes = " modify / Improve user information ", httpMethod = "POST") @PostMapping("/updateUserInfo") // Set the routing , This needs to be agreed between the front and back ; public GraceJSONResult updateUserInfo(@RequestBody @Valid UpdateUserInfoBO updateUserInfoBO, BindingResult result);
explain :
(1) Content description ;
● Once the verification fails , There is an error message , We can go through 【BindingResult result】 To get the corresponding error information ; therefore , Here in the parameter , It has also been introduced. 【BindingResult result】;
3. stay 【user】 User microservices UserController Class , To achieve 【 change / Improve user information , Interface 】;
/** * 【 modify / Improve user information , Interface 】 * * @param updateUserInfoBO * @param result * @return */ @Override public GraceJSONResult updateUserInfo(@Valid UpdateUserInfoBO updateUserInfoBO, BindingResult result) { //0. Judge BindingResult Whether the error message of validation failure is saved in , If there is , It indicates that there is a problem with the input of the front end ; // that , We get this error message , And build a GraceJSONResult Unified return object , return ; if (result.hasErrors()) { Map<String, String> map = getErrorsFromBindingResult(result);// therefore , This class needs to inherit BaseController return GraceJSONResult.errorMap(map); } //1. Perform an update operation userService.updateUserInfo(updateUserInfoBO); return GraceJSONResult.ok(); }
explain :
(1) Content description ;
4. stay 【user】 User microservices UserService Interface , Define a 【 modify / Improve user information , And activate the user 】 Methods ;;; stay UserServiceImpl in , Implement this method ;
(1) stay 【user】 User microservices UserService Interface , Define a 【 modify / Improve user information , And activate the user 】 Methods
……………………………………………………
(2) stay UserServiceImpl in , Implement this method ;
/** * modify / Improve user information , And activate the user ; * * @param updateUserInfoBO */ @Override public void updateUserInfo(UpdateUserInfoBO updateUserInfoBO) { // Pass it from the front updateUserInfoBO Attribute value in ,copy To a AppUser Go to the object ; AppUser userInfo = new AppUser(); BeanUtils.copyProperties(updateUserInfoBO, userInfo); // Reset its update time userInfo.setUpdatedTime(new Date()); // Set its user status , Set its state to 1( Is activated ); userInfo.setActiveStatus(UserStatus.ACTIVE.type); // and mybatis-plus The routine is basically the same , We use updateByPrimaryKeySelective() Method ( This method only updates those in the database table userInfo In some ,, Nothing will move ); // Instead of using updateByPrimaryKey();( This method will completely update the contents of the database table ,,userInfo There is no the , It will be set to empty ), int result = appUserMapper.updateByPrimaryKeySelective(userInfo); if (result != 1) {// If the return value of the above method is not 1, It means that there is something wrong with the update operation , Then we throw an exception ; GraceException.display(ResponseStatusEnum.USER_UPDATE_ERROR); } }
explain :
(1) Content description ;
3、 ... and : test ;
Statement :
We can do it in Swagger2 Test on the page , You can also use Postman To test ;
test :
(1) First, the whole situation install Look at the whole project ;
(2) then , start-up 【user】 Main startup class of microservice ;
(3) then , Visit the page to update / Improve user information ;
边栏推荐
- 易周金融 | Q1保险行业活跃人数8688.67万人 19家支付机构牌照被注销
- Scripy framework learning
- Using nsproxy to forward messages
- Excuse me, have you encountered this situation? CDC 1.4 cannot use timestamp when connecting to MySQL 5.7
- [FAQ] summary of common causes and solutions of Huawei account service error 907135701
- 分布式BASE理论
- C语言程序设计选题参考
- Getting started with microservices
- SQL语言
- 提高MySQL深分页查询效率的三种方案
猜你喜欢
基于STM32+华为云IOT设计的酒驾监控系统
Redis - how to install redis and configuration (how to quickly install redis on ubuntu18.04 and centos7.6 Linux systems)
CommVault cooperates with Oracle to provide metallic data management as a service on Oracle cloud
Personalized online cloud database hybrid optimization system | SIGMOD 2022 selected papers interpretation
SQL statement syntax error in test SQL statement deletion in eclipse linked database
基于链表管理的单片机轮询程序框架
提高MySQL深分页查询效率的三种方案
CVPR 2022 | 大幅减少零样本学习所需的人工标注,提出富含视觉信息的类别语义嵌入(源代码下载)...
Oracle 被 Ventana Research 评为数字创新奖总冠军
MySQL45讲——学习极客时间MySQL实战45讲笔记—— 06 | 全局锁和表锁_给表加个字段怎么有这么多阻碍
随机推荐
C basic supplement
CTF competition problem solution STM32 reverse introduction
DGraph: 大规模动态图数据集
Scripy framework learning
C#基础补充
Introduction to XML III
Go 语言入门很简单:Go 实现凯撒密码
Source code compilation and installation of MySQL
WS2811 M是三通道LED驱动控制专用电路彩灯带方案开发
8 expansion sub packages! Recbole launches 2.0!
2022年起重机械指挥考试模拟100题模拟考试平台操作
C foundation in-depth study I
C語言宿舍管理查詢軟件
微服务入门
OPPO Find N2产品形态首曝:补齐各项短板
[cloud native | kubernetes] in depth understanding of ingress (12)
WPF double slider control and forced capture of mouse event focus
Cann operator: using iterators to efficiently realize tensor data cutting and blocking processing
Web knowledge supplement
Comparative study of the gods in the twilight Era