0

We are building a RESTFUL JSON based web service, where we need to validate the input bean parameters, such as not blank, not null etc. We are able to validate the bean parameters using annotations.

We have overloaded the CXF Inbound interceptor to send business defined HTTP error code instead of plain HTTP 500 error, in case of validation failures.

As we are building the bean (stub) classes from WADL and XSD file, every time we generate new bean classes, these validator annotation are lost.

Hence, instead of using annotations, we are trying externalize the validations through some external property.

Please let us know, if there is any way to validate the incoming request JSON, through externalized constraints property/XML file.

Thanks, Indranil

Indranil
  • 1,776
  • 1
  • 17
  • 22

2 Answers2

0

What are you using to generate your classes?

If you are using Jaxb then you can use plugins to add annotations to the classes when they are generated. Jaxb2 annotate plugin can be used alongside jaxb binding files (docs here: jaxb customisation to customise what is being generated.

The second option is to use Json validation as this question wanted to: validating incoming json request. For this you'll need an xsd or json schema which it seems you have.

Community
  • 1
  • 1
fiw
  • 716
  • 5
  • 8
  • We are using wadl2java CSF tool to generate the stub classes. Does wadl2java has any options to include validations in stub classes. – Indranil Oct 20 '15 at 13:39
  • http://cxf.apache.org/docs/jaxrs-services-description.html#JAXRSServicesDescription-JAXBcustomizations shows you can use .xjb binding files to customise the output – fiw Oct 20 '15 at 13:58
  • Hi @fiw , if you can point me to an example that would be really helpful. – Indranil Oct 20 '15 at 16:44
  • Can you update your question with an example of your wadl and I'll show you what I mean – fiw Oct 21 '15 at 07:39
  • Thanks @fiw for your help. I am going to use Hibernate XML based validation (validation.xml and constraints.xml combination), which I can put outside of stub classes and load during start up. It works, thanks for your help again :) – Indranil Oct 21 '15 at 17:48
0

@fiw I have a Wadl which looks like this -

<xs:element name="password">
    <xs:simpleType>
    <xs:restriction base="xs:string">
    <xs:length value="8"/>
    </xs:restriction>
    </xs:simpleType>
</xs:element>

I am using wadl2java by Apache CXF plugin to generate the pojos but am unable to add validation annotations to the classes when they are generated.

I am not clear how to use the jaxb plugin to add validation annotations like @size for string validation

  • Found a partial solution - use this plugin - https://github.com/krasa/krasa-jaxb-tools It works with wadl2java as well for auto generation of validations like notnull, minsize, etc. – Rishabh Baid Sep 26 '20 at 08:25