我正在制作一个带有弹簧数据休息的api的过程,并且对响应中生成的字段有点困惑,并且想知道是否有办法摆脱其中的一些。我已将媒体类型设置为APPLICATION_JSON,并且许多项的响应有三个字段:
我想要所有这些字段,但是,内容数组中的每个对象也都具有完全不必要的内容和链接字段。链接指向自身,内容始终为空。以下是返回项目的示例:
{
"id" : 9,
"positionNumber" : "F38520",
"openDate" : "2014-11-11T05:00:00.000+0000",
"description" : "Research: This position is responsible for research related activities including, but not limited to, research design and implementation, preparation of grant proposals to both intramural and extramural funding sources, both collaborating on and leading manuscript development for submissions to peer-review journals.",
"type" : {
"title" : "Teaching and Research Faculty",
"code" : null
},
"category" : null,
"recruitmentType" : {
"name" : "Public"
} },
"content" : [ ],
"links" : [ {
"rel" : "self",
"href" : "https://localhost/v1/items/9"
}, {
"rel" : "item",
"href" : "https://localhost/v1/items/9"
} ]
}
此外,媒体类型必须是APPLICATION_JSON,因此我无法将其设置为使用hal + json。我到处寻找答案。似乎无法解决这个问题。在此先感谢您的帮助!
答案 0 :(得分:-1)
你需要使用这个jar jackson-annotations-2.2.3.jar 并在你的DTO上应用注释,你可以用它来制作json。
@JsonInclude(JsonInclude.Include.NON_NULL) // this will not include null members of class in json
@JsonInclude(JsonInclude.Include.NON_EMPTY) // this will not include empty members of class in json
您可以在课程级别以及班级成员级别上应用它们。
例如。
@JsonInclude(JsonInclude.Include.NON_NULL)
public class myDto {
@JsonInclude(JsonInclude.Include.NON_EMPTY)
public Content[] content;
}