3

I'm using spring-data Mongo (1.3.3) as a mechanism for accessing Mongo. My domain objects are written in Groovy and I use Jackson annotations to define properties and names:

@JsonProperty('is_author')
boolean author = false
@JsonProperty('author_info')
AuthorInfo authorInfo

When I persist one of my domain objects to Mongo, the JsonProperty annotation is ignored and the field is persisted using the standard object's field name. By digging in the Spring Data Mongo documentation, I found out that the library expects a @Field annotation to modify the actual field's name in Mongo.

Is there a way to use only the Jackson annotations instead of using two annotations to achieve the same results. Maybe a "customized" version of MappingMongoConverter?

3
  • 1
    If you're using Groovy 2.1+, could you use the AnnotationCollector? mrhaki.blogspot.co.uk/2013/02/… Commented Feb 27, 2014 at 15:00
  • 1
    That's exactly what I did! I was going to update the question, but you were faster. Thanks! Commented Feb 27, 2014 at 15:15
  • Hehe, you did all the work! ;-) Maybe post what you did as an answer to this question? I know I'll upvote it ;-) Commented Feb 27, 2014 at 15:21

1 Answer 1

4

Since my application is in Groovy, I have used the new @AnnotationCollectorAST Transformation (http://blog.andresteingress.com/2013/01/25/groovy-2-1-the-annotationcollector-annotation/) to "merge" the Jackson and the Spring Data Mongo annotations. Here is how it looks like: simple and effective!

package com.someapp
import com.fasterxml.jackson.annotation.JsonProperty 
import groovy.transform.AnnotationCollector 
import org.springframework.data.mongodb.core.mapping.Field 
@AnnotationCollector([Field, JsonProperty]) 
public @interface JsonMongoProperty {}

And here is how it is used:

   @JsonMongoProperty('is_author')
   boolean author = false
   @JsonMongoProperty('author_info')
   AuthorInfo authorInfo
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.