Jackson + Bean validation (JSR-303)

1,983 views
Skip to first unread message

Martin Duris

unread,
Jul 25, 2016, 4:12:02 PM7/25/16
to jackson-user

Hi guys,

is possible to perform basic validation ? For example max-length on string :

public class MyBean {
   private String name;

   @JsonGetter("name")
   @Min(value = 100)
   public String getTheName() {
      return name;
   }

}

So this call :
mapper.readValue(jsonString, MyBean.class)
would throw error in case thaht name is longer than 100 characters ?
Can be jackson (un)marshalling combined with JSR-303 ?

Thank you very much !

Tatu Saloranta

unread,
Jul 26, 2016, 2:13:04 AM7/26/16
to jackson-user
Bean Validation works on POJOs, so it would naturally be invoked after databind, as a separate phase. This is how frameworks like DropWizard do this I think. So, bind the value first, and if that succeeds invoke validator to ensure that contents are valid as per annotations.

-+ Tatu +-

 

--
You received this message because you are subscribed to the Google Groups "jackson-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jackson-user...@googlegroups.com.
To post to this group, send email to jackso...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Martin Duris

unread,
Jul 26, 2016, 11:32:04 AM7/26/16
to jackson-user
Thank you very much !

Dňa utorok, 26. júla 2016 8:13:04 UTC+2 Tatu Saloranta napísal(-a):

Martin Duris

unread,
Jul 26, 2016, 11:32:04 AM7/26/16
to jackson-user
Thank you very much, i will do it in second phase ;-)
(also look into DropWizard)


Dňa utorok, 26. júla 2016 8:13:04 UTC+2 Tatu Saloranta napísal(-a):
On Mon, Jul 25, 2016 at 1:11 PM, Martin Duris <mato....@gmail.com> wrote:

Volodymyr Mykhalchuk

unread,
Mar 1, 2018, 12:35:27 PM3/1/18
to jackson-user
Hi,

IMHO there is sense in having JSR-303 support by Jackson.

Jackson owns information about which part of data source (XML, JSON, etc) is mapped to which field of pojo. So Jackson can return more meaningful message on where exactly problem is.
In contrary we get default error from bean validator saying that some bean PersonX filed Name is empty, which means nothing to the user who provided source data in form of XML, JSON, etc
Especially this can be critical when fields are not mapped to same names.

Tatu Saloranta

unread,
Mar 1, 2018, 12:40:17 PM3/1/18
to jackson-user
On Thu, Mar 1, 2018 at 8:41 AM, Volodymyr Mykhalchuk
<vmykh...@gmail.com> wrote:
> Hi,
>
> IMHO there is sense in having JSR-303 support by Jackson.
>
> Jackson owns information about which part of data source (XML, JSON, etc) is
> mapped to which field of pojo. So Jackson can return more meaningful message
> on where exactly problem is.
> In contrary we get default error from bean validator saying that some bean
> PersonX filed Name is empty, which means nothing to the user who provided
> source data in form of XML, JSON, etc
> Especially this can be critical when fields are not mapped to same names.

There are 2 areas that I try to keep Jackson mostly out of: Structural
transformations (like XSLT)
and value/structural Validation (above and beyond well-formedness
verification of format).

What this means that I:

(1) Agree that validation is important, and should be possible but
(2) Disagree in that Jackson should do that.

The main argument I have is that amount of work needed to properly
validate things is huge (same goes
for transformations), and that half-assing it would be worse than
keeping out of the business altogether.

I am supportive of efforts that make it easy/easier to add validation
logic via extensions, perhaps even by Jackson
module. I just do not want and will not add any direct validation
support in core Jackson.

I hope this clarifies my thinking. I will not add direct JSR-303
support in Jackson; but I am happy to work with others
that such extensions are possible and get all the information they
need. And, to the degree feasible, can integrate
with processing so that to user it may look like it was fully integrated.

-+ Tatu +-
Reply all
Reply to author
Forward
0 new messages