I have a JAX-RS resource in Quarkus whose inputs I want to validate. One of the properties in this validation bean below, which will validate the input from a PUT method, contains a property which is expected to be a number.
public class UpdateBookDTO {
public Optional<@Length(min = 2, max = 100) String> title;
public Optional<@Length(min = 2, max = 100) String> author;
public Optional<@Positive @Digits(fraction = 0, integer = 10) String> pages;
@Override
public String toString() {
return "UpdateBookDTO{"
"title=" title
", author=" author
", pages=" pages
'}';
}
}
However, since the @Digits' annotation doesn't work with the Integer data type, I have to use a String. The issue with this of course is that it requires an extra step of parsing. Can I, and how, do this directly in the validation bean with some "magical" annotation, or is this not possible?
Thanks in advance!
CodePudding user response:
The solution I went for is descibed in the comments. I created a new package called "exceptions" containing global handler to catch the exceptions generated when trying to execute the operation I described the the question. The basic handler ended up looking like this:
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;
import javax.ws.rs.ProcessingException;
@Provider
public class CustomExceptionHandler implements ExceptionMapper<ProcessingException> {
@Override
public Response toResponse(ProcessingException e) {
e.printStackTrace();
return Response.status(400).entity("Unsupported data type").build();
}
}
The implementation that actually was built based on the code above is a bit more sophisticated - the logic also parses which field is faulty and also tells you if it's located within a nested class - but this is how it would be done following basic principle. It should work out of the box without any other config, as long as you have the @Provider decorator present.