Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mwguy/spring-boot-starter-controller-validation
Spring boot starter that enables spring core validators on MVC/Rest controllers
https://github.com/mwguy/spring-boot-starter-controller-validation
Last synced: 2 days ago
JSON representation
Spring boot starter that enables spring core validators on MVC/Rest controllers
- Host: GitHub
- URL: https://github.com/mwguy/spring-boot-starter-controller-validation
- Owner: MWGuy
- License: wtfpl
- Created: 2023-08-01T21:31:11.000Z (over 1 year ago)
- Default Branch: master
- Last Pushed: 2023-08-01T22:08:10.000Z (over 1 year ago)
- Last Synced: 2023-08-01T23:26:07.490Z (over 1 year ago)
- Language: Kotlin
- Size: 61.5 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Spring controller validation ![Release](https://jitpack.io/v/MWGuy/spring-boot-starter-controller-validation.svg)
Spring boot starter that enables [bean validator](https://docs.spring.io/spring-framework/reference/core/validation/validator.html) in controller beans.
## Example
```kotlin
import org.springframework.validation.annotation.Validated
import com.mwguy.validation.EnableControllerValidation@RestController
class SimpleController {
@PostMapping
suspend fun doPostJob(@RequestBody @Validated payload: PostPayLoad): String {
return "Hello, ${payload.message}"
}class PostPayLoad(val message: String? = null)
}@Component
class SimpleValidator : Validator {
override fun supports(clazz: Class<*>): Boolean {
return clazz == SimpleController.PostPayLoad::class.java
}override fun validate(target: Any, errors: Errors) {
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "message", "message.empty")
}
}@SpringBootApplication
@EnableControllerValidation
class SpringCoreValidationApplicationfun main(args: Array) {
runApplication(*args)
}
```## Installing
First, add [jitpack](https://jitpack.io) maven repository:
```kotlin
repositories {
maven("https://jitpack.io")
}
```Second, add dependency:
```kotlin
dependencies {
implementation("com.github.MWGuy:spring-boot-starter-controller-validation:1.0")
}
```## Using library
First of all you need to enable controller validation with ``@EnableControllerValidation`` annotation.
Then you need to create controller advice which handles ``com.mwguy.validation.exception.ValidationFailedException``.
Example controller advice implementation:
```kotlin
import com.mwguy.validation.exception.ValidationFailedException@ControllerAdvice
class ValidationControllerAdvice {
@ExceptionHandler(ValidationFailedException::class)
fun handleValidationException(exception: ValidationFailedException): ResponseEntity> {
return ResponseEntity.badRequest().body(exception.errors.map {
ValidationErrorDetail(it.objectName, it.defaultMessage ?: it.code)
})
}data class ValidationErrorDetail(val path: String, val message: String?)
}
```