在某些条件下使用jackson创建json并忽略对象

时间:2013-06-27 06:37:50

标签: java jackson

我想仅在某些条件下应用@JsonIgnore。

例如,在一种情况下,我可能只需要测试对象而不是测试中的所有问题。   但在其他情况下,我可能需要使用@JsonManagedReference

来解决所有问题
class Test{
      private string testName; 
      @ManyToOne(fetch=FetchType.LAZY)
  @JoinColumn(name="abc")
      @JsonIgnore
      private Set<question> question;
 }

1 个答案:

答案 0 :(得分:1)

试试@JsonView。它允许您根据序列化期间提供的视图(标记类)有条件地包含/禁止属性。

class Test {
    private String testName;

    @JsonView(SomeCondition.class)
    private Set<Question> questions;
}

@GET
@Path("/testWithCondition")
@JsonView(SomeCondition.class)
public Response getTestWithCondition() {
    Test test = testService.lookupTest();
    return Response.ok(test).build();
}

注意:您可能必须在ObjectMapper上禁用MapperFeature.DEFAULT_VIEW_INCLUSION。

相关问题