mirror of
https://github.com/hs-web/hsweb-framework.git
synced 2026-06-02 19:02:44 +08:00
spring-boot 2.2.0.RELEASE
This commit is contained in:
@@ -7,6 +7,7 @@ import org.hswebframework.web.exception.BusinessException;
|
||||
import org.hswebframework.web.exception.NotFoundException;
|
||||
import org.hswebframework.web.exception.ValidationException;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
|
||||
import org.springframework.core.codec.DecodingException;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.validation.BindException;
|
||||
import org.springframework.validation.FieldError;
|
||||
@@ -18,6 +19,7 @@ import org.springframework.web.bind.support.WebExchangeBindException;
|
||||
import org.springframework.web.server.MediaTypeNotSupportedStatusException;
|
||||
import org.springframework.web.server.MethodNotAllowedException;
|
||||
import org.springframework.web.server.NotAcceptableStatusException;
|
||||
import org.springframework.web.server.ServerWebInputException;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import javax.validation.ConstraintViolationException;
|
||||
@@ -79,7 +81,7 @@ public class CommonErrorControllerAdvice {
|
||||
.stream()
|
||||
.filter(FieldError.class::isInstance)
|
||||
.map(FieldError.class::cast)
|
||||
.map(err -> new ValidationException.Detail(err.getField(), err.getDefaultMessage(),null))
|
||||
.map(err -> new ValidationException.Detail(err.getField(), err.getDefaultMessage(), null))
|
||||
.collect(Collectors.toList())));
|
||||
}
|
||||
|
||||
@@ -90,7 +92,7 @@ public class CommonErrorControllerAdvice {
|
||||
.stream()
|
||||
.filter(FieldError.class::isInstance)
|
||||
.map(FieldError.class::cast)
|
||||
.map(err -> new ValidationException.Detail(err.getField(), err.getDefaultMessage(),null))
|
||||
.map(err -> new ValidationException.Detail(err.getField(), err.getDefaultMessage(), null))
|
||||
.collect(Collectors.toList())));
|
||||
}
|
||||
|
||||
@@ -102,7 +104,7 @@ public class CommonErrorControllerAdvice {
|
||||
.stream()
|
||||
.filter(FieldError.class::isInstance)
|
||||
.map(FieldError.class::cast)
|
||||
.map(err -> new ValidationException.Detail(err.getField(), err.getDefaultMessage(),null))
|
||||
.map(err -> new ValidationException.Detail(err.getField(), err.getDefaultMessage(), null))
|
||||
.collect(Collectors.toList())));
|
||||
}
|
||||
|
||||
@@ -115,7 +117,7 @@ public class CommonErrorControllerAdvice {
|
||||
@ExceptionHandler
|
||||
@ResponseStatus(HttpStatus.GATEWAY_TIMEOUT)
|
||||
public Mono<ResponseMessage<?>> handleException(TimeoutException e) {
|
||||
log.error(e.getMessage(),e);
|
||||
log.error(e.getMessage(), e);
|
||||
return Mono.just(ResponseMessage.error(504, "timeout", e.getMessage()));
|
||||
}
|
||||
|
||||
@@ -137,7 +139,7 @@ public class CommonErrorControllerAdvice {
|
||||
@ResponseStatus(HttpStatus.BAD_REQUEST)
|
||||
public Mono<ResponseMessage<?>> handleException(IllegalArgumentException e) {
|
||||
log.error(e.getMessage(), e);
|
||||
return Mono.just(ResponseMessage.error(400,"illegal_argument", e.getMessage()));
|
||||
return Mono.just(ResponseMessage.error(400, "illegal_argument", e.getMessage()));
|
||||
}
|
||||
|
||||
@ExceptionHandler
|
||||
@@ -167,5 +169,19 @@ public class CommonErrorControllerAdvice {
|
||||
.result(e.getSupportedMethods()));
|
||||
}
|
||||
|
||||
@ExceptionHandler
|
||||
@ResponseStatus(HttpStatus.BAD_REQUEST)
|
||||
public Mono<ResponseMessage<?>> handleException(ServerWebInputException e) {
|
||||
Throwable exception=e;
|
||||
do {
|
||||
exception = exception.getCause();
|
||||
if (exception instanceof ValidationException) {
|
||||
return handleException(((ValidationException) exception));
|
||||
}
|
||||
|
||||
} while (exception != null && exception != e);
|
||||
|
||||
return Mono.just(ResponseMessage.error(400, "illegal_argument", e.getMessage()));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -20,6 +20,7 @@ import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
|
||||
import com.fasterxml.jackson.databind.deser.std.EnumDeserializer;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import com.fasterxml.jackson.databind.util.EnumResolver;
|
||||
import lombok.SneakyThrows;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.hswebframework.web.exception.ValidationException;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
@@ -345,6 +346,7 @@ public interface EnumDict<V> extends JSONSerializable {
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("all")
|
||||
@SneakyThrows
|
||||
public Object deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
|
||||
JsonNode node = jp.getCodec().readTree(jp);
|
||||
|
||||
@@ -356,22 +358,36 @@ public interface EnumDict<V> extends JSONSerializable {
|
||||
} else {
|
||||
findPropertyType = BeanUtils.findPropertyType(currentName, currentValue.getClass());
|
||||
}
|
||||
Supplier<ValidationException> exceptionSupplier = () -> {
|
||||
List<Object> values= Stream.of(findPropertyType.getEnumConstants())
|
||||
.map(Enum.class::cast)
|
||||
.map(e->{
|
||||
if(e instanceof EnumDict){
|
||||
return ((EnumDict) e).getValue();
|
||||
}
|
||||
return e.name();
|
||||
}).collect(Collectors.toList());
|
||||
|
||||
return new ValidationException("参数[" + currentName + "]在选项中不存在",
|
||||
Arrays.asList(
|
||||
new ValidationException.Detail(currentName, "选项中不存在此值", values)
|
||||
));
|
||||
};
|
||||
if (EnumDict.class.isAssignableFrom(findPropertyType) && findPropertyType.isEnum()) {
|
||||
if (node.isObject()) {
|
||||
return (EnumDict) EnumDict
|
||||
.findByValue(findPropertyType, node.get("value").textValue())
|
||||
.orElse(null);
|
||||
.orElseThrow(exceptionSupplier);
|
||||
}
|
||||
if (node.isNumber()) {
|
||||
return (EnumDict) EnumDict
|
||||
.find(findPropertyType, node.numberValue())
|
||||
.orElse(null);
|
||||
.orElseThrow(exceptionSupplier);
|
||||
}
|
||||
if (node.isTextual()) {
|
||||
return (EnumDict) EnumDict
|
||||
.find(findPropertyType, node.textValue())
|
||||
.orElse(null);
|
||||
.orElseThrow(exceptionSupplier);
|
||||
}
|
||||
throw new ValidationException("参数[" + currentName + "]在选项中不存在", Arrays.asList(
|
||||
new ValidationException.Detail(currentName, "选项中不存在此值", null)
|
||||
@@ -389,7 +405,7 @@ public interface EnumDict<V> extends JSONSerializable {
|
||||
return false;
|
||||
})
|
||||
.findAny()
|
||||
.orElse(null);
|
||||
.orElseThrow(exceptionSupplier);
|
||||
}
|
||||
|
||||
log.warn("unsupported deserialize enum json : {}", node);
|
||||
|
||||
@@ -29,8 +29,8 @@ public class CustomCodecsAutoConfiguration {
|
||||
CodecCustomizer jacksonDecoderCustomizer(EntityFactory entityFactory, ObjectMapper objectMapper) {
|
||||
objectMapper.setTypeFactory(new CustomTypeFactory(entityFactory));
|
||||
SimpleModule module = new SimpleModule();
|
||||
JsonDeserializer<EnumDict> deserialize = new EnumDict.EnumDictJSONDeserializer();
|
||||
module.addDeserializer(Enum.class, (JsonDeserializer) deserialize);
|
||||
JsonDeserializer deserializer = new EnumDict.EnumDictJSONDeserializer();
|
||||
module.addDeserializer(Enum.class, deserializer);
|
||||
objectMapper.registerModule(module);
|
||||
|
||||
|
||||
|
||||
2
pom.xml
2
pom.xml
@@ -79,7 +79,7 @@
|
||||
<java.version>1.8</java.version>
|
||||
<project.build.jdk>${java.version}</project.build.jdk>
|
||||
|
||||
<spring.boot.version>2.2.0.RC1</spring.boot.version>
|
||||
<spring.boot.version>2.2.0.RELEASE</spring.boot.version>
|
||||
|
||||
<javassist.version>3.20.0-GA</javassist.version>
|
||||
<activiti.version>5.19.0.2</activiti.version>
|
||||
|
||||
Reference in New Issue
Block a user