How to integrate jersey with guice and bean validation

I am using jersey + guice to develop a REST application. I would like to do an input check for query parameters, but what I'm looking for is a “ready to use” solution, not writing it myself.

I already found support for a bean check with a shirt, but unfortunately there is no idea how to integrate it with guice. Most of the checks I need to do is a simple @NotNull check. I need to check the value and return a detailed message if the validation fails.

Any ideas? Suggestions?

+4
source share
1 answer

I have worked.

, , apache bean. (gradle):

compile 'org.apache.bval:bval-guice:0.5'
compile 'javax.validation:validation-api:1.1.0.Final'

:

org.apache.bval.guice.ValidationModule

(groovy)


import com.google.inject.Singleton

import javax.validation.ConstraintViolationException
import javax.ws.rs.core.Response
import javax.ws.rs.ext.ExceptionMapper
import javax.ws.rs.ext.Provider

import static groovy.json.JsonOutput.toJson
import static javax.ws.rs.core.Response.Status.BAD_REQUEST
import static javax.ws.rs.core.Response.status

@Provider
@Singleton
class ValidationExceptionMapper implements ExceptionMapper {

    @Override
    Response toResponse(ConstraintViolationException e) {
        status(BAD_REQUEST).entity(toJson(e.constraintViolations*.message)).build()
    }
}

Mapper Exception guice bind(). , , :

org.apache.bval.guice.Validate

, :

javax.validation.constraints.NotNull

.

+5

All Articles