Swagger codegen ignore null fields for POJO classes - spring-boot

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!

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! :)

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.

Related

Spring Boot - autoconfigure if any property with prefix exists

I'm trying to register an autoconfiguration with #ConditionalOnProperty. This configuration should be created when there is any property with a predefined prefix.
For example, this configuration should be created when there is one of the properties listed below
test.property.any-text-goes-here.some-nested-property=test
test.property.nested.nested-two=another
I've tried to use #ConditionalOnProperty with name=test.property, but it doesn't seem to be working. Maybe this can be sorted out using #ConditionalOnExpression, but I have no idea how SpEL expression should look like.
I'm wondering if there a way to achieve this without the need of implementing custom Condition.
Thanks for your help!
Unfortunately, it is not possible. #ConditionalOnProperty evaluates and compares only final property values in flat structure. It does not work on objects :(

Spring Boot : how auto configure works and #JsonTest

I've read some stuff about how auto-configuration works behind the scene (configuration classes with #Conditional, spring.factories inside /META-INF etc...)
Now I'm trying to understand with an example : #JsonTest
I can see this annotation is annotated with things like #AutoConfigureJson
What this #AutoConfigureJson does exactly ? Does it import some configuration classes with beans inside ? How Spring know how to use this annotation (basically this annotation is almost empty and doesn't say which classes to scan)
#AutoConfigure... (like #AutoConfigureJson) annotations are the way to allow tests with multiple "slices".
Slices load into your tests only a subset of the application, making them run faster. Let's say you need to test a component that uses the Jackson Object Mapper, then you would need the #JsonTest slice. (here is the list of all available slices.)
But you may also need some other part of the framework in your test not just tha single slice; let's say the JPA layer. You may want to annotate the test with both #JsonTest and #DataJpaTest to load both slices. According to the docs, this is not supported.
What you should do instead is choose one of the#...Test annotation, and include the other with an #AutoConfigure... annotation.
#JsonTest
#AutoConfigureDataJpa
class MyTests {
// tests
}
Update:
at a certain point while evaluating the annotation, Spring Boot will hit this line and will pass to the method SpringFactoriesLoader.loadFactoryNames() a source, that is the fully qualified name of the annotation (like interface org.springframework.boot.test.autoconfigure.json.AutoConfigureJson for example).
The loadFactoryNames method will do its magic and read the necessary information from here.
If more details are needed, the best thing is to use a debugger and just follow along all the steps.

Spring boot yaml property binding: collection types

I find Spring Boot's (or spring in general) handling of yaml collections to be a bit peculiar. Collections according to yaml specs should be written in .yaml files as:
myCollection: ['foo', 'bar']
or
myCollection:
- foo
- bar
But neither #Value("${myCollection}") annotation or Environment.getProperty("myCollection", String[].class) (also tried List.class) can read collection properties (returns null). The only method I know of that works is to use #ConfigurationProperties annotation described in spring boot docs.
The problem with #ConfigurationProperties annotation is that (a) it is too verbose if all I want is a single property and (b) it rely on bean injection to get an instance of the #ConfigurationProperties class. Under some circumstances, bean injection is not available and all we have is a reference to Environment (e.g: thru ApplicationContext).
In my particular case, I want to read some properties during ApplicationEnvironmentPreparedEvent event, since it happens before context is built, the listener has to be manually registered and therefore, no bean injection. Via the event argument, I can get a reference to Environment. So, I can read other properties but cannot read collections.
A couple of "solutions" I noted (quoted because I don't find them very satisfactory):
Specify collections in .yaml file as myCollection: foo, bar. But this is not ideal because, the format isn't really yaml anymore.
Read individual elements using an index, for example Environment.getProperty("myCollection[0]", String.class). Will require some not-so-elegant utility methods to read and put all elements into a List.
So, my questions is - What is a good way to read collection-type properties if I cannot use #ConfigurationProperties? Also curious why comma-separated format works but not yaml-style collections.
EDIT: corrected some typos
Quite Frankly Spring boot application.properties and application.yaml or application.yml is meant to load configuration properties.
The #ConfigurationProperties annotation is designed as an abstraction to hide the implementations of configuration properties and support both .properties and .yaml/.yml.
For yaml/yml however Spring uses org.yaml.snakeyaml.Yaml library underneath to parse the file and load it to a Properties object inside org.springframework.boot.env.YamlPropertySourceLoader and a Collection is mapped as a Set not an array or List. So you try doing the following;
Environment.getProperty("myCollection", Set.class)

Programmatic way to build a hal+json from a list with certain object in spring-rest / Jackson

For testing purposes im searching for a elegant and less error prune way to build a hal+json data structure based on a Java list of certain objects.
Currently im using a quite huge ugly String for mapping/defining a expected hal+json data structure. I could place this of course also into a file but still imho its a bit error prune. As soon as a object/property would change i also would need to change my hard coded hal+json string/file...
Does anybody knows a helper class or something what could help to build the hal+json based on Java objects?
Spring HATEOAS helps you with generation of hal+json response .
you have to take care of following configuration
on any spring configuration class add #EnableHypermediaSupport(type = { HypermediaType.HAL })
2.make sure that you have Jackson library on classpath
3.Java object extends ResourceSupport or you wrap java object around Resource.
more details on Resource
4.This should generate hal+json response.
5.Add specific links to resource like self , to other resources .
Please click here for more details on Links

Reusing Spring RequestMapping parsing functionality

I have some properties like
/my/{custom}/url
I would need to replace {custom} with some value at runtime
I know that Spring is using "#RequestMapping" with a similar syntax for #PathAttribute matching.
I'm wondering if there is some Class I can reuse from Spring to achieve what I need.
A good option for this is to use a UriComponentsBuilder - see reference here: http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/util/UriComponentsBuilder.html
UriComponentsBuilder.fromPath("/test/{one}/{two}").buildAndExpand(map).toUriString()

Resources