Bean Validation in JAX-RS

I have not dug into the Bean Validation specification into detail before, but one of the entries posted in the Payara issue tracker made me explore how the JAX-RS specification integrates/leverages the Bean Validation features

Note: Bean Validation has been around since Java EE 6, but its integration with JAX-RS was materialized only in the latest (2.0) version (part of Java EE 7) 

In case you want to jump right into the code, you can fork it from GitHub

Quick overview

I don’t want to dive into too many details

  • JAX-RS supports declarative (as opposed to programmatic) validation using annotations from Bean Validation specification
  • It supports application of constraints to JAX-RS resource classes, method parameters (both request bodies as well as other components such as headers and URI parts like query parameters etc.), method return types and fields as well


The JAX-RS spec also defines default behaviour for scenarios when the bean  validation constraints are violated

Exception Handling

JAX-RS implementations are forced to provide (by the spec) a default ExceptionMapper for handling bean validation constraint violation exceptions. It returns standard responses (HTTP 400/500) to clients based on some (default) rules outlined in the specification (see section 7.6 of the JAX-RS spec doc)

Customizing default JAX-RS behaviour

The default JAX-RS behaviour can be overridden by providing your own (custom) Exception Mapper implementation

Further reading

  • I would encourage you to dig into Chapter 7 of the JAX-RS 2.0 specification document for deeper insight into this topic. It’s brief and to the point
  • Oh, and here is the link to the latest Bean Validation (1.1) spec doc



About Abhishek

Currently working as a Senior Product Manager in the Oracle Cloud Application Development team with a focus on Oracle Cloud PaaS portfolio. When not hovering in the clouds, I stay grounded with Java EE
This entry was posted in Java EE and tagged , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s