【问题标题】:Spring Boot JUnit Jackson Cannot Deserialize All FieldsSpring Boot JUnit Jackson 无法反序列化所有字段
【发布时间】:2020-01-09 03:24:58
【问题描述】:

我有一个无法通过的测试用例:

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;
    }

}

@PostMapping
public ResponseEntity<Response<String>> createContact(@Validated @RequestBody ContactDTO contactDTO, BindingResult bindingResult) {
    if (bindingResult.getErrorCount() > 0) {
        LOG.debug("Contact could not validated: {} and won't be created" +
                " Validation error is as follows: {}", contactDTO, bindingResult);

        return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(new Response<>(Error.CONTACT_VALIDATION));
    }
    ...
}

当我调试它时,我看到所有字段都在contactDTO 中填充,然后才发送到控制器。但是,在控制器中,仅填充了电子邮件字段,并导致 HTTP 错误请求。

PS:我使用的是 Spring Boot 2.1.7.RELEASE

【问题讨论】:

  • 你能显示控制器的代码吗?
  • 请同时显示您的 generateContactDTO 代码。
  • 嗨,Kamaci,请创建 ContactDTO 的 JSON 字符串并验证是否已创建正确的 JSON,还显示错误请求的原因,可能是您没有在标头中设置应用程序类型
  • 我已经添加了我的控制器代码。调试时绑定结果有错误。 generateContactDTO() 运行良好,因为它填充了对象的字段。当我通过 Postman 对其进行测试时,我没有收到任何错误。
  • @VishalPawar 我已经从对象创建了 json 字符串,并且那里只写了电子邮件。

标签: java spring spring-boot junit jackson


【解决方案1】:

由于没有公共访问器,Jackson 无法序列化我的 DTO。所以,我在我的 DTO 对象顶部添加了这个:

@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY)

【讨论】:

    猜你喜欢
    • 2020-09-25
    • 2020-05-08
    • 2018-11-17
    • 2019-04-03
    • 2021-03-31
    • 2015-09-01
    • 2018-01-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多