When changing the jackson naming strategy by using the following line in application.properties:
spring.jackson.property-naming-strategy=SNAKE_CASE
autowired JsonMapper objects adhere to the change. But a @RestController does not, properties that have an underscore in them are ignored and result in null values when deserialising.
Just a simple postmapping shows the problem:
@PostMapping
public void create(@RequestBody DemoEntity demoEntity) {
System.out.println("demoEntity: " + demoEntity);
// Output: demoEntity: DemoEntity [id=0, endDate=null]
}
Serialising (@GetMapping) does work correct though.
I have a demo showcasing the problem in a repo: https://github.com/rbetjes/spring-boot-property-naming-strategy-problem
Comment From: bclozel
I have just sent a PR to your repository. I think the problem was that the client used in the test was not serializing the entity to the expected JSON format because it was not configured by Spring Boot to use the configured Jackson serializer.
Using the auto-configured RestTestClient fixes the problem. I also took the liberty to clean up a bit the dependencies to be more in line with Spring Boot 4 best practices.
Comment From: rbetjes
You are right! Improper setup of the testing environment was the cause. I should have tried it with Postman like I normally do :-(. Thank you for the edit's, it fixes the issue in my main project also.