Java 如何使 WebMethod 参数成为必需
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2210346/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I make a WebMethod parameter required
提问by Dean Schulze
We use the "start from Java" approach to creating JAX-WS 2.0 web services using the Metro 1.5 stack. When we point a standard tool like SoapUI at one of our web services it indicates that parameters to WebMethods are optional. The generated xsd shows minOccurs=0.
我们使用“从 Java 开始”的方法来使用 Metro 1.5 堆栈创建 JAX-WS 2.0 Web 服务。当我们将像 SoapUI 这样的标准工具指向我们的一个 Web 服务时,它表明 WebMethods 的参数是可选的。生成的 xsd 显示 minOccurs=0。
I need a way to make WebMethod parameters required (maybe minOccurs=1 in the xsd) in the "start from Java" approach. I would think there is a Java annotation for this, but I haven't been able to find one. The XmlElement annotation has required attribute, but XmlElement cannot be applied to WebMethod parameters.
我需要一种在“从 Java 开始”方法中使 WebMethod 参数成为必需的方法(在 xsd 中可能是 minOccurs=1)。我认为有一个 Java 注释,但我找不到。XmlElement 批注具有 required 属性,但 XmlElement 不能应用于 WebMethod 参数。
Is there a way to make my WebMethod parameters required, short of manually editing the xsd and setting minOccurs to 1?
有没有办法让我的 WebMethod 参数成为必需的,而无需手动编辑 xsd 并将 minOccurs 设置为 1?
采纳答案by Dean Schulze
I've verified that Metro 2.0 does allow you to set @XmlElement(required=true)
on a parameter. The generated xsd does not have minOccurs=0
. It leaves minOccurs
out of the generated xsd so it assumes the default value of 1.
我已经验证 Metro 2.0 确实允许您设置@XmlElement(required=true)
参数。生成的 xsd 没有minOccurs=0
. 它忽略minOccurs
了生成的 xsd,因此它假定默认值为 1。
You also have to upgrade your JDK by putting the JAX-WS 2.2 webservices-api.jar
in the endorsed/
directory.
您还必须通过将 JAX-WS 2.2webservices-api.jar
放在endorsed/
目录中来升级您的 JDK 。
I posted the same question on the Java forums.
我在 Java 论坛上发布了同样的问题。
Thanks to jitu for both the answer and pointing out that minOccurs defaults to 1 so leaving it out of the .xsd has the desired effect.
感谢 jitu 的回答并指出 minOccurs 默认为 1,因此将其排除在 .xsd 之外具有预期的效果。
When @XmlElement(required=true) is set on the parameter SoapUI no longer indicates that the parameter as optional.
当 @XmlElement(required=true) 在参数上设置时,SoapUI 不再指示该参数为可选。
回答by Pascal Thivent
The generated xsd shows minOccurs=0.
生成的 xsd 显示 minOccurs=0。
This is per specification: any non-primitives are optional, only primitives are required.
这是每个规范:任何非原语都是可选的,只有原语是必需的。
I need a way to make WebMethod parameters required (maybe minOccurs=1 in the xsd) in the "start from Java" approach.
我需要一种在“从 Java 开始”方法中使 WebMethod 参数成为必需的方法(在 xsd 中可能是 minOccurs=1)。
This is not possible, unless you use primitives as previously mentioned.
这是不可能的,除非您使用前面提到的原语。
And actually, this is one of the reasons why Java-first sucks (yeah, developers like it but it just does not work): its fragile, the contract may change(!), its doesn't give you all the control you need, it doesn't fit well with WS-Security, etc. So, indeed, contract-firstis not pleasant, but at least, it works.
实际上,这就是 Java-first 糟糕透顶的原因之一(是的,开发人员喜欢它,但它就是行不通):它很脆弱,合约可能会改变(!),它没有给你你需要的所有控制,它不适合 WS-Security等。因此,实际上,契约优先并不令人愉快,但至少,它有效。
回答by Dean Schulze
Here is another discussion of this same question. According to the response Metro 2.0 supports putting @XmlElement on a method parameter which should solve my problem.
这是对同一问题的另一个讨论。根据响应 Metro 2.0 支持将 @XmlElement 放在应该可以解决我的问题的方法参数上。
http://forums.java.net/jive/thread.jspa?messageID=385565񞈝
http://forums.java.net/jive/thread.jspa?messageID=385565
Metro 2.0 was released on Dec. 10, 2009 so it is no longer in EA. I'll give it a try and see if it works.
Metro 2.0 于 2009 年 12 月 10 日发布,因此不再在 EA 中。我会尝试一下,看看它是否有效。