说明:
(1)本篇博客内容:开发【更改/完善用户信息,接口】;
目录
零:本篇博客合理性说明;(或者说是:【更改/完善用户信息,接口】是什么)
一:正式开发;
1.在【model】模型工程中,创建UpdateUserInfoBO类,以用于承接【更改/完善用户信息,接口】的参数;
2.在【api】接口工程的UserControllerApi接口中,定义【更改/完善用户信息,接口】;
3.在【user】用户微服务的UserController类中,去实现【更改/完善用户信息,接口】;
4.在【user】用户微服务的UserService接口中,定义一个【修改/完善用户信息,并且激活用户】的方法;;;在UserServiceImpl中,实现这个方法;
(1)在【user】用户微服务的UserService接口中,定义一个【修改/完善用户信息,并且激活用户】的方法
(2)在UserServiceImpl中,实现这个方法;
三:测试;
零:本篇博客合理性说明;(或者说是:【更改/完善用户信息,接口】是什么)
一:正式开发;
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接口中,定义【更改/完善用户信息,接口】;
@ApiOperation(value = "修改/完善用户信息", notes = "修改/完善用户信息", httpMethod = "POST")
@PostMapping("/updateUserInfo") //设置路由,这个是需要前后端约定好的;
public GraceJSONResult updateUserInfo(@RequestBody @Valid UpdateUserInfoBO updateUserInfoBO,
BindingResult result);
说明:
(1)内容说明;
● 一旦校验失败,有错误信息后,我们就可以通过【BindingResult result】来获取对应的错误信息;所以,这儿在参数中,也引入了【BindingResult result】;
3.在【user】用户微服务的UserController类中,去实现【更改/完善用户信息,接口】;
@Override
public GraceJSONResult updateUserInfo(@Valid UpdateUserInfoBO updateUserInfoBO,
BindingResult result) {
//0.判断BindingResult中是否保存了验证失败的错误信息,如果有,说明前端的输入是有问题的;
// 那么,我们就获取这个错误信息,并构建一个GraceJSONResult统一返回对象,返回;
if (result.hasErrors()) {
Map 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中,实现这个方法;
@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)内容说明;
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接口中,定义【更改/完善用户信息,接口】;
@ApiOperation(value = "修改/完善用户信息", notes = "修改/完善用户信息", httpMethod = "POST")
@PostMapping("/updateUserInfo") //设置路由,这个是需要前后端约定好的;
public GraceJSONResult updateUserInfo(@RequestBody @Valid UpdateUserInfoBO updateUserInfoBO,
BindingResult result);
说明:
(1)内容说明;
● 一旦校验失败,有错误信息后,我们就可以通过【BindingResult result】来获取对应的错误信息;所以,这儿在参数中,也引入了【BindingResult result】;
3.在【user】用户微服务的UserController类中,去实现【更改/完善用户信息,接口】;
@Override
public GraceJSONResult updateUserInfo(@Valid UpdateUserInfoBO updateUserInfoBO,
BindingResult result) {
//0.判断BindingResult中是否保存了验证失败的错误信息,如果有,说明前端的输入是有问题的;
// 那么,我们就获取这个错误信息,并构建一个GraceJSONResult统一返回对象,返回;
if (result.hasErrors()) {
Map 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中,实现这个方法;
@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)然后,访问页面去更新/完善用户信息;
声明:
我们可以在Swagger2页面上去测试,也可以使用Postman去测试;
测试:
(1)先全局install一下整个项目;
(2)然后,启动【user】微服务的主启动类;
(3)然后,访问页面去更新/完善用户信息;



