2017-08-01 30 views
0

這裏我使用的實體類我想刪除零和「」從JSON性反應的輸出值數據

import java.util.ArrayList; 
import com.fasterxml.jackson.annotation.JsonInclude; 
@JsonInclude(JsonInclude.Include.NON_EMPTY) 
public class MappingDTO { 

    private String mid; 
    private String location; 
    private String department; 
    private String role; 
    private String tenent_id; 
    private String cid; 
    private ArrayList<CategoryDTO> categoryDTOAL; 
    //setters and getters 
} 

註釋,並使用廣口瓶

<dependency> 
<groupId>com.fasterxml.jackson.core</groupId> 
<artifactId>jackson-annotations</artifactId> 
<version>2.6.0</version> 
</dependency> 

我的電流輸出

{ 
"mid":"1", 
"location":"", 
"department":"IT" 
"role":"Developer", 
"tenent_id":"", 
"cid":"1001", 
"categoryDTOAL":null 
} 

預計產量

{ 
"mid":"1", 
"department":"IT" 
"role":"Developer", 
"cid":"1001", 
} 

如何我是解決這個問題時,我的這個註解失敗,如果你想忽略空或「」值,那麼你可以TRU @JsonInclude(Include.NON_NULL)@JsonInclude(Include.NON_EMPTY) begore類來解決這個問題

+0

看到這個答案https://stackoverflow.com/a/16089705/2310289 –

+0

最後我m到處回答 ' org.codehaus.jackson 傑克遜映射器 - 翔升 1.5.0 '添加此jar並使用Annotation'@JsonSerialize(include = JsonSerialize.Inclusion.NON_EMPTY)'這裏我導入'import org.codehaus.jackson.map.annotate.JsonSerialize;' –

回答

0

你可以找到下面的示例代碼:

@JsonInclude(Include.NON_NULL) 
public static class Test { 

} 

你也可以在任何固定柱使用@JsonIgnore根據您的使用情況。

+0

所以OP的代碼有什麼問題? –

+0

你好@Mayank夏爾馬它沒有在我的情況下工作.... –

+0

然後嘗試使用'@JsonSerialize(包括=包含.NON_NULL)' intead of @JsonInclude(JsonInclude.Include.NON_EMPTY)'或嘗試使用@ JsonInclude(JsonInclude.Include.NON_NULL) –