Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
open-source
/
chejia-smart-admin
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit e6e5377f
authored
May 14, 2021
by
tyfanchz
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
为 jackson 增加读取配置的支持
1 parent
11caa975
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
72 additions
and
0 deletions
src/main/java/com/glsx/admin/config/JacksonMapperConfig.java
src/main/java/com/glsx/admin/config/JacksonMapperConfig.java
0 → 100644
View file @
e6e5377
package
com
.
glsx
.
admin
.
config
;
import
javax.annotation.PostConstruct
;
import
java.text.SimpleDateFormat
;
import
java.util.Map
;
import
java.util.TimeZone
;
import
com.fasterxml.jackson.databind.DeserializationFeature
;
import
com.fasterxml.jackson.databind.ObjectMapper
;
import
com.fasterxml.jackson.databind.SerializationFeature
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.boot.autoconfigure.jackson.JacksonProperties
;
import
org.springframework.context.annotation.Configuration
;
/**
* <p>Description: 用于配置 jackson 序列化和反序列化
* <p>Project: chejia-smart
*
* @author wbh
* @date 2021-05-14
*/
@Configuration
public
class
JacksonMapperConfig
{
@Value
(
"${spring.jackson.date-format:yyyy-MM-dd HH:mm:ss}"
)
private
String
pattern
;
@Value
(
"${spring.jackson.time-zone:GMT+8}"
)
private
String
timeZone
;
@Autowired
(
required
=
false
)
private
ObjectMapper
objectMapper
;
@Autowired
private
JacksonProperties
jacksonProperties
;
@PostConstruct
public
void
init
()
{
if
(
objectMapper
!=
null
)
{
//objectMapper.setDateFormat(new SimpleDateFormat(pattern));
//objectMapper.setTimeZone(TimeZone.getTimeZone(timeZone));
//objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
//objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
//objectMapper.configure(DeserializationFeature.FAIL_ON_IGNORED_PROPERTIES,
// false);
//objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,
// false);
this
.
configDateFormat
(
objectMapper
);
this
.
configTimeZone
(
objectMapper
);
this
.
configSerialization
(
objectMapper
,
jacksonProperties
.
getSerialization
());
this
.
configDeserialization
(
objectMapper
,
jacksonProperties
.
getDeserialization
());
}
}
private
void
configDateFormat
(
ObjectMapper
objectMapper
)
{
objectMapper
.
setDateFormat
(
new
SimpleDateFormat
(
pattern
));
}
private
void
configTimeZone
(
ObjectMapper
objectMapper
)
{
objectMapper
.
setTimeZone
(
TimeZone
.
getTimeZone
(
timeZone
));
}
private
void
configSerialization
(
ObjectMapper
objectMapper
,
Map
<
SerializationFeature
,
Boolean
>
features
)
{
features
.
forEach
(
objectMapper:
:
configure
);
}
private
void
configDeserialization
(
ObjectMapper
objectMapper
,
Map
<
DeserializationFeature
,
Boolean
>
features
)
{
features
.
forEach
(
objectMapper:
:
configure
);
}
}
Write
Preview
Markdown
is supported
Attach a file
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 post a comment