1

here I am using Annotation on Entity class

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
}

and using Jar is

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

My Current output

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

Expected Output

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

How to i'm solve it when my this Annotation is failed to solve this problem

Cœur
  • 37,241
  • 25
  • 195
  • 267
  • see this answer https://stackoverflow.com/a/16089705/2310289 – Scary Wombat Aug 01 '17 at 06:25
  • 1
    Finally I m getting Answer ` org.codehaus.jackson jackson-mapper-asl 1.5.0 ` add this jar and Use Annotation `@JsonSerialize(include=JsonSerialize.Inclusion.NON_EMPTY)` Here i am importing `import org.codehaus.jackson.map.annotate.JsonSerialize;` –  Aug 02 '17 at 09:45

1 Answers1

0

if you want to ignore null or "" value then you can tru @JsonInclude(Include.NON_NULL) or @JsonInclude(Include.NON_EMPTY) begore class.

you can find below sample code:

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

}

you can also use @JsonIgnore on any fix column as per your use case.

Mayank Sharma
  • 403
  • 2
  • 2
  • so what is wrong with the OP's code? – Scary Wombat Aug 01 '17 at 06:38
  • Hello @Mayank Sharma its not working on my case.... –  Aug 01 '17 at 06:44
  • then try to use `@JsonSerialize(include = Inclusion.NON_NULL)` intead of `@JsonInclude(JsonInclude.Include.NON_EMPTY)` or try to use @JsonInclude(JsonInclude.Include.NON_NULL) – Mayank Sharma Aug 01 '17 at 06:49
  • 1
    @mayank Sharma when i am using '@JsonIgnore' in my "mid" its forcefully remove it . i want to remove only null and "" values. –  Aug 01 '17 at 06:53
  • actually. `@JsonInclude(JsonSerialize.Inclusion.NON_NULL)` works fine into my code and as per documentation version 2.x+ the syntax for this annotation is: `@JsonInclude(JsonSerialize.Inclusion.NON_NULL)` and you are also use `2.6.0`. it should work. can you upgrade the version to `2.8.8`, may be it works. – Mayank Sharma Aug 01 '17 at 07:00
  • Finally I m getting Answer ` org.codehaus.jackson jackson-mapper-asl 1.5.0 ` add this jar and Use Annotation `@JsonSerialize(include=JsonSerialize.Inclusion.NON_EMPTY)` . Here i am importing `import org.codehaus.jackson.map.annotate.JsonSerialize;` –  Aug 02 '17 at 09:49