本文介绍了如何在测试中禁用JsonProperty或JsonIgnore的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在测试中禁用@JsonProperty(access = JsonProperty.Access.WRITE_ONLY)@JsonIgnore?

If there a way to disable @JsonProperty(access = JsonProperty.Access.WRITE_ONLY) or @JsonIgnore on testing?

我正在尝试测试createUser(),但是当我解析User对象时需要启用user.getPassword()方法.

I am trying to test my createUser() but I need user.getPassword() method be enabled when I parse my User object.

如果我注释@JsonProperty行,则它可以工作,但如果这样做,则将在GET /users or GET /users/{id}方法上返回密码字段.

If I comment the @JsonProperty line it works but if that I do so, the password field will be returned on GET /users or GET /users/{id} method.

这是我的考验

@Test
public void createUser() throws Exception {
    User user = UserFactory.newUser();
    String userJson = objectMapper.writeValueAsString(user);

    LOGGER.info("User to register: " + userJson);

    mockMvc.perform(post("/users")
            .content(userJson)
            .contentType(contentType))
            .andExpect(status().isCreated())
            .andExpect(jsonPath("$.id", is(notNullValue())));
}

创建新用户的方法:

public static User newUser() {
    Fairy fairy = Fairy.create();
    Person person = fairy.person();

    User user = new User();
    user.setName(person.getFirstName());
    user.setLastName(person.getLastName());
    user.setEmail(person.getEmail());
    user.setUsername(person.getUsername());
    user.setPassword(person.getPassword());
    user.setSex(person.isMale() ? User.Sex.MALE : User.Sex.FEMALE);
    user.setPhone(person.getTelephoneNumber());
    user.setCountry(person.getAddress().getCity());

    return user;
}

这是用ObjectMapper序列化User对象后得到的json:

This is the json it got after serialize User object with the ObjectMapper:

{
    "createdAt" : null,
    "updatedAt" : null,
    "name" : "Jasmine",
    "lastName" : "Neal",
    "email" : "[email protected]",
    "username" : "jasminen",
    "sex" : "FEMALE",
    "phone" : "321-104-989",
    "country" : "San Francisco"
}

UserController.class方法

@RequestMapping(method = RequestMethod.POST)
public ResponseEntity store(@Valid @RequestBody User user) {
    userService.store(user);
    return new ResponseEntity<Object>(user, HttpStatus.CREATED);
}

User.class属性

@Column(name = "password", length = 100)
@NotNull(message = "error.password.notnull")
@JsonProperty(access = JsonProperty.Access.WRITE_ONLY) // If I comment this, it works
private String password;

这是解决方法吗?

推荐答案

您可以通过添加以下行来禁用所有Jackson注释:

You can disable all Jackson annotations by adding the following line:

objectMapper.disable(MapperFeature.USE_ANNOTATIONS);

有关更多信息,您可以检查此链接.

For more info, you can check this link.

在您的情况下,这应该可行:

In your case, this should work:

@Test
public void createUser() throws Exception {
User user = UserFactory.newUser();
objectMapper.disable(MapperFeature.USE_ANNOTATIONS);
String userJson = objectMapper.writeValueAsString(user);

LOGGER.info("User to register: " + userJson);

mockMvc.perform(post("/users")
        .content(userJson)
        .contentType(contentType))
        .andExpect(status().isCreated())
        .andExpect(jsonPath("$.id", is(notNullValue())));
}

这篇关于如何在测试中禁用JsonProperty或JsonIgnore的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-08 09:58