开发者

JAXWS/JAXB check that an object is empty (NOT a string)

For a Web Service, I am generating objects from WSDL/XSD. The typical XML would be like:

<Document>
  ...
  <Destination>
    <Type>Person</Type>
    <Name>Someone</Name>
    <Address>
      <Street>Main Street</Street>
      <City>Foo</City>
    </Address>
  </Destination>
  ...
</Document>

So, when Destination is specified, I have to validate it against a number of rules:

if 开发者_Go百科(document.getDestination() != null) {
  ... check validity
}

But then, someone passes:

<Destination/>

It's not null anymore and my validation rejects it. I would like to write:

if (document.getDestination() != null && !document.isEmpty()) {
  ... check validity
}

I don't like the idea of checking the presence of every sub-element as I will miss one at some point and create weird bugs. I saw the XMLNullRepresentationType from EclipseLink MOXy implementation but I'm using the Sun WebService stack. This is a pity because that would be a nice solution to my problem but it's not possible right now.

Does anyone know of a way to check that an element is empty ? (I repeat that it is a complex type, not a String). Enumerate the properties and check them all automatically maybe ?

Thanks.


You can use the JAX-WS @SchemaValidation annotation on the WebService endpoint to force the request to be validated against the provided schema. If you define that the child of the Destination element are mandatory, then omitting them will fail the schema validation.

You can plug your own error handler on the @SchemaValidation to report a nice error message to the user.


when you generate your JAXB/JAXWS objects from your XSD/WSDL you can use the JAXB customization facility to tell the JAXB compiler to geterate an "isSet" method for your Java Objects.

I typically include a file with this JAXB customization in all of my JAXB/JAXWS generation tasks:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

<jxb:bindings
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
    targetNamespace="http://java.sun.com/xml/ns/jaxb"
    version="1.0">

    <jxb:globalBindings
            generateIsSetMethod="true" 
    />

</jxb:bindings>

assuming that you have this XML in a file called "jaxb_global_customization.jxb" you can include this in your generation steps:

    <wsimport
        sourcedestdir="${dao.dir}/build/generated"
        destdir="${dao.dir}/build/bin/generated"
        wsdl="${dao.dir}/src/resources/schema/dao/SomeService.wsdl"
        wsdlLocation="./wsdl/SomeService.wsdl"
        >
        <binding dir="${dao.dir}/src/resources/schema/" includes="*.xml"/>
        <binding dir="${dao.dir}/src/resources/schema/" includes="*.xsd"/>
        <binding dir="${dao.dir}/src/resources/schema/" includes="*.jxb"/>
    </wsimport>

or with the XJC task:

    <xjc destdir="${dao.dir}/build/generated" package="com.example.dao">
        <schema dir="${dao.dir}/src/resources/schema" includes="*.xsd" />
        <binding dir="${dao.dir}/src/resources/schema" includes="*.jxb" />
    </xjc>

in your generated code for your sample, the JAXWS/JAXB generated code will include methods like so:

if (document.isSetDestination()) {
    // here you know that if Destination is a complex type, that the object which represents Destination is not null
    // if destination is a simple type, you know that it is not null and that it is not empty (meaning Destination != null && Destination.length() >0
    // if Destination is a repeating type, you know that it is not null and if it has any elements in it (ie, it's present and not an empty list)


  ... check validity
}

This greatly simplifies your code.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜