Spring Web Validator

Convenience methods for concise server-side http validation for projects using Spring Web

License

License

Categories

Categories

Ant Build Tools
GroupId

GroupId

com.github.instantpudd
ArtifactId

ArtifactId

spring-web-validator
Last Version

Last Version

0.0.1
Release Date

Release Date

Type

Type

jar
Description

Description

Spring Web Validator
Convenience methods for concise server-side http validation for projects using Spring Web
Project URL

Project URL

https://github.com/InstantPudd/spring-web-validator
Source Code Management

Source Code Management

https://github.com/InstantPudd/spring-web-validator

Download spring-web-validator

How to add to project

<!-- https://jarcasting.com/artifacts/com.github.instantpudd/spring-web-validator/ -->
<dependency>
    <groupId>com.github.instantpudd</groupId>
    <artifactId>spring-web-validator</artifactId>
    <version>0.0.1</version>
</dependency>
// https://jarcasting.com/artifacts/com.github.instantpudd/spring-web-validator/
implementation 'com.github.instantpudd:spring-web-validator:0.0.1'
// https://jarcasting.com/artifacts/com.github.instantpudd/spring-web-validator/
implementation ("com.github.instantpudd:spring-web-validator:0.0.1")
'com.github.instantpudd:spring-web-validator:jar:0.0.1'
<dependency org="com.github.instantpudd" name="spring-web-validator" rev="0.0.1">
  <artifact name="spring-web-validator" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.github.instantpudd', module='spring-web-validator', version='0.0.1')
)
libraryDependencies += "com.github.instantpudd" % "spring-web-validator" % "0.0.1"
[com.github.instantpudd/spring-web-validator "0.0.1"]

Dependencies

provided (1)

Group / Artifact Type Version
org.springframework : spring-web jar 4.3.10.RELEASE

test (1)

Group / Artifact Type Version
junit : junit jar 4.12

Project Modules

There are no modules declared in this project.

Spring Web Validator

Utility methods for concise server-side http validation for client errors using Spring Web

Adding to your project

You will need to have the spring-web dependency in your project https://mvnrepository.com/artifact/org.springframework/spring-web. Anything below version 3.2 should not work.

Add this dependency:

<dependency>
    <groupId>com.github.instantpudd</groupId>
    <artifactId>spring-web-validator</artifactId>
    <version>0.0.1</version>
</dependency>

Create a bean for ClientErrorExceptionHandler. This will allow it to work with Spring to map the exceptions this library throws to appropriate responses.

@Bean
public ClientErrorExceptionHandler clientErrorExceptionHandler() {
    return new ClientErrorExceptionHandler();
}

Validating

@RequestMapping(method=RequestMethod.POST)
public void someEndpoint(@RequestBody SomeObject someObject) {
    //Will result in a 400 status with the response body containing the message
    //"someProperty cannot be null" if someProperty is null.
    Validator
        .returnStatus(ClientErrorStatusCode.BAD_REQUEST)  //Synonym to ClientErrorStatusCode.STATUS_400. Every 4xx http status represented.
        .ifNull(someObject.getSomeProperty())             //Other options: always(), ifTrue(expression), ifFalse(expression), ifNotNull()
        .withErrorMessage("someProperty cannot be null")  //withNoErrorMessage() also possible
        .execute();                                       //Does the validation
   
   //...rest of the method
}

Versions

Version
0.0.1