13.07.2015 Views

Spring Data MongoDB - Spring Web Services - Parent - SpringSource

Spring Data MongoDB - Spring Web Services - Parent - SpringSource

Spring Data MongoDB - Spring Web Services - Parent - SpringSource

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

please define productname in your docbook file!import org.springframework.core.convert.converter.Converter;import com.mongodb.BasicDBObject;import com.mongodb.DBObject;public class PersonWriteConverter implements Converter {}public DBObject convert(Person source) {DBObject dbo = new BasicDBObject();dbo.put("_id", source.getId());dbo.put("name", source.getFirstName());dbo.put("age", source.getAge());return dbo;}Reading using a <strong>Spring</strong> ConverterAn example implemention of a Converter that converts from a DBObject ot a Person object is shownnbelowpublic class PersonReadConverter implements Converter {}public Person convert(DBObject source) {Person p = new Person((ObjectId) source.get("_id"), (String) source.get("name"));p.setAge((Integer) source.get("age"));return p;}Registering <strong>Spring</strong> Converters with the MongoConverterThe Mongo <strong>Spring</strong> namespace provides a convenience way to register <strong>Spring</strong> Converters with theMappingMongoConverter. The configuration snippet below shows how to manually register converterbeans as well as configuring the wrapping MappingMongoConverter into a MongoTemplate.You can also use the base-package attribute of the custom-converters element to enable classpathscanning for all Converter and GenericConverter implementations below the given package.1.4.0.BUILD-SNAPSHOT<strong>Spring</strong> <strong>Data</strong> <strong>MongoDB</strong> -Reference Documentation 62

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!