当前位置:网站首页>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 ;
边栏推荐
猜你喜欢

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

Haproxy high availability solution

CA: efficient coordinate attention mechanism for mobile terminals | CVPR 2021

Annual comprehensive analysis of China's mobile reading market in 2022

字节面试算法题

Install Trinity and solve error reporting

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

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

MySQL45讲——学习极客时间MySQL实战45讲笔记—— 06 | 全局锁和表锁_给表加个字段怎么有这么多阻碍

7 月数据库排行榜:MongoDB 和 Oracle 分数下降最多
随机推荐
Lick the dog until the last one has nothing (state machine)
程序员转方向
After the game starts, you will be prompted to install HMS core. Click Cancel, and you will not be prompted to install HMS core again (initialization failure returns 907135003)
提高MySQL深分页查询效率的三种方案
Haproxy high availability solution
嵌入式编程中五个必探的“潜在错误”
SQL语言
Node の MongoDB 安装
Reading cognitive Awakening
Is the outdoor LED screen waterproof?
2022kdd pre lecture | 11 first-class scholars take you to unlock excellent papers in advance
XILINX/system-controller-c/BoardUI/无法连接开发板,任意操作后卡死的解决办法
Interviewer: what is the difference between redis expiration deletion strategy and memory obsolescence strategy?
When MDK uses precompiler in header file, ifdef is invalid
Optional values and functions of the itemized contenttype parameter in the request header
Three schemes to improve the efficiency of MySQL deep paging query
FS4056 800mA充电ic 国产快充电源ic
C#基础深入学习一
A data person understands and deepens the domain model
30:第三章:开发通行证服务:13:开发【更改/完善用户信息,接口】;(使用***BO类承接参数,并使用了参数校验)














