Toggle navigation
Toggle navigation
This project
Loading...
Sign in
yangyoupeng
/
zhaoonline-utils
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Network
Create a new issue
Builds
Commits
Authored by
yangyoupeng
2016-11-01 19:02:51 +0800
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
0e7a2cc877734c4667acfec2cbf98650efcb0a5e
0e7a2cc8
1 parent
e649a23a
添加JsonDateSerializer
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
73 additions
and
11 deletions
src/main/java/com/zhaoonline/common/json/JsonDateSerializer.java
src/test/java/com/zhaoonline/common/es/TestESClient.java
src/test/java/com/zhaoonline/common/json/TestDateObject.java
src/test/java/com/zhaoonline/common/json/TestJsonDateSerializer.java
src/main/java/com/zhaoonline/common/json/JsonDateSerializer.java
0 → 100644
View file @
0e7a2cc
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
);
}
}
src/test/java/com/zhaoonline/common/es/TestESClient.java
View file @
0e7a2cc
...
...
@@ -27,17 +27,21 @@ public class TestESClient {
String
index
=
"zhaoon1"
;
String
type
=
"test1"
;
ESClientConfiguration
config
=
new
ESClientConfiguration
();
config
.
addHostPorts
(
"1
27.0.0.1
:9200"
);
config
.
addHostPorts
(
"1
92.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
(
"1
27.0.0.1
:9200"
);
config
.
addHostPorts
(
"1
92.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
(
"1
27.0.0.1
:9200"
);
config
.
addHostPorts
(
"1
92.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
);
...
...
src/test/java/com/zhaoonline/common/json/TestDateObject.java
0 → 100644
View file @
0e7a2cc
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
;
}
}
src/test/java/com/zhaoonline/common/json/TestJsonDateSerializer.java
0 → 100644
View file @
0e7a2cc
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
));;
}
}
Please
register
or
login
to post a comment