-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
106 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
.../src/main/java/site/zido/coffee/autoconfigure/web/JavaxExceptionEnablerConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package site.zido.coffee.autoconfigure.web; | ||
|
||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
/** | ||
* @author zido | ||
*/ | ||
@Configuration | ||
@ConditionalOnClass(name = "javax.validation.ConstraintViolationException") | ||
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET) | ||
public class JavaxExceptionEnablerConfiguration { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
coffee-modules/coffee-core/src/main/java/site/zido/coffee/core/constants/Patterns.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package site.zido.coffee.core.constants; | ||
|
||
public class Patterns { | ||
public static final String PHONE_PATTERN = "^1[123456789][\\d]{9}$"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
...les/coffee-webmvc/src/main/java/site/zido/coffee/mvc/exceptions/EnableJavaxException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package site.zido.coffee.mvc.exceptions; | ||
|
||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.context.annotation.Import; | ||
import site.zido.coffee.mvc.rest.JavaxValidationExceptionAdvice; | ||
|
||
import java.lang.annotation.*; | ||
|
||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target({ElementType.TYPE}) | ||
@Documented | ||
@Import({JavaxValidationExceptionAdvice.class}) | ||
@Configuration | ||
public @interface EnableJavaxException { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
...coffee-webmvc/src/main/java/site/zido/coffee/mvc/rest/JavaxValidationExceptionAdvice.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package site.zido.coffee.mvc.rest; | ||
|
||
import org.springframework.core.annotation.Order; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import org.springframework.web.bind.annotation.RestControllerAdvice; | ||
import org.springframework.web.context.request.WebRequest; | ||
import site.zido.coffee.mvc.CommonErrorCode; | ||
|
||
import javax.validation.ConstraintViolation; | ||
import javax.validation.ConstraintViolationException; | ||
import javax.validation.Path; | ||
import java.util.ArrayList; | ||
import java.util.Iterator; | ||
import java.util.List; | ||
import java.util.Set; | ||
|
||
@RestControllerAdvice | ||
@Order | ||
public class JavaxValidationExceptionAdvice extends BaseGlobalExceptionHandler { | ||
public JavaxValidationExceptionAdvice(HttpResponseBodyFactory factory) { | ||
super(factory); | ||
} | ||
|
||
/** | ||
* parameter参数校验异常处理 | ||
* | ||
* @param e 校验异常 | ||
* @return result | ||
*/ | ||
@ExceptionHandler(value = ConstraintViolationException.class) | ||
public ResponseEntity<Object> handleConstraintViolationException(ConstraintViolationException e, WebRequest request) { | ||
Set<ConstraintViolation<?>> constraintViolations = e.getConstraintViolations(); | ||
Iterator<ConstraintViolation<?>> iterator = constraintViolations.iterator(); | ||
List<String> errors = new ArrayList<>(); | ||
while (iterator.hasNext()) { | ||
ConstraintViolation<?> next = iterator.next(); | ||
Path propertyPath = next.getPropertyPath(); | ||
String name = "unknown"; | ||
//获取参数名 | ||
for (Path.Node node : propertyPath) { | ||
name = node.getName(); | ||
} | ||
//参数错误提示 | ||
String message = "[" + name + "] " + next.getMessage(); | ||
errors.add(message); | ||
} | ||
return handleExceptionInternal( | ||
e, | ||
factory.error(CommonErrorCode.VALIDATION_FAILED, | ||
messages.getMessage("ValidationFailed", "Validation Failed"), | ||
errors), | ||
new HttpHeaders(), | ||
HttpStatus.BAD_REQUEST, | ||
request | ||
); | ||
} | ||
} |