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 ef4971b1
authored
Sep 20, 2023
by
刘鑫
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix: jackson java8 时间类接收错误
1 parent
17a82b39
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
9 additions
and
75 deletions
project-order/src/main/java/com/dituhui/pea/order/config/JacksonConfiguration.java
project-order/src/main/java/com/dituhui/pea/order/config/MvcConfig.java
project-order/src/main/java/com/dituhui/pea/order/controller/PeaApiController.java
project-order/src/main/java/com/dituhui/pea/order/dto/param/CapacityQueryDTO.java
project-order/src/main/java/com/dituhui/pea/order/dto/param/EngineerParamDTO.java
project-order/src/main/java/com/dituhui/pea/order/config/JacksonConfiguration.java
deleted
100644 → 0
View file @
17a82b3
package
com
.
dituhui
.
pea
.
order
.
config
;
import
com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer
;
import
com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer
;
import
com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer
;
import
com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer
;
import
org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
import
java.time.format.DateTimeFormatter
;
/**
* jackson java8 时间类配置
*/
@Configuration
public
class
JacksonConfiguration
{
@Bean
public
Jackson2ObjectMapperBuilderCustomizer
jackson2ObjectMapperBuilderCustomizer
()
{
return
builder
->
{
// formatter
DateTimeFormatter
dateFormatter
=
DateTimeFormatter
.
ofPattern
(
"yyyy-MM-dd"
);
DateTimeFormatter
dateTimeFormatter
=
DateTimeFormatter
.
ofPattern
(
"yyyy-MM-dd HH:mm:ss"
);
// deserializers
builder
.
deserializers
(
new
LocalDateDeserializer
(
dateFormatter
));
builder
.
deserializers
(
new
LocalDateTimeDeserializer
(
dateTimeFormatter
));
// serializers
builder
.
serializers
(
new
LocalDateSerializer
(
dateFormatter
));
builder
.
serializers
(
new
LocalDateTimeSerializer
(
dateTimeFormatter
));
};
}
}
project-order/src/main/java/com/dituhui/pea/order/config/MvcConfig.java
deleted
100644 → 0
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
);
}
}
project-order/src/main/java/com/dituhui/pea/order/controller/PeaApiController.java
View file @
ef4971b
...
@@ -5,6 +5,7 @@ import com.dituhui.pea.order.dto.OrganizationTreeDTO;
...
@@ -5,6 +5,7 @@ import com.dituhui.pea.order.dto.OrganizationTreeDTO;
import
com.dituhui.pea.order.dto.param.CapacityQueryDTO
;
import
com.dituhui.pea.order.dto.param.CapacityQueryDTO
;
import
com.dituhui.pea.order.dto.param.EngineerParamDTO
;
import
com.dituhui.pea.order.dto.param.EngineerParamDTO
;
import
com.dituhui.pea.order.dto.param.ValidGroup
;
import
com.dituhui.pea.order.dto.param.ValidGroup
;
import
com.fasterxml.jackson.annotation.JsonFormat
;
import
lombok.RequiredArgsConstructor
;
import
lombok.RequiredArgsConstructor
;
import
org.springframework.format.annotation.DateTimeFormat
;
import
org.springframework.format.annotation.DateTimeFormat
;
import
org.springframework.validation.annotation.Validated
;
import
org.springframework.validation.annotation.Validated
;
...
@@ -96,8 +97,10 @@ public class PeaApiController {
...
@@ -96,8 +97,10 @@ public class PeaApiController {
@GetMapping
(
"/capacity/engineer"
)
@GetMapping
(
"/capacity/engineer"
)
public
Result
<
CapacityQueryDTO
.
PersonalResult
>
capacityEngineer
(
@NotBlank
@RequestParam
(
"engineerCode"
)
String
engineerCode
,
public
Result
<
CapacityQueryDTO
.
PersonalResult
>
capacityEngineer
(
@NotBlank
@RequestParam
(
"engineerCode"
)
String
engineerCode
,
@NotNull
@DateTimeFormat
(
pattern
=
"yyyy-MM-dd"
)
@NotNull
@DateTimeFormat
(
pattern
=
"yyyy-MM-dd"
)
@JsonFormat
(
pattern
=
"yyyy-MM-dd"
,
timezone
=
"GMT+8"
)
@RequestParam
(
"beginDate"
)
LocalDate
beginDate
,
@RequestParam
(
"beginDate"
)
LocalDate
beginDate
,
@DateTimeFormat
(
pattern
=
"yyyy-MM-dd"
)
@DateTimeFormat
(
pattern
=
"yyyy-MM-dd"
)
@JsonFormat
(
pattern
=
"yyyy-MM-dd"
,
timezone
=
"GMT+8"
)
@RequestParam
(
"endDate"
)
LocalDate
endDate
)
{
@RequestParam
(
"endDate"
)
LocalDate
endDate
)
{
...
...
project-order/src/main/java/com/dituhui/pea/order/dto/param/CapacityQueryDTO.java
View file @
ef4971b
...
@@ -110,7 +110,8 @@ public class CapacityQueryDTO {
...
@@ -110,7 +110,8 @@ public class CapacityQueryDTO {
/**
/**
* 日期 yyyy-MM-dd
* 日期 yyyy-MM-dd
*/
*/
@JsonFormat
(
pattern
=
"yyyy-MM-dd"
)
@DateTimeFormat
(
pattern
=
"yyyy-MM-dd"
)
@JsonFormat
(
pattern
=
"yyyy-MM-dd"
,
timezone
=
"GMT+8"
)
private
LocalDate
date
;
private
LocalDate
date
;
/**
/**
...
@@ -139,14 +140,14 @@ public class CapacityQueryDTO {
...
@@ -139,14 +140,14 @@ public class CapacityQueryDTO {
/**
/**
* 起始时间 yyyy-MM-dd HH:mm:ss
* 起始时间 yyyy-MM-dd HH:mm:ss
*/
*/
@JsonFormat
(
pattern
=
"yyyy-MM-dd HH:mm:ss"
)
@JsonFormat
(
pattern
=
"yyyy-MM-dd HH:mm:ss"
,
timezone
=
"GMT+8"
)
@DateTimeFormat
(
pattern
=
"yyyy-MM-dd HH:mm:ss"
)
@DateTimeFormat
(
pattern
=
"yyyy-MM-dd HH:mm:ss"
)
private
LocalDateTime
beginTime
;
private
LocalDateTime
beginTime
;
/**
/**
* 截止时间 yyyy-MM-dd HH:mm:ss
* 截止时间 yyyy-MM-dd HH:mm:ss
*/
*/
@JsonFormat
(
pattern
=
"yyyy-MM-dd HH:mm:ss"
)
@JsonFormat
(
pattern
=
"yyyy-MM-dd HH:mm:ss"
,
timezone
=
"GMT+8"
)
@DateTimeFormat
(
pattern
=
"yyyy-MM-dd HH:mm:ss"
)
@DateTimeFormat
(
pattern
=
"yyyy-MM-dd HH:mm:ss"
)
private
LocalDateTime
endTime
;
private
LocalDateTime
endTime
;
...
...
project-order/src/main/java/com/dituhui/pea/order/dto/param/EngineerParamDTO.java
View file @
ef4971b
package
com
.
dituhui
.
pea
.
order
.
dto
.
param
;
package
com
.
dituhui
.
pea
.
order
.
dto
.
param
;
import
com.fasterxml.jackson.annotation.JsonFormat
;
import
lombok.Getter
;
import
lombok.Getter
;
import
lombok.Setter
;
import
lombok.Setter
;
import
org.springframework.format.annotation.DateTimeFormat
;
import
org.springframework.format.annotation.DateTimeFormat
;
...
@@ -62,6 +63,7 @@ public class EngineerParamDTO {
...
@@ -62,6 +63,7 @@ public class EngineerParamDTO {
*/
*/
private
Location
location
;
private
Location
location
;
@JsonFormat
(
pattern
=
"yyyy-MM-dd"
,
timezone
=
"GMT+8"
)
@DateTimeFormat
(
pattern
=
"yyyy-MM-dd"
)
@DateTimeFormat
(
pattern
=
"yyyy-MM-dd"
)
private
LocalDate
birth
;
private
LocalDate
birth
;
...
...
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