Skip to content

Commit

Permalink
version: upgrade to 2.1.3;
Browse files Browse the repository at this point in the history
web: support BusinessException of super valid exception;
web: fix the bug when the placeholder value is not exist in parameters throw NullPointer Exception
  • Loading branch information
zhouxx committed Feb 12, 2024
1 parent 94bef1f commit 9d2c8f2
Show file tree
Hide file tree
Showing 16 changed files with 176 additions and 104 deletions.
2 changes: 1 addition & 1 deletion boot-plus-cache/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>boot-plus</artifactId>
<groupId>com.alilitech</groupId>
<version>2.1.2</version>
<version>2.1.3</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion boot-plus-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>boot-plus</artifactId>
<groupId>com.alilitech</groupId>
<version>2.1.2</version>
<version>2.1.3</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion boot-plus-dependencies/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>boot-plus</artifactId>
<groupId>com.alilitech</groupId>
<version>2.1.2</version>
<version>2.1.3</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion boot-plus-log/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>boot-plus</artifactId>
<groupId>com.alilitech</groupId>
<version>2.1.2</version>
<version>2.1.3</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion boot-plus-routing-datasource/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>boot-plus</artifactId>
<groupId>com.alilitech</groupId>
<version>2.1.2</version>
<version>2.1.3</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion boot-plus-security/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>boot-plus</artifactId>
<groupId>com.alilitech</groupId>
<version>2.1.2</version>
<version>2.1.3</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion boot-plus-swagger/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>boot-plus</artifactId>
<groupId>com.alilitech</groupId>
<version>2.1.2</version>
<version>2.1.3</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion boot-plus-web/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>boot-plus</artifactId>
<groupId>com.alilitech</groupId>
<version>2.1.2</version>
<version>2.1.3</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,18 @@
import com.alilitech.web.jackson.ser.CompositeSerializerModifier;
import com.alilitech.web.jackson.ser.dict.DictWithLocaleCacheManager;
import com.alilitech.web.jackson.ser.dict.DictWithoutLocaleCacheManager;
import com.alilitech.web.valid.ValidAdvice;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;

import javax.validation.ValidatorFactory;
import java.util.List;


Expand Down Expand Up @@ -78,4 +82,10 @@ public void addCorsMappings(CorsRegistry registry) {
public void extendHandlerExceptionResolvers(List<HandlerExceptionResolver> resolvers) {
resolvers.add(defaultExceptionResolver);
}

@Bean
@ConditionalOnMissingBean(ResponseEntityExceptionHandler.class)
public ValidAdvice validAdvice(ValidatorFactory validatorFactory) {
return new ValidAdvice(validatorFactory);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* Copyright 2017-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alilitech.web.valid;

import org.springframework.http.HttpStatus;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
*
* @author Zhou Xiaoxiang
* @since 2.1.3
*/
public class BusinessException extends RuntimeException {

private final HttpStatus httpStatus;

/**
* 除validatedValue外的其它占位符
*/
private final Map<String, Object> placeholderMap = new ConcurrentHashMap<>();

/**
* 每个message国际化会自带一个validatedValue占位符
*/
private Object validatedValue = -1;

/**
* 字段路径,用于给接口或前端展示
*/
private String propertyPath;

public BusinessException(HttpStatus httpStatus, String message) {
super(message);
this.httpStatus = httpStatus;
}

public BusinessException addPlaceholder(String key, Object value) {
placeholderMap.put(key, value);
return this;
}

public BusinessException validatedValue(Object validatedValue) {
this.validatedValue = validatedValue;
placeholderMap.put("validatedValue",validatedValue);
return this;
}

public BusinessException propertyPath(String propertyPath) {
this.propertyPath = propertyPath;
return this;
}

public HttpStatus getHttpStatus() {
return httpStatus;
}

public Map<String, Object> getPlaceholderMap() {
return placeholderMap;
}

public Object getValidatedValue() {
return validatedValue;
}

public String getPropertyPath() {
return propertyPath;
}

}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright 2017-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alilitech.web.valid;

import org.springframework.http.HttpStatus;

/**
* @author Zhou Xiaoxiang
* @since 1.0
*/
public class NoFoundException extends BusinessException {

public NoFoundException(String message) {
super(HttpStatus.NOT_FOUND, message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@
import com.alilitech.web.CommonBody;
import org.hibernate.validator.internal.engine.MessageInterpolatorContext;
import org.hibernate.validator.internal.engine.path.PathImpl;
import org.hibernate.validator.internal.metadata.core.ConstraintHelper;
import org.hibernate.validator.internal.metadata.descriptor.ConstraintDescriptorImpl;
import org.hibernate.validator.internal.metadata.location.ConstraintLocation;
import org.hibernate.validator.internal.util.annotation.AnnotationDescriptor;
import org.hibernate.validator.internal.util.annotation.ConstraintAnnotationDescriptor;
import org.hibernate.validator.messageinterpolation.ExpressionLanguageFeatureLevel;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.http.HttpHeaders;
Expand All @@ -39,6 +44,7 @@

import javax.validation.MessageInterpolator;
import javax.validation.ValidatorFactory;
import javax.validation.constraints.AssertTrue;
import java.util.Collections;
import java.util.stream.Collectors;

Expand All @@ -47,7 +53,6 @@
* @since 1.0
*/
@ControllerAdvice
@ConditionalOnMissingBean(ResponseEntityExceptionHandler.class)
public class ValidAdvice extends ResponseEntityExceptionHandler {

protected final ValidatorFactory validatorFactory;
Expand Down Expand Up @@ -93,22 +98,27 @@ protected ResponseEntity<Object> handleNoHandlerFoundException(NoHandlerFoundExc
.body(new CommonBody(HttpStatus.NOT_FOUND.value(), e.getMessage()));
}

@ExceptionHandler({ ValidException.class })
@ExceptionHandler({ BusinessException.class })
@ResponseBody
public ResponseEntity<Object> handleValidException(ValidException e, WebRequest request) {
public ResponseEntity<Object> handleValidException(BusinessException e, WebRequest request) {
return handleNotValid(e, request);
}

protected ResponseEntity<Object> handleNotValid(ValidException e, WebRequest request) {
protected ResponseEntity<Object> handleNotValid(BusinessException e, WebRequest request) {
MessageInterpolator messageInterpolator = validatorFactory.getMessageInterpolator();
MessageInterpolatorContext context = new MessageInterpolatorContext(null,

AnnotationDescriptor<AssertTrue> annotationDescriptor = new AnnotationDescriptor.Builder<>(VirtualEntity.class.getAnnotation(AssertTrue.class)).build();
ConstraintDescriptorImpl<AssertTrue> descriptor = new ConstraintDescriptorImpl<>(ConstraintHelper.forAllBuiltinConstraints(), null, new ConstraintAnnotationDescriptor<>(annotationDescriptor), ConstraintLocation.ConstraintLocationKind.TYPE);

MessageInterpolatorContext context = new MessageInterpolatorContext(
descriptor,
e.getValidatedValue(),
Object.class,
StringUtils.hasLength(e.getPropertyPath()) ? PathImpl.createPathFromString(e.getPropertyPath()) : null,
e.getPlaceholderMap(),
Collections.emptyMap(),
ExpressionLanguageFeatureLevel.DEFAULT,
false);
true);
String message = messageInterpolator.interpolate(e.getMessage(), context);
CommonBody body = StringUtils.hasLength(e.getPropertyPath()) ? new CommonBody(e.getHttpStatus().value(), Collections.singletonList(new ValidMessage(e.getPropertyPath(), message))) : new CommonBody(e.getHttpStatus().value(), message);
return ResponseEntity.status(e.getHttpStatus()).body(body);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,75 +17,15 @@

import org.springframework.http.HttpStatus;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
* ValidException like hibernate validation
*
* @author Zhou Xiaoxiang
* @since 1.0
*/
public class ValidException extends RuntimeException {

private HttpStatus httpStatus = HttpStatus.BAD_REQUEST;

/**
* 除validatedValue外的其它占位符
*/
private final Map<String, Object> placeholderMap = new ConcurrentHashMap<>();

/**
* 每个message国际化会自带一个validatedValue占位符
*/
private Object validatedValue = -1;

/**
* 字段路径,用于给接口或前端展示
*/
private String propertyPath;

public ValidException() {
}
public class ValidException extends BusinessException {

public ValidException(String message) {
super(message);
}

public ValidException addPlaceholder(String key, Object value) {
placeholderMap.put(key, value);
return this;
}

public ValidException httpStatus(HttpStatus httpStatus) {
this.httpStatus = httpStatus;
return this;
}

public ValidException validatedValue(Object validatedValue) {
this.validatedValue = validatedValue;
placeholderMap.put("validatedValue",validatedValue);
return this;
}

public ValidException propertyPath(String propertyPath) {
this.propertyPath = propertyPath;
return this;
}

public HttpStatus getHttpStatus() {
return httpStatus;
}

public Map<String, Object> getPlaceholderMap() {
return placeholderMap;
}

public Object getValidatedValue() {
return validatedValue;
}

public String getPropertyPath() {
return propertyPath;
super(HttpStatus.BAD_REQUEST, message);
}
}
Loading

0 comments on commit 9d2c8f2

Please sign in to comment.