Library provides validating service.
@Inject
private Validator validator;
// Asserts object is not null
validator.assertNotNull(request.field);
// Asserts string is not null or empty (spaces are trimmed)
validator.assertNotBlank(request.field);
// Asserts string matches regex pattern
validator.assertRegex(request.field, regexp);
All of these methods throw ValidationException if assertion fails. More in exceptions
There are also available overloaded methods to provide additional information to user.
Library introduces new statuses:
- 422 (validation failed)
int status = HttpStatus.VALIDATION_FAILED;
- 424 (validation failed)
int status = HttpStatus.FAILED_DEPENDENCY;
It also provides header fields:
- X-Total-Count
String header = HttpHeaders.X_TOTAL_COUNT;
- Content-Disposition
String header = HttpHeaders.CONTENT_DISPOSITION;
- Authorization
String header = HttpHeaders.AUTHORIZATION;
- X-Service-Name
String header = HttpHeaders.X_SERVICE_NAME;
- X-Service-Version
String header = HttpHeaders.X_SERVICE_VERSION;
- X-Powered-By
String header = HttpHeaders.X_POWERED_BY;
Generic base exception. All other exceptions inherit from it.
Thrown when call to other service fails. Returns code 503.
Validation exception uses self-enclosing data (ExceptionResponse
), so there is no need to map its data. To provide better message to users, simply provide exception mapper:
import si.rso.rest.exceptions.ValidationException;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;
@Provider
public class ValidationExceptionMapper implements ExceptionMapper<ValidationException> {
@Override
public Response toResponse(ValidationException e) {
return Response.status(e.getResponse().getStatus()).entity(e.getResponse()).build();
}
}
Error when receiveing bad data from user. Returns 400.
Return 401 or 403 if user lacks proper permissions.
Thrown when requested entity does not exists and returns 404. It also provides overloaded constructor to easily describe to user what is missing.
To deploy snapshot version, simply execute:
mvn clean deploy -P rso