Skip to content

rso-vaje-6315/lib-rest-utils

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

26 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Rest Utils library 1.3.0

status

Library content

Validator

Library provides validating service.

Usage

@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.

Additional HTTP constants

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;

Exceptions

Rest exception

Generic base exception. All other exceptions inherit from it.

ServiceCall exception

Thrown when call to other service fails. Returns code 503.

Validation exception

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();
    }
}

Bad Request exception

Error when receiveing bad data from user. Returns 400.

Forbidden & Unauthorized exception

Return 401 or 403 if user lacks proper permissions.

Not Found exception

Thrown when requested entity does not exists and returns 404. It also provides overloaded constructor to easily describe to user what is missing.

Development

Deployment

To deploy snapshot version, simply execute:

mvn clean deploy -P rso

About

Library for REST utils

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages