3

I have a JAX-RS @POST endpoint whose input data has to be @Valid:

  @POST
  @Consumes(MediaType.APPLICATION_JSON)
  @Produces(MediaType.APPLICATION_JSON)
  public Response myEndpoint(@javax.validation.Valid MyInputData input) { /*...*/ }

With MyInputData class annotated with many constraints:

@lombok.Data
public class InputData {
  @Size(min = 1, max = 3)
  private String someString;

  /* ... */
}

Beyond that I have an ExceptionMapper<ConstraintViolationException> that transform the Exception into a Collection<String> (basically every single ConstraintViolation transformed to String using its getMesssage() method), then returns a Response.status(Status.BAD_REQUEST).entity(list).build().

Everything is working nicely. Fed an invalid input and I get back a HTTP 400 with a nice array of constraint violations in json format.

So far, so good...

BUT... the messages are in server's locale. Even if HTTP post sends a Accept-language header (and it is correctly detected when getting HttpServletRequest::getLocale).

By the time the ExceptionMapper gets hold of ConstraintViolation every message has already been interpolated, so no chance set the client locale.

Since the validation runs even before the JAX-RS resource (indeed, the JAX-RS resource isn't even called in case of invalid input), this locale aware message interpolator must be configured somewhere else.

Where? Is there already a MessageInterpolator implementation whose operation takes the HttpServletRequest locale into account?

rslemos
  • 2,454
  • 22
  • 32

0 Answers0