我正在创建一个更新API,用于更新超级管理员的配置文件,我将成员表Map到DTO,在成员表上,密码设置为非空,并且我没有在DTO上包括密码字段,因为有一个用于此的规定,当我在postman上测试API时,它在控制台上返回
DataIntegrityViolationException
SQL Error: 1048, SQLState: 23000
Column 'password' cannot be null
这是我的代码
德托
@Getter
@Setter
public class UpdateProfileDto {
@NotNull(message = "{member.firstName.notNull}")
@JsonProperty("first_name")
private String firstName;
@NotNull(message = "{member.lastName.notNull}")
@JsonProperty("last_name")
private String lastName;
@JsonProperty("nationality")
private Long nationality;
@JsonProperty("country_of_residence")
private Long countryOfResidence;
@JsonProperty("date_of_birth")
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE)
@JsonFormat(pattern = "dd-MM-yyyy")
@Past(message = "{customer.dateOfBirth.past}")
private Date dateOfBirth;
@JsonProperty("current_job_title")
private String currentJobTitle;
@NotNull(message = "{member.emailAddress.notNull}")
@JsonProperty("email_address")
private String emailAddress;
@JsonProperty("username")
private String username;
@NotNull(message = "{member.phoneNumber.notNull}")
@PhoneNumber
@JsonProperty("phone_number")
private String phoneNumber;
@Size(max = 300, message = "{member.city.size}")
@JsonProperty("city")
private String city;
@Size(max = 300, message = "{member.state.size}")
@JsonProperty("state")
private String state;
}
服务实现
@Override
@Transactional
public Member updateProfile(UpdateProfileDto body) {
Optional<Role> existingRole = roleJpaRepository.findByCode(RoleType.RECRUITER.getValue());
if (existingRole.isEmpty()) {
throw new RoleNotFoundException(RoleType.RECRUITER.getValue());
}
Country nationality = countryRepository.getOne(body.getNationality());
if (nationality == null) {
throw new CountryNotFoundException(body.getNationality());
}
Country countryOfResidence = countryRepository.getOne(body.getCountryOfResidence());
if (countryOfResidence == null) {
throw new CountryNotFoundException(body.getCountryOfResidence());
}
Member superAdmin = mapper.map(body, Member.class);
superAdmin.getRoles().add(existingRole.get());
superAdmin.setNationality(nationality);
superAdmin.setCountryOfResidence(countryOfResidence);
return repository.updateOne(superAdmin);
}
主计长
@RestController
@RequestMapping(
value = "super-admin",
produces = { MediaType.APPLICATION_JSON_VALUE }
)
public class SuperAdminController {
private final SuperAdminService service;
public SuperAdminController(SuperAdminService service) {
this.service = service;
}
@PutMapping("/update")
public Member updateProfile(@Valid @RequestBody UpdateProfileDto body){
Member superAdmin = service.updateProfile(body);
return superAdmin;
}
}
1条答案
按热度按时间dwbf0jvd1#
根据具体的使用情形,您的选择很少。
1.提取现有密码,使用
UpdateProfileDto
中的唯一属性,电子邮件看起来可以完成这项工作。伪代码:
1.为密码设置一个虚拟值,以便稍后更新。
1.使数据库中的列可为空。