"minOccurs" and the generated type in the Java Contract-First web service

I have a problem with a contract based web service in Java. In my schema, I have something like this:

<element maxOccurs="1" minOccurs="1" name="GUID" type="xs:long" /> 

What generates a class with such a field:

 protected long GUID; 

Now, when I try to start my web service, when the values ​​for the GUID are not set, the default value for the new java long is set to (0) and no exception is thrown. Of course, this is not good behavior, because I have to use a guiding element. On the other hand, when I change my circuit element to something like this:

 <element maxOccurs="1" minOccurs="0" name="GUID" type="xs:long" /> 

(which is wrong from a logical point of view, since a GUID element is required) the generated class field looks like this:

 {protected Long GUID; } 

And now that the GUID is not set in the web service execution, the GUID is null, and I can verify that I have selected an exception from the java code.

Therefore, I would like to ask you how to use the minOccurs = "0" generated class with a protected Long GUID; (or at least get an exception if this value is not set)

I am using JAXB, equipped with Glassfish 2.1.1 and SopaUI to execute web services.

+4
source share
1 answer

I was able to find a solution. Very simple: I had to add nillable = "true":

 <element maxOccurs="1" minOccurs="1" name="GUID" type="xs:long" nillable="true" /> 

And the type of filed in the generated Long class now!
BTW. This shows that the description of your problems is good, even if no one answers it (maybe this is a similar mechanism as in the "Rubber duck debugging" method? :))

0
source

Source: https://habr.com/ru/post/1447162/


All Articles