How to preserve case sensitivity for property names as specified in MongoDB when returned from MongoTemplate with Spring Boot?

Viewed 189

I have a MongoDB set up with a collection "Data" as follows:

[{"action_id":"A1","Variant":"V1","env":"ENV1"},{...}, ... and so on]

I need to return data from my backend service in the exact same format(case sensitivity) using mongoTemplate and spring boot. The JSON returned from the mongoTemplate is not coming as expected though.

With the the model set up as:-

@Field("action_id")
private String actionId;

@Field("Variant")
private String variant;

@Field("env")
private String env;

I get response as:

{
  actionId: "...",
  variant: "...",
  env: ""
}

But when I change the variable names in the model class to this:

@Field("action_id")
private String action_id;

@Field("Variant")
private String Variant;

@Field("env")
private String env;

I get response as:

{
  action_id: "...",
  variant: "...",
  env: ""
}

For some reason during serialization/de-serialization it changes Variant to variant. I'm guessing it needs camel similar to Jackson.

How can I force MongoTemplate to use the naming convention specified in the db itself? Is there some way to configure this with MongoMappingConverter? If so how?

0 Answers
Related