我有以下实体,我希望我的网络服务作为jsons
传递@Entity(name = "tests")
public class Test {
@Id
@GeneratedValue
private int id;
@Column(name = "test_name")
private String name;
@JoinColumn(name = "test_id")
@OneToMany(cascade = CascadeType.REMOVE)
private List<Question> questions;
}
@Entity(name = "questions")
public class Question {
@Id
@GeneratedValue
private int id;
@Column
private String question;
@Column(name = "is_multi_select")
private boolean isMultiSelect;
@JoinColumn(name = "question_id")
@OneToMany(cascade = CascadeType.REMOVE)
private List<Answer> answers;
}
问题是我希望问题列表不包含在我的json中,但我不能让杰克逊忽略它们。
我尝试使用@JsonIgnore
注释问题列表,但没有结果。我该怎么做?
P.S。
忘记提及序列化是通过泽西完成的,这里是实际返回我的测试列表的方法
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("/tests")
public Response getLanguageList() {
List<Test> tests = TestDao.getTests();
return Response.status(200).entity(tests).build();
}
答案 0 :(得分:0)
您必须配置映射器以忽略空数组:
ObjectMapper mapper = new ObjectMapper();
mapper.configure(SerializationFeature.WRITE_EMPTY_JSON_ARRAYS, false);
要忽略特定属性,com.fasterxml.jackson.annotation.JsonIgnore
注释应执行:
@JsonIgnore
@JoinColumn(name = "test_id")
@OneToMany(cascade = CascadeType.REMOVE)
private List<Question> questions;
编辑:您可能需要在web.xml
中启用杰克逊:
<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
</init-param>
或通过代码配置:
ClientConfig clientConfig = new DefaultClientConfig();
clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
Client client = Client.create(clientConfig);
答案 1 :(得分:0)
@JsonInclude(JsonInclude.Include.NON_EMPTY)
NON_EMPTY仅包含非null字段,不包含空集合类型字段。