简体   繁体   English

“minOccurs”并在Java Contract-First Web服务中生成类型

[英]“minOccurs” and generated type in Java Contract-First web service

I've got problem with contract-first generated web service in Java. 我在Java中使用契约优先生成的Web服务时遇到问题。 In my schema I've got something like this: 在我的架构中,我有这样的东西:

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

Which generates class with field like this: 哪个生成带有这样的字段的类:

protected long GUID;

Now, when I try to fire my webservice, when don't put any value for GUID, the default value of new java long is set (0) and no exception is thrown. 现在,当我尝试触发我的webservice时,当没有为GUID设置任何值时,new java long的默认值设置为(0)并且不抛出任何异常。 Of course this is not good behaviour because I the guid element should be required. 当然这不是好行为,因为我应该需要guid元素。 On the other hand, when I change my schema element to something like this: 另一方面,当我将schema元素更改为以下内容时:

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

(Which is from logic point of view incorrect because GUID element is required) generated class field looks like this: (这是从逻辑角度来看不正确,因为需要GUID元素)生成的类字段如下所示:

{protected Long GUID; }

And now when no GUID is set in web service execution, GUID value is null, which I can check I throw exception from java code. 现在,当Web服务执行中没有设置GUID时,GUID值为null,我可以检查我是否从java代码中抛出异常。

So I would like to kindly ask you for advice how to using minOccurs="0" generated class with protected Long GUID; 所以我想请教您如何使用带有受保护的Long GUID的minOccurs =“0”生成的类; (or at least getting exception when this value is not being set) (或者至少在没有设置此值时获得异常)

I'm using JAXB provided with glassfish 2.1.1 and SopaUI for executing web services. 我正在使用glassfish 2.1.1和SopaUI提供的JAXB来执行Web服务。

I was able to find solution. 我能够找到解决方案。 Very simple: I had to add nillable="true" : 很简单:我必须添加nillable =“true”:

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

And type of filed in generated class is Long now! 现在生成的类中提交的类型很长!
BTW. BTW。 This shows that describing your problems is good even if no one answer to it (maybe it's simmilar mechanism like in "Rubber duck debugging" method? :) ) 这表明即使没有人回答它,描述你的问题也是好的(也许它是像“橡皮鸭调试”方法中的类似机制?:))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM