Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in / Register
Toggle navigation
B
BasicAPI
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
杜科
BasicAPI
Commits
44e2aa64
Commit
44e2aa64
authored
Feb 10, 2023
by
杜科
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
增加xml转换
parent
027bc448
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
72 additions
and
3 deletions
+72
-3
JsonUtils.java
src/com/yonde/common/JsonUtils.java
+72
-3
No files found.
src/com/yonde/common/JsonUtils.java
View file @
44e2aa64
...
@@ -2,18 +2,19 @@ package com.yonde.common;
...
@@ -2,18 +2,19 @@ package com.yonde.common;
import
com.fasterxml.jackson.annotation.JsonFilter
;
import
com.fasterxml.jackson.annotation.JsonFilter
;
import
com.fasterxml.jackson.core.JsonProcessingException
;
import
com.fasterxml.jackson.core.JsonProcessingException
;
import
com.fasterxml.jackson.databind.JavaType
;
import
com.fasterxml.jackson.databind.*
;
import
com.fasterxml.jackson.databind.JsonNode
;
import
com.fasterxml.jackson.databind.ObjectMapper
;
import
com.fasterxml.jackson.databind.node.ObjectNode
;
import
com.fasterxml.jackson.databind.node.ObjectNode
;
import
com.fasterxml.jackson.databind.ser.FilterProvider
;
import
com.fasterxml.jackson.databind.ser.FilterProvider
;
import
com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter
;
import
com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter
;
import
com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider
;
import
com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider
;
import
com.fasterxml.jackson.dataformat.xml.XmlMapper
;
import
com.fasterxml.jackson.dataformat.xml.ser.ToXmlGenerator
;
import
org.apache.commons.lang.StringUtils
;
import
org.apache.commons.lang.StringUtils
;
import
org.apache.log4j.Logger
;
import
org.apache.log4j.Logger
;
import
org.springframework.core.annotation.AnnotationUtils
;
import
org.springframework.core.annotation.AnnotationUtils
;
import
java.io.IOException
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.io.OutputStream
;
import
java.io.OutputStream
;
import
java.util.ArrayList
;
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.List
;
...
@@ -25,9 +26,14 @@ public class JsonUtils {
...
@@ -25,9 +26,14 @@ public class JsonUtils {
/** @deprecated */
/** @deprecated */
@Deprecated
@Deprecated
private
static
ObjectMapper
objectMapper
=
null
;
private
static
ObjectMapper
objectMapper
=
null
;
private
static
XmlMapper
xmlMapper
=
null
;
public
JsonUtils
()
{
public
JsonUtils
()
{
}
}
static
{
xmlMapper
=
initXmlMapper
();
}
private
static
ObjectMapper
getObjectMapper
()
{
private
static
ObjectMapper
getObjectMapper
()
{
if
(
ObjectsUtil
.
isNull
(
objectMapper
))
{
if
(
ObjectsUtil
.
isNull
(
objectMapper
))
{
...
@@ -41,6 +47,69 @@ public class JsonUtils {
...
@@ -41,6 +47,69 @@ public class JsonUtils {
return
objectMapper
;
return
objectMapper
;
}
}
private
static
XmlMapper
initXmlMapper
()
{
XmlMapper
newXmlMapper
=
new
XmlMapper
();
//newXmlMapper.findAndRegisterModules();
//newXmlMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
newXmlMapper
.
enable
(
SerializationFeature
.
INDENT_OUTPUT
);
newXmlMapper
.
disable
(
DeserializationFeature
.
FAIL_ON_IGNORED_PROPERTIES
);
newXmlMapper
.
disable
(
SerializationFeature
.
FAIL_ON_EMPTY_BEANS
);
//newXmlMapper.setDefaultUseWrapper(false);
newXmlMapper
.
configure
(
ToXmlGenerator
.
Feature
.
WRITE_XML_DECLARATION
,
true
);
return
newXmlMapper
;
}
/**
* 对象转为XML字符串
* @param value
* @return
* @throws JsonProcessingException
*/
public
static
String
toXMLStr
(
Object
value
)
throws
JsonProcessingException
{
return
xmlMapper
.
writeValueAsString
(
value
);
}
/**
* 对象转xml写入流
* @param value
* @return
* @throws IOException
*/
public
static
void
writeXmlStr
(
OutputStream
os
,
Object
value
)
throws
IOException
{
xmlMapper
.
writeValue
(
os
,
value
);
}
/**
* xml字符串转为对象
* @param <T>
* @param xmlString
* @param clazz
* @return
*/
public
static
<
T
>
T
fromXml
(
String
xmlString
,
Class
<
T
>
clazz
)
{
if
(
StringUtils
.
isEmpty
(
xmlString
))
{
return
null
;
}
try
{
return
xmlMapper
.
readValue
(
xmlString
,
clazz
);
}
catch
(
IOException
e
)
{
log
.
error
(
"parse xml string error:"
+
xmlString
,
e
);
return
null
;
}
}
public
static
<
T
>
T
fromXml
(
InputStream
xmlInput
,
Class
<
T
>
clazz
)
{
if
(
xmlInput
==
null
)
{
return
null
;
}
try
{
return
xmlMapper
.
readValue
(
xmlInput
,
clazz
);
}
catch
(
IOException
e
)
{
log
.
error
(
"parse xml input error"
,
e
);
return
null
;
}
}
public
static
String
toJsonStr
(
Object
value
)
{
public
static
String
toJsonStr
(
Object
value
)
{
return
toJsonStr
(
value
,
getObjectMapper
());
return
toJsonStr
(
value
,
getObjectMapper
());
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment