Last active
August 29, 2015 14:06
-
-
Save rosswarren/c92a7b6efe77a0e87556 to your computer and use it in GitHub Desktop.
ParamBasedParameterNameProvider for use in Jersey to get nice output when you use Bean Validations
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class JerseyConfig extends ResourceConfig { | |
public JerseyConfig() { | |
register(ValidationConfigurationContextResolver.class); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.glassfish.jersey.server.validation.ValidationConfig; | |
import org.glassfish.jersey.server.validation.internal.InjectingConstraintValidatorFactory; | |
import javax.validation.ParameterNameProvider; | |
import javax.validation.Validation; | |
import javax.ws.rs.HeaderParam; | |
import javax.ws.rs.PathParam; | |
import javax.ws.rs.QueryParam; | |
import javax.ws.rs.core.Context; | |
import javax.ws.rs.ext.ContextResolver; | |
import java.lang.annotation.Annotation; | |
import java.lang.reflect.Constructor; | |
import java.lang.reflect.Method; | |
import java.util.List; | |
public class ValidationConfigurationContextResolver implements ContextResolver<ValidationConfig> { | |
@Context | |
private javax.ws.rs.container.ResourceContext resourceContext; | |
@Override | |
public ValidationConfig getContext(final Class<?> type) { | |
return new ValidationConfig() | |
.constraintValidatorFactory(resourceContext.getResource(InjectingConstraintValidatorFactory.class)) | |
.parameterNameProvider(new ParamBasedParameterNameProvider()); | |
} | |
private class ParamBasedParameterNameProvider implements ParameterNameProvider { | |
private final ParameterNameProvider nameProvider; | |
public ParamBasedParameterNameProvider() { | |
nameProvider = Validation.byDefaultProvider().configure().getDefaultParameterNameProvider(); | |
} | |
@Override | |
public List<String> getParameterNames(final Constructor<?> constructor) { | |
return nameProvider.getParameterNames(constructor); | |
} | |
@Override | |
public List<String> getParameterNames(final Method method) { | |
List<String> names = nameProvider.getParameterNames(method); | |
Annotation[][] parameterAnnotations = method.getParameterAnnotations(); | |
for (int i = 0; i < parameterAnnotations.length; i++) { | |
Annotation[] parameterAnnotation = parameterAnnotations[i]; | |
for (Annotation annotation : parameterAnnotation) { | |
if (annotation.annotationType() == HeaderParam.class) { | |
names.set(i, ((HeaderParam) annotation).value() + " header"); | |
} | |
if (annotation.annotationType() == PathParam.class) { | |
names.set(i, ((PathParam) annotation).value() + " path parameter"); | |
} | |
if (annotation.annotationType() == QueryParam.class) { | |
names.set(i, ((PathParam) annotation).value() + " query parameter"); | |
} | |
} | |
} | |
return names; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment