Exclude null properties from Spring Boot Rest controllers - spring-boot

I have a rest service exposed using spring boot rest controller but with the response i'm sending object's properties those has null values.
For ex : ReponseEntity.ok(list) and that list consist of Objects A with lot of null properties.
Is there an easy way of excluding those null properties with spring boot tools?

You can try this in application.properties file
spring.jackson.default-property-inclusion=non_null
Ref - https://docs.spring.io/spring-boot/docs/2.0.0.M3/reference/html/howto-spring-mvc.html#howto-customize-the-jackson-objectmapper
or you can try following annotation in class level or property level
#JsonInclude(JsonInclude.Include.NON_NULL)

Related

How to access defined in the application.properties file in Spring Boot?

I want to access values provided in application.properties, e.g.:
logging.level.org.springframework.web: DEBUG
logging.level.org.hibernate: ERROR
logging.file=${HOME}/application.log
userBucket.path=${HOME}/bucket
I want to access userBucket.path in my main program in a Spring Boot application
You can use the #Value annotation and access the property in whichever Spring bean you're using
#Value("${userBucket.path}")
private String userBucketPath;

How to reproduce Spring Boot Configuration POJO creation but in Spring Core (No boot)

The Context
As described in the Spring Boot documentation, one of the ways to create a POJO that holds configuration properties values is:
//On the configuration class
#EnableConfigurationProperties(value = {
POJO_CLASS_HERE.class
})
//On the POJO Class
#ConfigurationProperties(prefix = "PROPERTIES_PREFIX_HERE")
#ConstructorBinding
It works fine - when you have Spring boot in the project.
Now, I have another project that has only Spring V4 (core + batch) - No boot.
The Question
How to use/reproduce the Configuration POJO creation of Spring Boot in Spring core ?
Spring Boot - Type Safe Configuration properties

Why createEntityManager is called before my REST controller method

This is more of a Spring JPA (Hibernate) related issue than AspectJ one, I believe.
Spring Boot v2.2.2.RELEASE, Spring v5.2.2.RELEASE, Hibernate v5.4.9.Final, Spring Data Repositories
I am trying to implement Multi-tenancy in my existing single tenant application.
The tenant is chosen by the users via front-end. The tenant id is
stored in HttpSession.
An advice for Rest Controller methods, reads
the tenant id and keeps it in a ThreadLocal variable.
Now I have to enable a Hibernate Filter using the tenant id. For this, an AspectJ advice for my Repository (interface extends JpaRepository) methods work, but gets executed for each repository method call from Service.
This post is about the following (identical) implementations where the advice for enabling Hibernate Filter is fired before the advice for reading the tenant id from HttpSession. How do I correct the order? The advice for reading the tenant id does not make any repository calls.
#AfterReturning(pointcut = "execution(* javax.persistence.EntityManagerFactory+.createEntityManager(..))", returning = "entityManager")
Ref: https://github.com/spring-projects/spring-framework/issues/25125 (This thread contains a non-AOP way also - post-processing transactional EntityManager instances - That gets triggered only when the Spring Boot starts)
#AfterReturning(pointcut="bean(entityManagerFactory) && execution(* createEntityManager(..))", returning="retVal")
Ref: Access to Session using Spring JPA and Hibernate in order to enable filters
I tried setting the application property spring.jpa.open-in-view=false, but that didn't help.

Using #ConfigurationProperties statically - such as on #RequestMapping

Let's ignore for a moment whether doing this is a great idea, but I'm creating Spring Boot AutoConfiguration for an internal library and as part of this I want to auto-register a Controller that accepts GET/POST/DELETE requests (it is responsible for setting/clearing a cookie value for application testing purposes)
The issue is that I would like the request mapping path to be configurable by the end user. I have a #ConfigurationProperties(prefix = "my.configs") class that contains all the configuration values with their defaults for example: private String path = "default-path"
Ideally i'd be able to reference this in my controller like so: #RequestMapping(path=${my.configs.path}) but this does not work, Spring reports that it is unable to find that configuration parameter, if I place it into a properties file instead of into a the type-safe #ConfigurationProperties it works as expected.
I know I could get around this by putting a default value into the Request mapping, but I'd like to understand just what is happening here, and why I cannot statically refer environment variables read / defaulted into #ConfigurationProperties in the way that I can those defined in files.
#RequestMapping is a Spring MVC annotation and it gets processed by Spring MVC - no matter if it is all wrapped in Spring Boot app or not.
#ConfiguationProperties is on the other hand 100% Spring Boot code and to my knowledge both types of properties are processed at different moments during Spring Context startup lifecycle.

Does a Spring controller returning a ListenableFuture needs #AsynchEnable in configuration?

As of spring 4.1, spring controllers accept return value that can be of type ListenableFuture. is returning a ListenableFuture return value sufficient in making the controller async? Or does it also need #enableAsync annotation somewhere in spring configuration file or/and anything else? I am following this tutorial
I found out that what i was looking for is not #enableAsync but a servlet 3.0 property called async-supported. According to this link, spring-boot defaults async-supported to true.
Hence, there is no need of any further configuration to do if you're using spring-boot.

Resources