简体   繁体   English

Spring Boot JUnit Jackson无法反序列化所有字段

[英]Spring Boot JUnit Jackson Cannot Deserialize All Fields

I have a test case that cannot pass: 我有一个无法通过的测试用例:

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

Here is my ContactDTO class: 这是我的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));
    }
    ...
}

When I debug it, I see that all fields are populated at contactDTO before it's been send to controller. 当我调试它时,我看到在将所有字段发送到控制器之前已经在contactDTO中进行了填充。 However, at controller, only e-mail field is populated and it results with HTTP Bad Request. 但是,在控制器上,仅填充电子邮件字段,并且其结果是HTTP错误请求。

PS: I use Spring Boot 2.1.7.RELEASE PS:我使用Spring Boot 2.1.7.RELEASE

Since there was no public accessors, Jackson could not serialize my DTO. 由于没有公共访问者,因此Jackson无法序列化我的DTO。 So, I've added this at top of my DTO object: 因此,我将其添加到了DTO对象的顶部:

@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM