当前位置:网站首页>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 ;
边栏推荐
- Database lock table? Don't panic, this article teaches you how to solve it
- 面试拆解:系统上线后Cpu使用率飙升如何排查?
- [cloud native | kubernetes] in depth understanding of ingress (12)
- Rsyslog configuration and use tutorial
- 2022年山东省安全员C证考试题库及在线模拟考试
- OpenHarmony应用开发之如何创建DAYU200预览器
- Install Trinity and solve error reporting
- XML入门一
- 提高MySQL深分页查询效率的三种方案
- Solution: how to delete the information of Jack in two tables with delete in one statement in Oracle
猜你喜欢
SQL statement syntax error in test SQL statement deletion in eclipse linked database
结合案例:Flink框架中的最底层API(ProcessFunction)用法
ViewBinding和DataBinding的理解和区别
One of the solutions for unity not recognizing riders
Dgraph: large scale dynamic graph dataset
JVM series - stack and heap, method area day1-2
8 expansion sub packages! Recbole launches 2.0!
源码编译安装MySQL
Comparative study of the gods in the twilight Era
数据库公共字段自动填充
随机推荐
C语言集合运算
美国土安全部部长警告移民“不要踏上危险的旅程”
Introduction to XML I
Go zero micro service practical series (IX. ultimate optimization of seckill performance)
The old-fashioned synchronized lock optimization will make it clear to you at once!
c#数组补充
字节面试算法题
#yyds干货盘点# 解决名企真题:连续最大和
基于链表管理的单片机轮询程序框架
Openharmony application development how to create dayu200 previewer
SQL language
CTF competition problem solution STM32 reverse introduction
XILINX/system-controller-c/BoardUI/无法连接开发板,任意操作后卡死的解决办法
安装trinity、解决报错
Five "potential errors" in embedded programming
舔狗舔到最后一无所有(状态机)
源码编译安装MySQL
JVM series - stack and heap, method area day1-2
免费、好用、强大的轻量级笔记软件评测:Drafts、Apple 备忘录、Flomo、Keep、FlowUs、Agenda、SideNote、Workflowy
C语言职工管理系统