Check soap requests against schema in JAX-WS code approach

I created the JAX-WS web service using JAXB annotations in some request fields to make them mandatory.

@XmlElement(required = true) protected String number; 

WSDL generated by cxf-java2ws-plugin is correct, in the fields minOccurs="0" not:

 <xs:element name="number" type="xs:string"/> 

But when a service receives a request that does not take these restrictions into account (missing fields), SoapFault or exception is not thrown.

I also tried adding @SchemaValidation to my WS class without effect.

How can query verification against schmema be automated (or rather, checking for constraints based on annotations)?

+7
java soap wsdl jax-ws cxf
source share
3 answers

I suggest two workarounds for your problem if you cannot solve it:

  • Confirm your bean after unwinding JAXB
  • Use CXF Validation Function

Validation after JAXB Sweeping

I think you can add the JAXB afterUnmarshal () callback to your Java bean request ( as described here ), and do all the validation you want in it (format validation or others).

For example:

 @XmlRootElement public class YourRequest { @XmlElement(required = true) protected String number; // some code here public void afterUnmarshal(Unmarshaller unmarshaller, Object parent) { if(number == null) { // Throw validation exception } } } 

CXF Check Function

Another possibility is to use bean validation with CXF ( as described here ). This is usually not necessary for schema-based validation, but if you need to have a more complex verification than schema-based validation, I think this may be the solution to your problem in the end.

Hope this helps you in expecting a better answer.

+2
source share

The default value for minOccurs is 1. Thus, your value must exist.
The default value for nillable is false. Thus, your value cannot be empty.
And you activated the schema check in your web service:

 @WebService @SchemaValidation(enabled = true) public class MyService { //... } 

And finally, last but not least, you have a resulting schema.

But the JAXB reference implementation does not have any validation for you.

You must do it yourself using the Java Xml Validation API

Here is a simple circuit check example

 public class XmlSchemaValidation { public static void main(String[] args) throws Exception { Something myObject = new Something(); validate(myObject); } public static void validate(Something myObject) throws Exception { Schema s = SchemaFactory .newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI) .newSchema(new File("Something.xsd")); Validator validator = s.newValidator(); //You can set an error handler //validator.setErrorHandler(errorHandler); validator.validate( new JAXBSource(JAXBContext.newInstance(Something.class), myObject)); } } 
+2
source share

Use some tool like jaxb2-maven-plugin to create the JAXB class. and Confirm before sending the payload to WebService or before accepting the payload to your system using the JSR 303 specification. Click here to see the JSR 303 validator.

This will make your system reliable.

+1
source share

All Articles