0

I'm using hibernate-validator with a JAX-RS service to validate query parameters using @NotNull:

@GET
public Response doSomething(@NotNull @QueryParam("myParam") String myParam) {...}

This works as expected and throws a ConstraintViolationException if myParam is null. I'd like to extract the param name which is associated to the violation (e.g. myParam), and return that in the response message to the client but there does not appear to be an obvious way of extracting this from the exception. Can someone provide some insight?

dur
  • 15,689
  • 25
  • 79
  • 125
user1491636
  • 2,355
  • 11
  • 44
  • 71

2 Answers2

1

As of BeanValidation 1.1 there is a ParameterNameProvider contract which makes parameter name extraction configurable. As mentioned in the other answer, with Java 8 you can get the parameter names in the byte code provided you compile with the -parameters flag. Use the ReflectionParameterNameProvider in this case. However, even with Java 7 you can get parameter names, for example by using the ParanamerParameterNameProvider. This parameter name provider is based on Paranamer and there are several ways to set it up.

Hardy
  • 18,659
  • 3
  • 49
  • 65
0

This only works if you're using Java 8, as prior to Java 8 the actual parameter name was lost at compile time. Its now retained, assuming you compile and run at Java 8. See also http://docs.jboss.org/hibernate/validator/5.2/reference/en-US/html_single/#_java_8_support

John Ament
  • 11,595
  • 1
  • 36
  • 45