Adding additional HTTP headers with JAX-RS and Bean Validation

A JAX-RS resource with input validation:

public class PingResource {

    public String ping() {
        return "Enjoy Jakarta EE 8!";

    public void save(@Size(min = 2, max = 3) String ping) {

and installed ExceptionMapper

import javax.validation.ConstraintViolationException;

public class ContraintViolationMapper implements ExceptionMapper<ConstraintViolationException> {

    public Response toResponse(ConstraintViolationException exception) {
        String messages = exception.getConstraintViolations().stream().
                map(v -> v.getMessage()).
        return Response.status(400).header("reason", messages).build();


...will return additional information in header on each contraint violation.

A failed input validation: curl -XPOST -i -H"Content-type: text/plain" -d'duke' http://localhost:8080/jaxrs-beanvalidation/resources/ping mainifests as:

HTTP/1.1 400 Bad Request
Server: Payara Server  5.184 #badassfish
reason: size must be between 2 and 3

The 7kB ThinWAR was built and deployed with in 3 seconds.

See you at Web, MicroProfile and Java EE Workshops at Munich Airport, Terminal 2 or Virtual Dedicated Workshops / consulting. Is Munich's airport too far? Learn from home:


Hi Adam,
there is also a similar header for warning messages. Called Warning:
The Warning general-header field is used to carry additional information about the status or transformation of a message which might not be reflected in the message. This information is typically used to warn about a possible lack of semantic transparency from caching operations or transformations applied to the entity body of the message.


Posted by Manuel Blechschmidt on May 03, 2019 at 08:14 AM CEST #

Post a Comment:
  • HTML Syntax: NOT allowed
...the last 150 posts
...the last 10 comments