When we use @JsonRootName in SpringBoot apps, we found that it does not work.
The maven pom.xml
The domain class:
Run the code, and visit the url:
What we expected:
But we got:
It seems that the @JsonRootName does not work, why did this happen?
3. The Solution
There are two ways to solve this problem.
3.1 Way 1: Use SpringBoot property
Add this line to your src/main/resources/application.properties
Rerun the code, we get this:
3.2 Way 2: Use SpringBoot @Configuration Bean
We can also change the jackson serialization feature by using java config class like this:
Rerun the code, we also get this:
It works too!
4. Why did this happen?
By default, SpringBoot and Jackson object mapper disable the WRAP_ROOT_VALUE feature of serialization. So , you need to enable it explicitly by using Configuration Beans or properties.
The example source code has been uploaded to github, you can visit here to view the example source codes.