@XmlElement(required=true) 对于@WebParam 不起作用
我正在使用 JAX-WS 构建 Web 服务。我有一个奇怪的问题,@WebParam
的注释 @XmlElement(required=true)
在某些 @WebService
类中有效,但不起作用在其他一些地方工作。
我在两个 @WebService 类中有非常相似的代码。什么可能导致这个问题?参数类型还是实体类?
编辑:添加示例代码
我有两个 Web 服务:
@WebService(name = "ClubMemberPortType", serviceName = "ClubMemberService", portName = "ClubMemberSoapPort", targetNamespace = "http://club.com/api/ws")
public class ClubMemberWS {
@WebMethod(operationName = "findClubMembersByClubId", action = "urn:findClubMembersByClubId")
@WebResult(name = "club_membership")
public List<ClubMembership> findClubMembershipsByClubId(@XmlElement(required=true)
@WebParam(name = "club_id") String clubId,
@WebParam(name = "status") StatusEnum status){
...
}}
第二
@WebService(name = "ClubPortType", serviceName = "ClubService", portName = "ClubSoapPort", targetNamespace = "http://club.com/api/ws")
public class ClubWS {
@WebMethod(operationName = "findClubByClubId", action = "urn:findClubByClubId")
@WebResult(name = "club")
public Club findClubByClubId(@XmlElement(required=true)
@WebParam(name = "club_id") String clubId) {
...
}}
第一个 Web 方法的生成架构是:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ws="http://club.com/api/ws">
<soapenv:Header/>
<soapenv:Body>
<ws:findClubMembersByClubId>
<club_id>?</club_id>
<!--Optional:-->
<status>?</status>
</ws:findClubMembersByClubId>
</soapenv:Body>
</soapenv:Envelope>
个 Web 方法的生成架构是:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ws="http://club.com/api/ws">
<soapenv:Header/>
<soapenv:Body>
<ws:findClubByClubId>
<!--Optional:-->
<club_id>?</club_id>
</ws:findClubByClubId>
</soapenv:Body>
</soapenv:Envelope>
所以第一个可以正常工作,第二个可以不工作。怎么可能呢? :(
I'm building web service using JAX-WS. I have a strange problem that the annotation @XmlElement(required=true)
for @WebParam
works in some @WebService
class, but doesn't work in some others.
I have very similar code in the two @WebService
classes. What may cause this problem? Parameter type or the entity class?
Edit: Add sample code
I have two web services:
@WebService(name = "ClubMemberPortType", serviceName = "ClubMemberService", portName = "ClubMemberSoapPort", targetNamespace = "http://club.com/api/ws")
public class ClubMemberWS {
@WebMethod(operationName = "findClubMembersByClubId", action = "urn:findClubMembersByClubId")
@WebResult(name = "club_membership")
public List<ClubMembership> findClubMembershipsByClubId(@XmlElement(required=true)
@WebParam(name = "club_id") String clubId,
@WebParam(name = "status") StatusEnum status){
...
}}
and
@WebService(name = "ClubPortType", serviceName = "ClubService", portName = "ClubSoapPort", targetNamespace = "http://club.com/api/ws")
public class ClubWS {
@WebMethod(operationName = "findClubByClubId", action = "urn:findClubByClubId")
@WebResult(name = "club")
public Club findClubByClubId(@XmlElement(required=true)
@WebParam(name = "club_id") String clubId) {
...
}}
The generated schema for the first web method is:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ws="http://club.com/api/ws">
<soapenv:Header/>
<soapenv:Body>
<ws:findClubMembersByClubId>
<club_id>?</club_id>
<!--Optional:-->
<status>?</status>
</ws:findClubMembersByClubId>
</soapenv:Body>
</soapenv:Envelope>
The generated schema for the second web method is:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ws="http://club.com/api/ws">
<soapenv:Header/>
<soapenv:Body>
<ws:findClubByClubId>
<!--Optional:-->
<club_id>?</club_id>
</ws:findClubByClubId>
</soapenv:Body>
</soapenv:Envelope>
So the first one works fine, the second one does not work. How is it possible? :(
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在
@WebParam
之后添加@XmlElement(required=true,nillable=false)
解决了我类似的问题。使用 CXF 2.7.9。没有尝试把@XmlElement
放在第一位,难道就这么简单吗?Adding
@XmlElement(required=true,nillable=false)
after@WebParam
solved my similar problem. Using CXF 2.7.9. Did not try putting@XmlElement
first, could it be that simple?我有同样的问题。我找到了使用单独的类作为服务方法参数的解决方案。
例如
网络方法
也许这会有所帮助
I have same problem. I found solution in using of separate class for parameters of service method.
e.g.
web-method
maybe this will help
如果您收到以下错误消息:“此位置不允许使用注释 @XmlElement”,则您可能使用了错误的导入语句。
将其更改为:
由于 Eclipse 建议将另一个包作为第一个选项,这是一个非常常见的错误。
If you are having the following error message: "The annotation @XmlElement is disallowed for this location", chances are you're using the wrong import statement.
Change it to:
As Eclipse suggests another package as the first option, it's a very common mistake.
您尝试更改@WebParam和@XmlElement中的顺序吗?实际上我有一个 WS,它是这样的:
生成的描述是:
模式定义:
就是这样!
You try change the order in @WebParam and @XmlElement? actually i have a WS whit this:
And the description generate is:
and the schema definition:
that's it!!
也许我找到了解决方案。如果有人遇到同样的问题,只需按照这个顺序将这些注释放入您的 Web 服务合约(接口)中,就像之前所有人回答的那样。
Perhaps I found the solution. If anyone encounters the same problem, just place these annotations in you web service contract(interface) in exact this order, as all guys answered before.