【问题标题】:Java JSON - Override @JsonIgnore for testsJava JSON - 覆盖 @JsonIgnore 以进行测试
【发布时间】:2019-05-11 15:21:36
【问题描述】:

我在 Spring Boot 中有项目。我有 User 模型,Profile 模型与OneToOne 有什么关系:

用户:(简体)

@Entity
@Table(name = "users")
public class User extends AbstractEntity {

    @Id
    @GeneratedValue
    private Integer id;

    @NotEmpty
    @Basic(optional = false)
    @Column(nullable = false, unique = true)
    private String username;

    @Valid
    @JsonIgnore
    @OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL, optional = false)
    private Profile profile;

    @JsonIgnore
    public Profile getProfile() {
        return profile;
    }

    @JsonProperty
    public void setProfile(Profile profile) {
        this.profile = profile;
    }
}

简介:(简体)

@Entity
@Table(name = "profiles")
public class Profile extends AbstractEntity {

    @Id
    @GeneratedValue
    private Integer id;

    @NotEmpty
    @Basic(optional = false)
    @Column(nullable = false)
    private String name;

    @NotEmpty
    @Basic(optional = false)
    @Column(nullable = false)
    private String surname;

    // Getters, setters, etc
}

我的测试:

 @Test
    public void createUserAndProfileReturnsCreatedStatus() throws Exception {
        final User user = Generator.generateUser();
        user.setProfile(Generator.generateProfile());
        MvcResult mvcResult = this.mockMvc.perform(
                post("/users")
                        .contentType(MediaType.APPLICATION_JSON)
                        .content(toJson(user)))
                .andExpect(status().isCreated())
                .andReturn();
    }

问题是,当我执行user.setProfile() 时,配置文件设置为用户,但是当我调用toJson(user) 时,由于模型中的注释,它会自动被忽略。

如何仅出于测试目的禁用这些注释?可能吗? 我不想从模型中删除@JsonIgnore 注释,因为当我READ 用户GET /users/<id> 时,它们不会暴露个人资料。

【问题讨论】:

  • 由于您的测试似乎是一个集成测试(或多或少),因此您不希望这样做,因为调用资源时您并不期望此输出。
  • @maio290 我希望201 - Created 当我调用POST /users 时,会在数据库中创建新的用户+ 个人资料。当然,我可以手动编写 JSON 请求正文,但它会很痛苦,而且制作起来很慢。
  • 我的错,在你的测试中被误解了一个部分。但是,据我所知,@JsonIgnore 是杰克逊的注释。您可以尝试使用json-b序列化对象,它不应该注意Jackson的注释。

标签: java json spring rest jackson2


【解决方案1】:

这可以通过使用 Jackson 的 Mixin feature 来实现,您可以在其中创建另一个取消忽略注释的类。 mixin 类的唯一要求是具有相同的属性名称和类型。类名不重要,也不需要实例化:

public class DoNotIgnoreProfile
{
    @JsonIgnore(false)
    private Profile profile;
}

需要一个 Jackson 模块来将 bean 和 mixin 绑定在一起:

@SuppressWarnings("serial")
public class DoNotIgnoreProfileModule extends SimpleModule
{
    public DoNotIgnoreProfileModule() {
        super("DoNotIgnoreProfileModule");
    }

    @Override
    public void setupModule(SetupContext context)
    {
        context.setMixInAnnotations(User.class, DoNotIgnoreProfile.class);
    }
}

现在您需要将模块注册到 ObjectMapper 中,一切就绪:

public string toJson(User user)
{
    try {
        ObjectMapper mapper = new ObjectMapper();
        mapper.registerModule(new DoNotIgnoreProfileModule());
        return mapper.writeValueAsString(user);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

编辑: 我刚刚看到ObjectMapper 有一个addMixin() 方法,因此可以跳过整个模块设置

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-07-26
    • 2017-05-03
    • 2011-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-14
    • 1970-01-01
    相关资源
    最近更新 更多