1

I would like to see the json response with null fields. Even if set the defalut value as null in the json schema, those fields are not getting displayed. I think, all null fields are getting filtered internally. for ex.

"streetAddress": {
            "line1": "2374 WATER WORKS RD"
            "line2" : null
          },

Currently if i don't set the line2 , it is not getting appeared in the response. Simply i am getting

"streetAddress": {
            "line1": "2374 WATER WORKS RD"
             }

pom.xml

POJO's are generated using maven plugin

<plugin>
    <groupId>org.jsonschema2pojo</groupId>
    <artifactId>jsonschema2pojo-maven-plugin</artifactId>
    <version>0.4.11</version>
    <configuration>
        <sourceDirectory>${basedir}/src/main/resources/schema</sourceDirectory>
        <targetPackage>com.dnb.daas.matchplus.product.ng.schema.request</targetPackage>
        <includeJsr303Annotations>true</includeJsr303Annotations>
        <initializeCollections>false</initializeCollections>
    </configuration>
    <executions>
        <execution>
            <goals>
                <goal>generate</goal>
            </goals>
        </execution>
    </executions>
</plugin>

Json Schema:

"streetAddress": {
  "id": "streetAddress",
  "type": "object",
  "properties": {
    "line1": {
      "id": "line1",
      "type": "string"                        
    },
    "line2": {                        
      "id": "line2",
      "type": "string"
    }
  },
  "additionalProperties": false                   
}

Sample POJO:

@JsonInclude(JsonInclude.Include.NON_NULL)
@Generated("org.jsonschema2pojo")
@JsonPropertyOrder({
    "line1",
    "line2"
})
public class StreetAddress {

    @JsonProperty("line1")
    private String line1;
    @JsonProperty("line2")
    private String line2;

I tried putting a sysout before returning the json response and i could see the null fields are present.

[line1=2374 WATER WORKS RD,line2=AnglebracketOpennullAngleBracketClose]

I am using Swagger and Post Man as the rest client. In both null field are not appearing.

2
  • 1
    have you tried removing @JsonInclude(JsonInclude.Include.NON_NULL)? Commented Jul 8, 2015 at 13:22
  • Good spot... I tried couple of things removing the @JsonInclude(JsonInclude.Include.NON_NULL) also changing it to @JsonInclude(JsonInclude.Include.ALWAYS). In both the case , i could see the null field in my rest client. Thank you very much.. .Can you please tell how to remove this tag or modify this to @JsonInclude(JsonInclude.Include.ALWAYS) as i am using maven to automatically generate pojo's out of json schema. Commented Jul 8, 2015 at 13:27

2 Answers 2

1

If you are using Micronaut Framework then below code works

Application.yml

jackson:
  serializationInclusion: ALWAYS
Sign up to request clarification or add additional context in comments.

Comments

0

This maven config does the trick:

        <plugin>
            <groupId>org.jsonschema2pojo</groupId>
            <artifactId>jsonschema2pojo-maven-plugin</artifactId>
            <version>0.4.37</version>
            <executions>
                <execution>
                    <id>exec-id</id>
                    <configuration>
                        <sourceDirectory>${basedir}/src/main/resources/schema/json</sourceDirectory>
                        <targetPackage>org.jozefowicz</targetPackage>
                        <inclusionLevel>ALWAYS</inclusionLevel>
                    </configuration>
                    <goals>
                        <goal>generate</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.