Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
yangxiujun
/
paidan_demo
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit 17a82b39
authored
Sep 20, 2023
by
刘鑫
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix: jackson java8 时间类接收错误
1 parent
416174e3
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
36 additions
and
0 deletions
project-order/src/main/java/com/dituhui/pea/order/config/MvcConfig.java
project-order/src/main/java/com/dituhui/pea/order/config/MvcConfig.java
0 → 100644
View file @
17a82b3
package
com
.
dituhui
.
pea
.
order
.
config
;
import
com.fasterxml.jackson.annotation.JsonTypeInfo
;
import
com.fasterxml.jackson.databind.DeserializationFeature
;
import
com.fasterxml.jackson.databind.ObjectMapper
;
import
com.fasterxml.jackson.databind.SerializationFeature
;
import
com.fasterxml.jackson.databind.jsontype.impl.LaissezFaireSubTypeValidator
;
import
com.fasterxml.jackson.datatype.jsr310.JavaTimeModule
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.http.converter.HttpMessageConverter
;
import
org.springframework.http.converter.json.MappingJackson2HttpMessageConverter
;
import
org.springframework.web.servlet.config.annotation.WebMvcConfigurer
;
import
java.util.List
;
@Configuration
public
class
MvcConfig
implements
WebMvcConfigurer
{
@Override
public
void
extendMessageConverters
(
List
<
HttpMessageConverter
<?>>
converters
)
{
MappingJackson2HttpMessageConverter
messageConverter
=
new
MappingJackson2HttpMessageConverter
();
ObjectMapper
objectMapper
=
new
ObjectMapper
();
objectMapper
.
configure
(
DeserializationFeature
.
FAIL_ON_UNKNOWN_PROPERTIES
,
false
);
objectMapper
.
configure
(
SerializationFeature
.
WRITE_DATES_AS_TIMESTAMPS
,
false
);
objectMapper
.
configure
(
SerializationFeature
.
FAIL_ON_EMPTY_BEANS
,
false
);
objectMapper
.
configure
(
DeserializationFeature
.
FAIL_ON_INVALID_SUBTYPE
,
false
);
//不适用默认的dateTime进行序列化,使用JSR310的LocalDateTimeSerializer
objectMapper
.
configure
(
SerializationFeature
.
WRITE_DATE_KEYS_AS_TIMESTAMPS
,
false
);
//重点,这是序列化LocalDateTIme和LocalDate的必要配置,由Jackson-data-JSR310实现
objectMapper
.
registerModule
(
new
JavaTimeModule
());
//必须配置,有兴趣参考源码解读
objectMapper
.
activateDefaultTyping
(
LaissezFaireSubTypeValidator
.
instance
,
ObjectMapper
.
DefaultTyping
.
NON_FINAL
,
JsonTypeInfo
.
As
.
PROPERTY
);
messageConverter
.
setObjectMapper
(
objectMapper
);
converters
.
add
(
0
,
messageConverter
);
}
}
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