Compare commits
4 Commits
8828b12c78
...
8eac9054cd
Author | SHA1 | Date | |
---|---|---|---|
8eac9054cd | |||
a55c712349 | |||
0eda94a658 | |||
c816696c55 |
@@ -78,6 +78,7 @@ System.out.println(result); // Output: Return string
|
||||
public final class LoginException
|
||||
extends RuntimeException
|
||||
implements MultiTypesException<LoginException, LoginException.Type> {
|
||||
private static final long serialVersionUID = 881293090625085616L;
|
||||
private final Type type;
|
||||
private LoginException(@Nonnull Type type, @Nonnull String message) {
|
||||
super(message);
|
||||
|
@@ -35,6 +35,7 @@ import xyz.zhouxy.plusone.commons.base.IWithCode;
|
||||
* public final class LoginException
|
||||
* extends RuntimeException
|
||||
* implements MultiTypesException<LoginException, LoginException.Type> {
|
||||
* private static final long serialVersionUID = 881293090625085616L;
|
||||
* private final Type type;
|
||||
* private LoginException(@Nonnull Type type, @Nonnull String message) {
|
||||
* super(message);
|
||||
|
@@ -31,7 +31,7 @@ import xyz.zhouxy.plusone.commons.exception.MultiTypesException.ExceptionType;
|
||||
* 如果表示用户传参造成的解析失败,可使用 {@link RequestParamsException#RequestParamsException(Throwable)},
|
||||
* 将 ParsingFailureException 包装成 {@link RequestParamsException} 再抛出。
|
||||
* <pre>
|
||||
* throw new RequestParamsException(ParsingFailureException.of(ParsingFailureException.Type.NUMBER_PARSING_FAILURE));
|
||||
* throw new RequestParamsException(ParsingFailureException.Type.NUMBER_PARSING_FAILURE.create());
|
||||
* </pre>
|
||||
*
|
||||
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
|
||||
@@ -40,6 +40,7 @@ import xyz.zhouxy.plusone.commons.exception.MultiTypesException.ExceptionType;
|
||||
public final class ParsingFailureException
|
||||
extends RuntimeException
|
||||
implements MultiTypesException<ParsingFailureException, ParsingFailureException.Type> {
|
||||
private static final long serialVersionUID = 795996090625132616L;
|
||||
|
||||
private final Type type;
|
||||
|
||||
|
@@ -29,6 +29,7 @@ package xyz.zhouxy.plusone.commons.exception.business;
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public class BizException extends RuntimeException {
|
||||
private static final long serialVersionUID = 982585090625482416L;
|
||||
|
||||
private static final String DEFAULT_MSG = "业务异常";
|
||||
|
||||
|
@@ -36,6 +36,7 @@ import xyz.zhouxy.plusone.commons.exception.MultiTypesException;
|
||||
public final class InvalidInputException
|
||||
extends RequestParamsException
|
||||
implements MultiTypesException<InvalidInputException, InvalidInputException.Type> {
|
||||
private static final long serialVersionUID = -28994090625082516L;
|
||||
|
||||
private final Type type;
|
||||
|
||||
|
@@ -26,6 +26,7 @@ package xyz.zhouxy.plusone.commons.exception.business;
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public class RequestParamsException extends BizException {
|
||||
private static final long serialVersionUID = 448337090625192516L;
|
||||
|
||||
private static final String DEFAULT_MSG = "用户请求参数错误";
|
||||
|
||||
|
@@ -31,6 +31,7 @@
|
||||
* public final class LoginException
|
||||
* extends RuntimeException
|
||||
* implements MultiTypesException<LoginException, LoginException.Type> {
|
||||
* private static final long serialVersionUID = 881293090625085616L;
|
||||
* private final Type type;
|
||||
* private LoginException(@Nonnull Type type, @Nonnull String message) {
|
||||
* super(message);
|
||||
|
@@ -31,6 +31,7 @@ package xyz.zhouxy.plusone.commons.exception.system;
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public final class DataOperationResultException extends SysException {
|
||||
private static final long serialVersionUID = 992754090625352516L;
|
||||
|
||||
private final long expected;
|
||||
private final long actual;
|
||||
|
@@ -26,6 +26,7 @@ package xyz.zhouxy.plusone.commons.exception.system;
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public class SysException extends RuntimeException {
|
||||
private static final long serialVersionUID = -936435090625482516L;
|
||||
|
||||
private static final String DEFAULT_MSG = "系统异常";
|
||||
|
||||
|
@@ -21,6 +21,8 @@ import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.ZonedDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.time.temporal.TemporalAccessor;
|
||||
import java.time.temporal.TemporalQuery;
|
||||
|
||||
import com.google.gson.TypeAdapter;
|
||||
import com.google.gson.stream.JsonReader;
|
||||
@@ -42,16 +44,15 @@ public class JSR310TypeAdapters {
|
||||
* {@code LocalDate} 的 {@code TypeAdapter},
|
||||
* 用于 Gson 对 {@code LocalDate} 进行相互转换。
|
||||
*/
|
||||
public static final class LocalDateTypeAdapter extends TypeAdapter<LocalDate> {
|
||||
|
||||
private final DateTimeFormatter dateTimeFormatter;
|
||||
public static final class LocalDateTypeAdapter
|
||||
extends TemporalAccessorTypeAdapter<LocalDate, LocalDateTypeAdapter> {
|
||||
|
||||
/**
|
||||
* 默认构造函数,
|
||||
* 使用 {@link DateTimeFormatter#ISO_LOCAL_DATE} 进行 {@link LocalDate} 的序列化与反序列化。
|
||||
*/
|
||||
public LocalDateTypeAdapter() {
|
||||
this.dateTimeFormatter = DateTimeFormatter.ISO_LOCAL_DATE;
|
||||
this(DateTimeFormatter.ISO_LOCAL_DATE);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -61,20 +62,7 @@ public class JSR310TypeAdapters {
|
||||
* @param formatter 用于序列化 {@link LocalDate} 的格式化器,不可为 {@code null}。
|
||||
*/
|
||||
public LocalDateTypeAdapter(DateTimeFormatter formatter) {
|
||||
AssertTools.checkArgumentNotNull(formatter, "formatter can not be null.");
|
||||
this.dateTimeFormatter = formatter;
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public void write(JsonWriter out, LocalDate value) throws IOException {
|
||||
out.value(dateTimeFormatter.format(value));
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public LocalDate read(JsonReader in) throws IOException {
|
||||
return LocalDate.parse(in.nextString(), dateTimeFormatter);
|
||||
super(LocalDate::from, formatter);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -82,16 +70,15 @@ public class JSR310TypeAdapters {
|
||||
* {@code LocalDateTime} 的 {@code TypeAdapter},
|
||||
* 用于 Gson 对 {@code LocalDateTime} 进行相互转换。
|
||||
*/
|
||||
public static final class LocalDateTimeTypeAdapter extends TypeAdapter<LocalDateTime> {
|
||||
|
||||
private final DateTimeFormatter dateTimeFormatter;
|
||||
public static final class LocalDateTimeTypeAdapter
|
||||
extends TemporalAccessorTypeAdapter<LocalDateTime, LocalDateTimeTypeAdapter> {
|
||||
|
||||
/**
|
||||
* 默认构造函数,
|
||||
* 使用 {@link DateTimeFormatter#ISO_LOCAL_DATE_TIME} 进行 {@link LocalDateTime} 的序列化与反序列化。
|
||||
*/
|
||||
public LocalDateTimeTypeAdapter() {
|
||||
this.dateTimeFormatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
|
||||
this(DateTimeFormatter.ISO_LOCAL_DATE_TIME);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -101,20 +88,7 @@ public class JSR310TypeAdapters {
|
||||
* @param formatter 用于序列化 {@link LocalDateTime} 的格式化器,不可为 {@code null}。
|
||||
*/
|
||||
public LocalDateTimeTypeAdapter(DateTimeFormatter formatter) {
|
||||
AssertTools.checkArgumentNotNull(formatter, "formatter can not be null.");
|
||||
this.dateTimeFormatter = formatter;
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public void write(JsonWriter out, LocalDateTime value) throws IOException {
|
||||
out.value(dateTimeFormatter.format(value));
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public LocalDateTime read(JsonReader in) throws IOException {
|
||||
return LocalDateTime.parse(in.nextString(), dateTimeFormatter);
|
||||
super(LocalDateTime::from, formatter);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -122,16 +96,15 @@ public class JSR310TypeAdapters {
|
||||
* {@code ZonedDateTime} 的 {@code TypeAdapter},
|
||||
* 用于 Gson 对 {@code ZonedDateTime} 进行相互转换。
|
||||
*/
|
||||
public static final class ZonedDateTimeTypeAdapter extends TypeAdapter<ZonedDateTime> {
|
||||
|
||||
private final DateTimeFormatter dateTimeFormatter;
|
||||
public static final class ZonedDateTimeTypeAdapter
|
||||
extends TemporalAccessorTypeAdapter<ZonedDateTime, ZonedDateTimeTypeAdapter> {
|
||||
|
||||
/**
|
||||
* 默认构造函数,
|
||||
* 使用 {@link DateTimeFormatter#ISO_ZONED_DATE_TIME} 进行 {@link ZonedDateTime} 的序列化与反序列化。
|
||||
*/
|
||||
public ZonedDateTimeTypeAdapter() {
|
||||
this.dateTimeFormatter = DateTimeFormatter.ISO_ZONED_DATE_TIME;
|
||||
this(DateTimeFormatter.ISO_ZONED_DATE_TIME);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -141,20 +114,7 @@ public class JSR310TypeAdapters {
|
||||
* @param formatter 用于序列化 {@link ZonedDateTime} 的格式化器,不可为 {@code null}。
|
||||
*/
|
||||
public ZonedDateTimeTypeAdapter(DateTimeFormatter formatter) {
|
||||
AssertTools.checkArgumentNotNull(formatter, "formatter can not be null.");
|
||||
this.dateTimeFormatter = formatter;
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public void write(JsonWriter out, ZonedDateTime value) throws IOException {
|
||||
out.value(dateTimeFormatter.format(value));
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public ZonedDateTime read(JsonReader in) throws IOException {
|
||||
return ZonedDateTime.parse(in.nextString(), dateTimeFormatter);
|
||||
super(ZonedDateTime::from, formatter);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -166,18 +126,40 @@ public class JSR310TypeAdapters {
|
||||
* 使用 {@link DateTimeFormatter#ISO_INSTANT} 进行 {@link Instant} 的序列化与反序列化。
|
||||
*
|
||||
*/
|
||||
public static final class InstantTypeAdapter extends TypeAdapter<Instant> {
|
||||
public static final class InstantTypeAdapter
|
||||
extends TemporalAccessorTypeAdapter<Instant, InstantTypeAdapter> {
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public void write(JsonWriter out, Instant value) throws IOException {
|
||||
out.value(DateTimeFormatter.ISO_INSTANT.format(value));
|
||||
public InstantTypeAdapter() {
|
||||
super(Instant::from, DateTimeFormatter.ISO_INSTANT);
|
||||
}
|
||||
}
|
||||
|
||||
private abstract static class TemporalAccessorTypeAdapter<
|
||||
T extends TemporalAccessor,
|
||||
TTypeAdapter extends TemporalAccessorTypeAdapter<T, TTypeAdapter>>
|
||||
extends TypeAdapter<T> {
|
||||
|
||||
private final TemporalQuery<T> temporalQuery;
|
||||
|
||||
private final DateTimeFormatter dateTimeFormatter;
|
||||
|
||||
protected TemporalAccessorTypeAdapter(
|
||||
TemporalQuery<T> temporalQuery, DateTimeFormatter dateTimeFormatter) {
|
||||
AssertTools.checkArgumentNotNull(dateTimeFormatter, "formatter must not be null.");
|
||||
this.temporalQuery = temporalQuery;
|
||||
this.dateTimeFormatter = dateTimeFormatter;
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public Instant read(JsonReader in) throws IOException {
|
||||
return Instant.parse(in.nextString());
|
||||
public void write(JsonWriter out, T value) throws IOException {
|
||||
out.value(dateTimeFormatter.format(value));
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public T read(JsonReader in) throws IOException {
|
||||
return dateTimeFormatter.parse(in.nextString(), temporalQuery);
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -19,14 +19,12 @@ package xyz.zhouxy.plusone.commons.model.dto.test;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.Statement;
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@@ -45,9 +43,6 @@ import com.google.common.collect.ImmutableMap;
|
||||
import com.google.common.collect.Lists;
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.google.gson.TypeAdapter;
|
||||
import com.google.gson.stream.JsonReader;
|
||||
import com.google.gson.stream.JsonWriter;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
@@ -57,6 +52,9 @@ import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
import lombok.ToString;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import xyz.zhouxy.plusone.commons.gson.adapter.JSR310TypeAdapters.LocalDateTimeTypeAdapter;
|
||||
import xyz.zhouxy.plusone.commons.gson.adapter.JSR310TypeAdapters.LocalDateTypeAdapter;
|
||||
import xyz.zhouxy.plusone.commons.model.dto.PageResult;
|
||||
import xyz.zhouxy.plusone.commons.model.dto.PagingAndSortingQueryParams;
|
||||
import xyz.zhouxy.plusone.commons.model.dto.PagingParams;
|
||||
@@ -187,31 +185,8 @@ public class PagingAndSortingQueryParamsTests {
|
||||
@Test
|
||||
void testGson() {
|
||||
Gson gson = new GsonBuilder()
|
||||
.registerTypeAdapter(LocalDate.class, new TypeAdapter<LocalDate>() {
|
||||
|
||||
@Override
|
||||
public void write(JsonWriter out, LocalDate value) throws IOException {
|
||||
out.value(DateTimeFormatter.ISO_DATE.format(value));
|
||||
}
|
||||
|
||||
@Override
|
||||
public LocalDate read(JsonReader in) throws IOException {
|
||||
return LocalDate.parse(in.nextString(), DateTimeFormatter.ISO_DATE);
|
||||
}
|
||||
|
||||
})
|
||||
.registerTypeAdapter(LocalDateTime.class, new TypeAdapter<LocalDateTime>() {
|
||||
|
||||
@Override
|
||||
public void write(JsonWriter out, LocalDateTime value) throws IOException {
|
||||
out.value(DateTimeFormatter.ISO_LOCAL_DATE_TIME.format(value));
|
||||
}
|
||||
|
||||
@Override
|
||||
public LocalDateTime read(JsonReader in) throws IOException {
|
||||
return LocalDateTime.parse(in.nextString(), DateTimeFormatter.ISO_LOCAL_DATE_TIME);
|
||||
}
|
||||
})
|
||||
.registerTypeAdapter(LocalDate.class, new LocalDateTypeAdapter().nullSafe())
|
||||
.registerTypeAdapter(LocalDateTime.class, new LocalDateTimeTypeAdapter().nullSafe())
|
||||
.create();
|
||||
try (SqlSession session = sqlSessionFactory.openSession()) {
|
||||
AccountQueryParams params = gson.fromJson(JSON_STR, AccountQueryParams.class);
|
||||
|
Reference in New Issue
Block a user