0

I have a listener that listens to a queue. The message from the queue is a json text. I need to process them and then save in a mongodb database. I have used a DTO for incoming json. The problem is I can save the data as lower case only since I have used a DTO. But, the incoming data is upper case. How can I gracefully do this using jackson/spring?

I tried @JsonGetter and @JsonSetter in the DTO. But, that didn't work. It is still saving the data as lower case.

Mini version of my code:

DTO:

public String getMessage() {
return message;
}

@JsonSetter("MESSAGE")
public void setMessage(String message){
this.message = message;
}

Datasaver:

mongoOperations.save(DTO,collectionname);

Document in database:

_id: ObjectId("5da831183852090ddc7075fb")
message: "hi"

I want the data in mongodb as:

_id: ObjectId("5da831183852090ddc7075fb")
MESSAGE: "hi"

The incoming data has key as MESSAGE.So, I would like the same to store. I would not want the DTO fields names to be in uppercase.

Sangames Kumar
  • 45
  • 2
  • 10
  • Have you tried `@JsonProperty("MESSAGE")` on the field? [When is the @JsonProperty property used and what is it used for?](https://stackoverflow.com/questions/12583638/when-is-the-jsonproperty-property-used-and-what-is-it-used-for) – Michał Ziober Oct 17 '19 at 11:49
  • What is the purpose of storing MESSAGE in capital letters in mongoDB? – K.D Oct 17 '19 at 11:49
  • @K.D It is a business requirement. – Sangames Kumar Oct 17 '19 at 11:53
  • @MichałZiober Yes. I have. However, I used both JsonProperty and JsonSetter. Is that wrong? – Sangames Kumar Oct 17 '19 at 11:53
  • @SangamesKumar, if you want to store it in `MongoDB` use appropriate `annotation`. `Jackson` annotations are probably not recognised by `MongoDB`. Take a look at: [Mapping Annotation Overview](https://docs.spring.io/spring-data/data-mongo/docs/current/reference/html/#mapping-usage-annotations), [SpringData Mongo @Column equivalent annotation (@Property?)](https://stackoverflow.com/questions/15628575/springdata-mongo-column-equivalent-annotation-property) – Michał Ziober Oct 17 '19 at 12:21
  • @MichałZiober Thanks! Field annotation worked. – Sangames Kumar Oct 18 '19 at 06:00

2 Answers2

0

As per @MichaelZiober on comment above, none of the annotations related to jackson helped my need. @Field annotation of spring worked.

Sangames Kumar
  • 45
  • 2
  • 10
0

Should work with @JsonProperty("MESSAGE") If not (for some reason) - you could use custom serializer for this field

class CustomStringSerializer extends JsonSerializer<String> {

    @Override
    public void serialize(String value, JsonGenerator jgen, SerializerProvider provider) throws IOException {
        jgen.writeStartObject();
        jgen.writeObjectField("MESSAGE", value);
        jgen.writeEndObject();
    }

}

and init mapper in this way:

    ObjectMapper objectMapper = new ObjectMapper();
    SimpleModule mod = new SimpleModule("message");
    mod.addSerializer(String.class, new CustomStringSerializer());
    objectMapper.registerModule(mod);
Alex Chumakin
  • 56
  • 1
  • 5