5

I'm making a REST API and I would like to add at the class generation the Jackson annotation to ignore null fields, so I would like to add this annotation onfly for certain classes, not for the hole project.

I know that this could be acomplished by adding in application.properties the following line :

spring.jackson.default-property-inclusion=non_null

But this is for entire project.

I see that there are some ".mustache" files

- api.mustache
- apiController.mustache

I supose that I have to add some code in one of this one, or should I insert some code in application.yml ?

I'm also using Spring Boot with Swagger-codgen.

Thank you in advance and have a nice day!

kaya3
  • 47,440
  • 4
  • 68
  • 97
Florin Lei
  • 521
  • 8
  • 16
  • There is open GitHub issue on this topic. Support / upvote it PLS https://github.com/swagger-api/swagger-codegen/issues/6488 – Michal Foksa Dec 22 '17 at 21:46

3 Answers3

4

Ok, after few hours of research I found that because I'm using Swagger-codegen I have to search in https://github.com/swagger-api/swagger-codegen for all answers regarding Swagger-Codegen. Here are all the templates and I found that I need to add to my project the following two files

  • pojo.mustache
  • xmlAnnotation.mustache

The path where you can find the above files is

swagger-codegen/modules/swagger-codegen/src/main/resources/JavaSpring/

More than that, those files are simple templates to generate your Pojo classes, so you need to add the @JsonInclude(JsonInclude.Include.NON_NULL) annotation in the pojo.mustache file, above the line with public class {{classname}} {{#parent}}extends {{{parent}}}{{/parent}}... in order to be inserted when you generate pojo classes.

And done, build your project again! :)

Florin Lei
  • 521
  • 8
  • 16
1

You can try to set below proprty in application.yaml

spring.jackson.default-property-inclusion = NON_NULL

This worked for me to filter out null values from response.

Dhiraj Surve
  • 310
  • 2
  • 5
0

It is a duplicate of #42997380.

The option NotNullJacksonAnnotation was introduced in Swagger Codegen 2.4.15. You can find details here. Please feel free to use it to have your POJOs annotated with @JsonInclude(Include.NON_NULL).

<build>
    <plugins>
        <plugin>
            <groupId>io.swagger</groupId>
            <artifactId>swagger-codegen-maven-plugin</artifactId>
            <version>2.4.15</version>
            <executions>
                <execution>
                    <id>generate-api</id>
                    <goals>
                        <goal>generate</goal>
                    </goals>
                    <configuration>
                        <inputSpec>${project.basedir}/src/main/resources/swagger-api.yaml</inputSpec>
                        <language>java</language>
                        <modelPackage>org.test.model</modelPackage>
                        <configOptions>
                            <dateLibrary>java8</dateLibrary>
                            <notNullJacksonAnnotation>true</notNullJacksonAnnotation>
                        </configOptions>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
dswan
  • 11
  • 2