我在项目中使用Jackson lib,我希望以特定格式将数据存储在Mongo DB中。
TestQueries.java
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.TimeZone;
import java.util.regex.Pattern;
import org.jongo.MongoCollection;
public class TestQueries {
public static void main(String[] args) {
MyObj myObj = new MyObj();
myObj.setDt(new Date());
getCollection("testCollection").save(myObj);
System.out.println("Created object");
Iterator itr = getCollection("testCollection").find().as(MyObj.class).iterator();
while(itr.hasNext()){
System.out.println(((MyObj)itr.next()).getDt());
}
}
private static Object getDate() {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yyyy");
Date result = null;
try {
result = simpleDateFormat.parse("01/01/1900");
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
private static MongoCollection getCollection(String documentName) {
PlayJongo.openConnection();
return PlayJongo.getCollection(documentName);
}
}
我的POJO 的 MyObj.java
import java.io.Serializable;
import java.util.Date;
public class MyObj implements Serializable{
private boolean isTrue;
private boolean isFalse;
private Date dt;
/**
* @return the isTrue
*/
public boolean isTrue() {
return isTrue;
}
/**
* @param isTrue the isTrue to set
*/
public void setTrue(boolean isTrue) {
this.isTrue = isTrue;
}
/**
* @return the isFalse
*/
public boolean isFalse() {
return isFalse;
}
/**
* @param isFalse the isFalse to set
*/
public void setFalse(boolean isFalse) {
this.isFalse = isFalse;
}
/**
* @return the dt
*/
public Date getDt() {
return dt;
}
/**
* @param dt the dt to set
*/
public void setDt(Date dt) {
this.dt = dt;
}
}
这是我的Service类,它使用Jongo
创建与mongo DB的连接PlayJongo.java
import java.text.SimpleDateFormat;
import java.util.Date;
import org.jongo.Jongo;
import org.jongo.MongoCollection;
import org.jongo.marshall.jackson.JacksonMapper;
import org.jongo.marshall.jackson.JacksonMapper.Builder;
import com.fasterxml.jackson.core.Version;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.mongodb.DB;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientOptions;
import com.mongodb.ServerAddress;
/**
* Class used to establish connection to MongoDb using Jongo
*/
public class PlayJongo {
private static MongoClient mongoClient = null;
private static Jongo jongo = null;
private final static Class LOGGING_CLASS = PlayJongo.class;
//private static boolean debug = Logger.isAllowedToLog(PlayJongo.class.getCanonicalName());
private PlayJongo(){
}
/**
* Establishes connection with MongoDB if no connection already exists.
*/
public static void openConnection() {
long startTime = System.currentTimeMillis();
if (mongoClient == null) {
try {
String connectionUrl = "127.0.0.1";
int connectionPort =27017;
String dbName ="test_DB";
MongoClientOptions options = MongoClientOptions
.builder()
.connectionsPerHost(20)
.autoConnectRetry(true).build();
mongoClient = new MongoClient(new ServerAddress(connectionUrl, connectionPort), options);
DB db = mongoClient.getDB(dbName);
ObjectMapper objectMapper = new ObjectMapper();
JsonSerializer<Object> ser = new ObjectToDate();
objectMapper.setDateFormat(new SimpleDateFormat("dd/MM/yyyy HH:mm:sss aaa"));
Builder builder = new JacksonMapper.Builder(objectMapper);
jongo = new Jongo(db,builder.build());
} catch (Exception e) {
e.printStackTrace();
}
} else {
System.out.println("Already eastablished");
}
long endTime = System.currentTimeMillis();
}
/**
* Returns the MongoCollection with specified name
*
* @param name
* @return {@link MongoCollection}
*/
public static MongoCollection getCollection(String name) {
return jongo.getCollection(name);
}
/**
* Used to close the Mongo Connection. Should be called only at application
* shutdown.
*/
public static void closeConnection() {
long startTime = System.currentTimeMillis();
try {
mongoClient.close();
} catch (Exception e) {
e.printStackTrace();
}
long endTime = System.currentTimeMillis();
}
}
我在PlayJongo.java中遇到错误,我试图在TestQueries.java文件中保存对象。
Builder builder = new JacksonMapper.Builder(objectMapper);
,错误是,
Exception in thread "main" java.lang.IndexOutOfBoundsException
at java.nio.Buffer.checkIndex(Unknown Source)
at java.nio.HeapByteBuffer.get(Unknown Source)
at org.bson.io.BSONByteBuffer.get(BSONByteBuffer.java:51)
at org.bson.LazyBSONObject.sizeCString(LazyBSONObject.java:640)
at org.bson.LazyBSONObject.getElement(LazyBSONObject.java:435)
at org.bson.LazyBSONObject.get(LazyBSONObject.java:415)
at com.mongodb.LazyWriteableDBObject.get(LazyWriteableDBObject.java:93)
at org.jongo.Insert$LazyIdDBObject.get(Insert.java:105)
at com.mongodb.DBCollection.save(DBCollection.java:1040)
at org.jongo.Insert.save(Insert.java:49)
at org.jongo.MongoCollection.save(MongoCollection.java:128)
at TestQueries.main(TestQueries.java:21)
我在这里做错了吗?
我正在使用Jackson 2.5。