yangyoupeng

添加JsonDateSerializer

package com.zhaoonline.common.json;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
public class JsonDateSerializer extends JsonSerializer<Date>{
private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
@Override
public void serialize(Date date, JsonGenerator gen, SerializerProvider serializers)
throws IOException, JsonProcessingException {
String formattedDate = dateFormat.format(date);
gen.writeString(formattedDate);
}
}
......@@ -27,17 +27,21 @@ public class TestESClient {
String index="zhaoon1";
String type="test1";
ESClientConfiguration config=new ESClientConfiguration();
config.addHostPorts("127.0.0.1:9200");
config.addHostPorts("192.168.0.162:9200");
ESHttpClient client= new ESHttpClient(index,type,config);
client.init();
Map object=new HashMap<>();
object.put("testKey", "testValue");
String docString=JsonUtils.toJson(object);
String id="1";
boolean result=client.addDoc(id,docString);
Assert.assertTrue(result);
for(int i=0;i<100;i++){
Map object=new HashMap<>();
object.put("testKey"+i, "testValue");
String docString=JsonUtils.toJson(object);
boolean result=client.addDoc(""+i,docString);
}
// Assert.assertTrue(result);
}
......@@ -46,7 +50,7 @@ public class TestESClient {
String index="zhaoon1";
String type="test1";
ESClientConfiguration config=new ESClientConfiguration();
config.addHostPorts("127.0.0.1:9200");
config.addHostPorts("192.168.0.162:9200");
ESHttpClient client= new ESHttpClient(index,type,config);
client.init();
Map object=new HashMap<>();
......@@ -95,7 +99,7 @@ public class TestESClient {
String index="zhaoonline";
String type="service";
ESClientConfiguration config=new ESClientConfiguration();
config.addHostPorts("127.0.0.1:9200");
config.addHostPorts("192.168.0.162:9200");
ESHttpClient client= new ESHttpClient(index,type,config);
client.init();
......@@ -104,11 +108,12 @@ public class TestESClient {
buidler.setPage(0,100);
TermsQuery termsQuery=new TermsQuery("serviceName");
termsQuery.addValue("dubbo2").addValue("dubbo23");
termsQuery.addValue("wx").addValue("wxtest");
buidler.and(termsQuery);
TermQuery termQuery=new TermQuery("serviceName", "dubbo2");
TermQuery termQuery=new TermQuery("serviceName", "wxtest");
buidler.not(termQuery);
QueryResponse reponse=client.query(buidler);
Assert.assertNotNull(reponse);
......
package com.zhaoonline.common.json;
import java.util.Date;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
public class TestDateObject {
@JsonSerialize(using=JsonDateSerializer.class)
public Date date;
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
}
package com.zhaoonline.common.json;
import java.util.Date;
import org.junit.Test;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class TestJsonDateSerializer {
@Test
public void testDateParse() throws JsonProcessingException{
ObjectMapper mapper= new ObjectMapper();
TestDateObject o=new TestDateObject();
o.setDate(new Date(System.currentTimeMillis()));
System.out.println(mapper.writeValueAsString(o));;
}
}