我有一個無法通過的測試用例:ContactDTO contactDTO = generateContactDTO();HttpEntity<ContactDTO> request = new HttpEntity<>(contactDTO, headers);ResponseEntity<Response> response = restTemplate.exchange(generateBaseUrl() + "/contacts", HttpMethod.POST, request, Response.class);assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);這是我的 ContactDTO 類:public class ContactDTO { @NotNull @Size(min = 2, max = 100) private String firstName; @NotNull @Size(min = 2, max = 100) private String lastName; @NotNull @Size(min = 3, max = 100) private String email; @NotNull @Size(min = 3, max = 50) private String phoneNumber; @JsonCreator(mode = JsonCreator.Mode.PROPERTIES) ContactDTO(@JsonProperty("firstName") @NotNull @Size(min = 2, max = 100) String firstName, @JsonProperty("lastName") @NotNull @Size(min = 2, max = 100) String lastName, @JsonProperty("email") @NotNull @Size(min = 3, max = 50) String email, @JsonProperty("phoneNumber") @NotNull @Size(min = 3, max = 50) String phoneNumber) { this.firstName = firstName; this.lastName = lastName; this.email = email; this.phoneNumber = phoneNumber; } String getFirstName() { return firstName; } void setFirstName(String firstName) { this.firstName = firstName; } String getLastName() { return lastName; } void setLastName(String lastName) { this.lastName = lastName; } public String getEmail() { return email; } private void setEmail(String email) { this.email = email; } String getPhoneNumber() { return phoneNumber; } private void setPhoneNumber(String phoneNumber) { this.phoneNumber = phoneNumber; }}當(dāng)我調(diào)試它時,我發(fā)現(xiàn)所有字段在發(fā)送到控制器之前都已填充在 contactDTO 中。但是,在控制器處,僅填充電子郵件字段,并且會導(dǎo)致 HTTP 錯誤請求。PS:我使用Spring Boot 2.1.7.RELEASE
Spring Boot JUnit Jackson 無法反序列化所有字段
婷婷同學(xué)_
2023-07-13 14:29:22