Skip to content

Commit

Permalink
feat($Java): handle LocalDateTime, LocalDate and LocalTime
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnymillergh committed Jun 27, 2021
1 parent 388d5da commit 778e152
Show file tree
Hide file tree
Showing 8 changed files with 118 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.time.LocalDateTime;

/**
* Description: GetUserPageList, change description here.
*
Expand All @@ -13,4 +15,6 @@
@EqualsAndHashCode(callSuper = true)
public class GetUserPageListPayload extends PaginationBase {
private String username;
private LocalDateTime startTime;
private LocalDateTime endTime;
}
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ public String getUserStatus(@Valid @NotNull GetUserStatusPayload payload) {

@Override
public PageResponseBodyBean<User> getUserPageList(@Valid @NotNull GetUserPageListPayload payload) {
log.info("{}", payload);
val page = new Page<User>(payload.getCurrentPage(), payload.getPageSize());
val queryWrapper = Wrappers.lambdaQuery(User.class);
if (StrUtil.isNotBlank(payload.getUsername())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ public class PaginationBase {
*/
@JsonIgnore
@NotNull(message = "The current page is required!")
@Min(value = 1, message = "The current page is not less then 1!")
@Min(value = 1L, message = "The current page is not less then 1!")
private Integer currentPage = 1;
/**
* The page size. Default: 10
*/
@JsonIgnore
@NotNull(message = "The page size is required!")
@Range(min = 10, max = 100, message = "The rage of page size: 10 <= page size <= 100!")
@Range(min = 10L, max = 100L, message = "The rage of page size: 10 <= page size <= 100!")
private Integer pageSize = 10;
/**
* The order-by. (for table's field)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@
AsyncConfiguration.class,
RabbitmqConfiguration.class,
MinioConfiguration.class,
LocalDateTimeSerializerConfiguration.class
JacksonConfiguration.class,
TypeConversionConfiguration.class
})
public class MafAutoConfiguration {
@PostConstruct
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package com.jmsoftware.maf.springcloudstarter.configuration;

import cn.hutool.core.util.StrUtil;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import lombok.RequiredArgsConstructor;
import lombok.val;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.boot.autoconfigure.jackson.JacksonProperties;
import org.springframework.context.annotation.Bean;
Expand All @@ -16,7 +19,7 @@
* @author Johnny Miller (锺俊), email: johnnysviva@outlook.com, date: 6/27/2021 3:57 PM
**/
@RequiredArgsConstructor
public class LocalDateTimeSerializerConfiguration {
public class JacksonConfiguration {
public static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
private final JacksonProperties jacksonProperties;

Expand All @@ -31,6 +34,14 @@ public LocalDateTimeSerializer localDateTimeSerializer() {

@Bean
public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer(LocalDateTimeSerializer localDateTimeSerializer) {
return builder -> builder.serializerByType(LocalDateTime.class, localDateTimeSerializer);
var pattern = DEFAULT_DATE_FORMAT;
if (StrUtil.isNotBlank(jacksonProperties.getDateFormat())) {
pattern = jacksonProperties.getDateFormat();
}
val localDateTimeDeserializer = new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern(pattern));
return jackson2ObjectMapperBuilder -> jackson2ObjectMapperBuilder
.serializerByType(LocalDateTime.class, localDateTimeSerializer)
.deserializerByType(LocalDateTime.class, localDateTimeDeserializer)
.serializerByType(Long.class, ToStringSerializer.instance);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.jmsoftware.maf.springcloudstarter.configuration;

import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.StrUtil;
import com.jmsoftware.maf.springcloudstarter.constant.UniversalDateTime;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.context.annotation.Bean;
import org.springframework.core.convert.converter.Converter;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

/**
* Description: TypeConvesionConfiguration, change description here.
*
* @author Johnny Miller (锺俊), email: johnnysviva@outlook.com, date: 6/27/2021 9:36 PM
**/
@Slf4j
public class TypeConversionConfiguration {
@Bean
public StringToLocalDateConverter localDateConverter() {
return source -> {
if (StrUtil.isBlank(source)) {
return null;
}
return LocalDateTimeUtil.parseDate(source, UniversalDateTime.DATE_FORMAT);
};
}

@Bean
public StringToLocalTimeConverter localTimeConverter() {
return source -> {
if (StrUtil.isBlank(source)) {
return null;
}
return LocalTime.parse(source, DateTimeFormatter.ofPattern(UniversalDateTime.TIME_FORMAT));
};
}

@Bean
@ConditionalOnBean(name = "requestMappingHandlerAdapter")
public StringToLocalDateTimeConverter localDateTimeConverter() {
return source -> {
if (StrUtil.isBlank(source)) {
return null;
}
return LocalDateTimeUtil.parse(source, UniversalDateTime.DATE_TIME_FORMAT);
};
}

interface StringToLocalDateConverter extends Converter<String, LocalDate> {
}

interface StringToLocalTimeConverter extends Converter<String, LocalTime> {
}

interface StringToLocalDateTimeConverter extends Converter<String, LocalDateTime> {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.jmsoftware.maf.springcloudstarter.constant;

/**
* Description: UniversalDateTime, change description here.
*
* @author Johnny Miller (锺俊), email: johnnysviva@outlook.com, date: 6/27/2021 9:42 PM
**/
public interface UniversalDateTime {
String DATE_FORMAT = "yyyy-MM-dd";
String TIME_FORMAT = "HH:mm:ss";
String DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.jmsoftware.maf.springcloudstarter.controller;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
* Description: ControllerBase, change description here.
*
* @author Johnny Miller (锺俊), email: johnnysviva@outlook.com, date: 6/27/2021 9:20 PM
**/
public class ControllerBase {
// @InitBinder
// public void initBinder(WebDataBinder binder) {
// final SimpleDateFormat dateFormat = new SimpleDateFormat(DateTimeUniversalFormat.DATE_TIME_PATTERN);
// dateFormat.setLenient(false);
// binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
// }
}

0 comments on commit 778e152

Please sign in to comment.