mirror of
https://github.com/spring-projects/spring-framework.git
synced 2026-09-17 08:24:13 +00:00
Add Validation section examples
Signed-off-by: Tran Ngoc Nhan <ngocnhan.tran1996@gmail.com>
This commit is contained in:
committed by
Brian Clozel
parent
acbae80205
commit
c21ea9e249
@@ -65,64 +65,4 @@ The exception contains a list of ``ParameterValidationResult``s that group valid
|
||||
by method parameter. You can either iterate over those, or provide a visitor with callback
|
||||
methods by controller method parameter type:
|
||||
|
||||
[tabs]
|
||||
======
|
||||
Java::
|
||||
+
|
||||
[source,java,indent=0,subs="verbatim,quotes"]
|
||||
----
|
||||
HandlerMethodValidationException ex = ... ;
|
||||
|
||||
ex.visitResults(new HandlerMethodValidationException.Visitor() {
|
||||
|
||||
@Override
|
||||
public void requestHeader(RequestHeader requestHeader, ParameterValidationResult result) {
|
||||
// ...
|
||||
}
|
||||
|
||||
@Override
|
||||
public void requestParam(@Nullable RequestParam requestParam, ParameterValidationResult result) {
|
||||
// ...
|
||||
}
|
||||
|
||||
@Override
|
||||
public void modelAttribute(@Nullable ModelAttribute modelAttribute, ParameterErrors errors) {
|
||||
|
||||
// ...
|
||||
|
||||
@Override
|
||||
public void other(ParameterValidationResult result) {
|
||||
// ...
|
||||
}
|
||||
});
|
||||
----
|
||||
|
||||
Kotlin::
|
||||
+
|
||||
[source,kotlin,indent=0,subs="verbatim,quotes"]
|
||||
----
|
||||
// HandlerMethodValidationException
|
||||
val ex
|
||||
|
||||
ex.visitResults(object : HandlerMethodValidationException.Visitor {
|
||||
|
||||
override fun requestHeader(requestHeader: RequestHeader, result: ParameterValidationResult) {
|
||||
// ...
|
||||
}
|
||||
|
||||
override fun requestParam(requestParam: RequestParam?, result: ParameterValidationResult) {
|
||||
// ...
|
||||
}
|
||||
|
||||
override fun modelAttribute(modelAttribute: ModelAttribute?, errors: ParameterErrors) {
|
||||
// ...
|
||||
}
|
||||
|
||||
// ...
|
||||
|
||||
override fun other(result: ParameterValidationResult) {
|
||||
// ...
|
||||
}
|
||||
})
|
||||
----
|
||||
======
|
||||
include-code::./HandlerMethodValidationExceptionVisitor[tag=snippet,indent=0]
|
||||
@@ -65,64 +65,4 @@ The exception contains a list of ``ParameterValidationResult``s that group valid
|
||||
by method parameter. You can either iterate over those, or provide a visitor with callback
|
||||
methods by controller method parameter type:
|
||||
|
||||
[tabs]
|
||||
======
|
||||
Java::
|
||||
+
|
||||
[source,java,indent=0,subs="verbatim,quotes"]
|
||||
----
|
||||
HandlerMethodValidationException ex = ... ;
|
||||
|
||||
ex.visitResults(new HandlerMethodValidationException.Visitor() {
|
||||
|
||||
@Override
|
||||
public void requestHeader(RequestHeader requestHeader, ParameterValidationResult result) {
|
||||
// ...
|
||||
}
|
||||
|
||||
@Override
|
||||
public void requestParam(@Nullable RequestParam requestParam, ParameterValidationResult result) {
|
||||
// ...
|
||||
}
|
||||
|
||||
@Override
|
||||
public void modelAttribute(@Nullable ModelAttribute modelAttribute, ParameterErrors errors) {
|
||||
|
||||
// ...
|
||||
|
||||
@Override
|
||||
public void other(ParameterValidationResult result) {
|
||||
// ...
|
||||
}
|
||||
});
|
||||
----
|
||||
|
||||
Kotlin::
|
||||
+
|
||||
[source,kotlin,indent=0,subs="verbatim,quotes"]
|
||||
----
|
||||
// HandlerMethodValidationException
|
||||
val ex
|
||||
|
||||
ex.visitResults(object : HandlerMethodValidationException.Visitor {
|
||||
|
||||
override fun requestHeader(requestHeader: RequestHeader, result: ParameterValidationResult) {
|
||||
// ...
|
||||
}
|
||||
|
||||
override fun requestParam(requestParam: RequestParam?, result: ParameterValidationResult) {
|
||||
// ...
|
||||
}
|
||||
|
||||
override fun modelAttribute(modelAttribute: ModelAttribute?, errors: ParameterErrors) {
|
||||
// ...
|
||||
}
|
||||
|
||||
// ...
|
||||
|
||||
override fun other(result: ParameterValidationResult) {
|
||||
// ...
|
||||
}
|
||||
})
|
||||
----
|
||||
======
|
||||
include-code::./HandlerMethodValidationExceptionVisitor[tag=snippet,indent=0]
|
||||
+90
@@ -0,0 +1,90 @@
|
||||
/*
|
||||
* Copyright 2002-present 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
|
||||
*
|
||||
* https://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 org.springframework.docs.web.webflux.controller.mvcannvalidation;
|
||||
|
||||
import org.jspecify.annotations.Nullable;
|
||||
import org.springframework.validation.method.ParameterErrors;
|
||||
import org.springframework.validation.method.ParameterValidationResult;
|
||||
import org.springframework.web.bind.annotation.CookieValue;
|
||||
import org.springframework.web.bind.annotation.MatrixVariable;
|
||||
import org.springframework.web.bind.annotation.ModelAttribute;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestHeader;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RequestPart;
|
||||
import org.springframework.web.method.annotation.HandlerMethodValidationException;
|
||||
|
||||
public class HandlerMethodValidationExceptionVisitor {
|
||||
|
||||
static void main() {
|
||||
// tag::snippet[]
|
||||
HandlerMethodValidationException ex = /**/ new HandlerMethodValidationException(null);
|
||||
|
||||
ex.visitResults(new HandlerMethodValidationException.Visitor() {
|
||||
|
||||
@Override
|
||||
public void requestHeader(RequestHeader requestHeader, ParameterValidationResult result) {
|
||||
// ...
|
||||
}
|
||||
|
||||
@Override
|
||||
public void requestParam(@Nullable RequestParam requestParam, ParameterValidationResult result) {
|
||||
// ...
|
||||
}
|
||||
|
||||
@Override
|
||||
public void modelAttribute(@Nullable ModelAttribute modelAttribute, ParameterErrors errors) {
|
||||
// ...
|
||||
}
|
||||
|
||||
// @fold:on // ...
|
||||
@Override
|
||||
public void requestPart(RequestPart requestPart, ParameterErrors errors) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void cookieValue(CookieValue cookieValue, ParameterValidationResult result) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void matrixVariable(MatrixVariable matrixVariable, ParameterValidationResult result) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void pathVariable(PathVariable pathVariable, ParameterValidationResult result) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void requestBody(RequestBody requestBody, ParameterErrors errors) {
|
||||
|
||||
}
|
||||
// @fold:off
|
||||
|
||||
@Override
|
||||
public void other(ParameterValidationResult result) {
|
||||
// ...
|
||||
}
|
||||
});
|
||||
// end::snippet[]
|
||||
}
|
||||
|
||||
}
|
||||
+90
@@ -0,0 +1,90 @@
|
||||
/*
|
||||
* Copyright 2002-present 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
|
||||
*
|
||||
* https://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 org.springframework.docs.web.webmvc.mvccontroller.mvcannvalidation;
|
||||
|
||||
import org.jspecify.annotations.Nullable;
|
||||
import org.springframework.validation.method.ParameterErrors;
|
||||
import org.springframework.validation.method.ParameterValidationResult;
|
||||
import org.springframework.web.bind.annotation.CookieValue;
|
||||
import org.springframework.web.bind.annotation.MatrixVariable;
|
||||
import org.springframework.web.bind.annotation.ModelAttribute;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestHeader;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RequestPart;
|
||||
import org.springframework.web.method.annotation.HandlerMethodValidationException;
|
||||
|
||||
public class HandlerMethodValidationExceptionVisitor {
|
||||
|
||||
static void main() {
|
||||
// tag::snippet[]
|
||||
HandlerMethodValidationException ex = /**/ new HandlerMethodValidationException(null);
|
||||
|
||||
ex.visitResults(new HandlerMethodValidationException.Visitor() {
|
||||
|
||||
@Override
|
||||
public void requestHeader(RequestHeader requestHeader, ParameterValidationResult result) {
|
||||
// ...
|
||||
}
|
||||
|
||||
@Override
|
||||
public void requestParam(@Nullable RequestParam requestParam, ParameterValidationResult result) {
|
||||
// ...
|
||||
}
|
||||
|
||||
@Override
|
||||
public void modelAttribute(@Nullable ModelAttribute modelAttribute, ParameterErrors errors) {
|
||||
// ...
|
||||
}
|
||||
|
||||
// @fold:on // ...
|
||||
@Override
|
||||
public void requestPart(RequestPart requestPart, ParameterErrors errors) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void cookieValue(CookieValue cookieValue, ParameterValidationResult result) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void matrixVariable(MatrixVariable matrixVariable, ParameterValidationResult result) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void pathVariable(PathVariable pathVariable, ParameterValidationResult result) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void requestBody(RequestBody requestBody, ParameterErrors errors) {
|
||||
|
||||
}
|
||||
// @fold:off
|
||||
|
||||
@Override
|
||||
public void other(ParameterValidationResult result) {
|
||||
// ...
|
||||
}
|
||||
});
|
||||
// end::snippet[]
|
||||
}
|
||||
|
||||
}
|
||||
+96
@@ -0,0 +1,96 @@
|
||||
/*
|
||||
* Copyright 2002-present 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
|
||||
*
|
||||
* https://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 org.springframework.docs.web.webflux.controller.mvcannvalidation
|
||||
|
||||
import org.springframework.context.MessageSourceResolvable
|
||||
import org.springframework.validation.method.MethodValidationResult
|
||||
import org.springframework.validation.method.ParameterErrors
|
||||
import org.springframework.validation.method.ParameterValidationResult
|
||||
import org.springframework.web.bind.annotation.*
|
||||
import org.springframework.web.method.annotation.HandlerMethodValidationException
|
||||
import java.lang.reflect.Method
|
||||
|
||||
class HandlerMethodValidationExceptionVisitor {
|
||||
|
||||
fun main() {
|
||||
// tag::snippet[]
|
||||
val ex: HandlerMethodValidationException = /**/ HandlerMethodValidationException(EmptyMethodValidationResult())
|
||||
|
||||
ex.visitResults(object : HandlerMethodValidationException.Visitor {
|
||||
|
||||
override fun requestHeader(requestHeader: RequestHeader, result: ParameterValidationResult) {
|
||||
// ...
|
||||
}
|
||||
|
||||
override fun requestParam(requestParam: RequestParam?, result: ParameterValidationResult) {
|
||||
// ...
|
||||
}
|
||||
|
||||
override fun modelAttribute(modelAttribute: ModelAttribute?, errors: ParameterErrors) {
|
||||
// ...
|
||||
}
|
||||
|
||||
// @fold:on // ...
|
||||
override fun requestPart(requestPart: RequestPart, errors: ParameterErrors) {
|
||||
}
|
||||
|
||||
override fun cookieValue(cookieValue: CookieValue, result: ParameterValidationResult) {
|
||||
}
|
||||
|
||||
override fun matrixVariable(matrixVariable: MatrixVariable, result: ParameterValidationResult) {
|
||||
}
|
||||
|
||||
override fun pathVariable(pathVariable: PathVariable, result: ParameterValidationResult) {
|
||||
}
|
||||
|
||||
override fun requestBody(requestBody: RequestBody, errors: ParameterErrors) {
|
||||
}
|
||||
// @fold:off
|
||||
|
||||
override fun other(result: ParameterValidationResult) {
|
||||
// ...
|
||||
}
|
||||
})
|
||||
// end::snippet[]
|
||||
}
|
||||
|
||||
internal class EmptyMethodValidationResult : MethodValidationResult {
|
||||
override fun getTarget(): Any {
|
||||
TODO()
|
||||
}
|
||||
|
||||
override fun getMethod(): Method {
|
||||
TODO()
|
||||
}
|
||||
|
||||
override fun isForReturnValue(): Boolean {
|
||||
TODO()
|
||||
}
|
||||
|
||||
override fun getParameterValidationResults(): List<ParameterValidationResult> {
|
||||
TODO()
|
||||
}
|
||||
|
||||
override fun getCrossParameterValidationResults(): List<MessageSourceResolvable> {
|
||||
TODO()
|
||||
}
|
||||
|
||||
override fun toString(): String {
|
||||
TODO()
|
||||
}
|
||||
}
|
||||
}
|
||||
+96
@@ -0,0 +1,96 @@
|
||||
/*
|
||||
* Copyright 2002-present 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
|
||||
*
|
||||
* https://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 org.springframework.docs.web.webmvc.mvccontroller.mvcannvalidation
|
||||
|
||||
import org.springframework.context.MessageSourceResolvable
|
||||
import org.springframework.validation.method.MethodValidationResult
|
||||
import org.springframework.validation.method.ParameterErrors
|
||||
import org.springframework.validation.method.ParameterValidationResult
|
||||
import org.springframework.web.bind.annotation.*
|
||||
import org.springframework.web.method.annotation.HandlerMethodValidationException
|
||||
import java.lang.reflect.Method
|
||||
|
||||
class HandlerMethodValidationExceptionVisitor {
|
||||
|
||||
fun main() {
|
||||
// tag::snippet[]
|
||||
val ex: HandlerMethodValidationException = /**/ HandlerMethodValidationException(EmptyMethodValidationResult())
|
||||
|
||||
ex.visitResults(object : HandlerMethodValidationException.Visitor {
|
||||
|
||||
override fun requestHeader(requestHeader: RequestHeader, result: ParameterValidationResult) {
|
||||
// ...
|
||||
}
|
||||
|
||||
override fun requestParam(requestParam: RequestParam?, result: ParameterValidationResult) {
|
||||
// ...
|
||||
}
|
||||
|
||||
override fun modelAttribute(modelAttribute: ModelAttribute?, errors: ParameterErrors) {
|
||||
// ...
|
||||
}
|
||||
|
||||
// @fold:on // ...
|
||||
override fun requestPart(requestPart: RequestPart, errors: ParameterErrors) {
|
||||
}
|
||||
|
||||
override fun cookieValue(cookieValue: CookieValue, result: ParameterValidationResult) {
|
||||
}
|
||||
|
||||
override fun matrixVariable(matrixVariable: MatrixVariable, result: ParameterValidationResult) {
|
||||
}
|
||||
|
||||
override fun pathVariable(pathVariable: PathVariable, result: ParameterValidationResult) {
|
||||
}
|
||||
|
||||
override fun requestBody(requestBody: RequestBody, errors: ParameterErrors) {
|
||||
}
|
||||
// @fold:off
|
||||
|
||||
override fun other(result: ParameterValidationResult) {
|
||||
// ...
|
||||
}
|
||||
})
|
||||
// end::snippet[]
|
||||
}
|
||||
|
||||
internal class EmptyMethodValidationResult : MethodValidationResult {
|
||||
override fun getTarget(): Any {
|
||||
TODO()
|
||||
}
|
||||
|
||||
override fun getMethod(): Method {
|
||||
TODO()
|
||||
}
|
||||
|
||||
override fun isForReturnValue(): Boolean {
|
||||
TODO()
|
||||
}
|
||||
|
||||
override fun getParameterValidationResults(): List<ParameterValidationResult> {
|
||||
TODO()
|
||||
}
|
||||
|
||||
override fun getCrossParameterValidationResults(): List<MessageSourceResolvable> {
|
||||
TODO()
|
||||
}
|
||||
|
||||
override fun toString(): String {
|
||||
TODO()
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user