当前位置:网站首页>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 ;
边栏推荐
- Go 语言入门很简单:Go 实现凯撒密码
- Lick the dog until the last one has nothing (state machine)
- C language programming topic reference
- Introduction to XML I
- Xilinx/system-controller-c/boardui/ unable to connect to the development board, the solution of jamming after arbitrary operation
- C语言程序设计选题参考
- C语言图书租赁管理系统
- WS2811 M是三通道LED驱动控制专用电路彩灯带方案开发
- C basic supplement
- 源码编译安装MySQL
猜你喜欢
Node の MongoDB安装
Solution: how to delete the information of Jack in two tables with delete in one statement in Oracle
Introduction to reverse debugging PE structure resource table 07/07
Comparative study of the gods in the twilight Era
Annual comprehensive analysis of China's mobile reading market in 2022
OpenHarmony应用开发之如何创建DAYU200预览器
Automatic filling of database public fields
免费、好用、强大的轻量级笔记软件评测:Drafts、Apple 备忘录、Flomo、Keep、FlowUs、Agenda、SideNote、Workflowy
2022G3锅炉水处理考试题模拟考试题库及模拟考试
HAProxy高可用解决方案
随机推荐
C#基础深入学习一
Three schemes to improve the efficiency of MySQL deep paging query
JVM series - stack and heap, method area day1-2
Redis —— How To Install Redis And Configuration(如何快速在 Ubuntu18.04 与 CentOS7.6 Linux 系统上安装 Redis)
Xilinx/system-controller-c/boardui/ unable to connect to the development board, the solution of jamming after arbitrary operation
2022危险化学品经营单位主要负责人练习题及模拟考试
高质量软件架构的唯一核心指标
Introduction to reverse debugging PE structure resource table 07/07
Optional values and functions of the itemized contenttype parameter in the request header
上汽大通MAXUS正式发布全新品牌“MIFA”,旗舰产品MIFA 9正式亮相!
一次 Keepalived 高可用的事故,让我重学了一遍它
In 2022, it will be es2022 soon. Do you only know the new features of ES6?
HAProxy高可用解决方案
光环效应——谁说头上有光的就算英雄
Solution: how to delete the information of Jack in two tables with delete in one statement in Oracle
Node の MongoDB安装
Personalized online cloud database hybrid optimization system | SIGMOD 2022 selected papers interpretation
Cann operator: using iterators to efficiently realize tensor data cutting and blocking processing
CTF competition problem solution STM32 reverse introduction
Scripy framework learning