API 호출에 대한 응답으로 Json Class Object를 응답으로 보냅니다.
빈 개체를 제거하지 않고 이와 같은 응답이 필요합니다.
{
"links": {
"products": [],
"packages": []
},
"embedded":{
"products": [],
"packages": []
}
}
하지만 최종 응답은 다음과 같습니다.
{
"links": {},
"embedded": {}
}
주의해야 할 두 가지 사항 :
null
그리고 empty
다른 것들입니다.null
기본적 으로 값으로 속성을 직렬화하도록 구성됩니다 .개체에서 속성을 올바르게 초기화해야합니다. 예를 들면 :
class Dto {
private Link link;
private Embedded embedded;
//constructor, getters and setters...
}
class Link {
//by default these will be empty instead of null
private List<Product> products = new ArrayList<>();
private List<Package> packages = new ArrayList<>();
//constructor, getters and setters...
}
클래스가이 주석으로 다른 클래스를 확장하지 않는지 확인하십시오 @JsonInclude(JsonInclude.Include.NON_NULL)
. 예:
//It tells Jackson to exclude any property with null values from being serialized
@JsonInclude(JsonInclude.Include.NON_NULL)
class BaseClass {
}
//Any property with null value will follow the rules stated in BaseClass
class Dto extends BaseClass {
private Link link;
private Embedded embedded;
//constructor, getters and setters...
}
class Link extends BaseClass {
/* rest of the design */
}
후자가 있고 편집 할 수없는 BaseClass
경우 특정 클래스에서 다른 규칙을 정의 할 수 있습니다.
class Link extends BaseClass{
//no matter what rules are defined elsewhere, this field will be serialized
@JsonInclude(JsonInclude.Include.ALWAYS)
private List<Product> products;
//same here
@JsonInclude(JsonInclude.Include.ALWAYS)
private List<Package> packages;
//constructor, getters and setters...
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다