2011-10-23 36 views
13

我正在使用JAX-WS構建Web服務。我有一個奇怪的問題,註釋@XmlElement(required=true)@WebParam工作在一些@WebService類,但在其他一些工作。@WebParam的@XmlElement(必需= true)不起作用

我在兩個@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> 

所以第一個工作正常,第二個是行不通的。這怎麼可能? :(

+0

這兩種情況的任何代碼示例?您遇到的實際問題(例如,例外情況,對行爲的更好描述等)? –

+1

我已經添加了代碼。 – Shichao

+0

請添加錯誤的堆棧跟蹤 – Tomer

回答

3

我有同樣的問題。我找到了解決方案,使用單獨的類的服務方法的參數。

@XmlType(name="SampleRequestType", propOrder={"title", "ref"}) 
public class SampleRequest { 
    @XmlElement(name="title", required=false) 
    private String title; 
    @XmlElement(name="ref", required=true) 
    private String ref; 
    ... 

網絡的方法

@WebMethod 
public String sampleMethod(@WebParam(name = "params") SampleRequest params) { 

也許這將幫助

+2

如果我們這樣做,'SampleRequestType'本身顯示爲可選項 – NamingException

0

您嘗試更改@WebParam和@XmlElement中的順序嗎?實際上我有一個WS絲毫這樣的:

public Persona consultarPersona(@WebParam(name = "cedula") @XmlElement(required=true, nillable=false, name="cedula", namespace="cedulaParam") String cedula) 

和描述產生是:

<xsd:schema> 
<xsd:import namespace="cedulaParam" schemaLocation="http://eniacstein-pc:8080/WSDL_Sample/GestorPersonas?xsd=2"/> 
</xsd:schema> 

和模式定義:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" version="1.0" targetNamespace="cedulaParam"> 
<xs:element name="cedula" type="xs:string"/> 
</xs:schema> 

這就是它!

4

@WebParam之後加上@XmlElement(required=true,nillable=false)解決了我的類似問題。使用CXF 2.7.9。沒有先嚐試@XmlElement,這可能是那麼簡單嗎?

+0

獲取註釋@XmlElement不允許用於這個位置如何解決這個問題使用RSA進行開發如果我需要更新cxf版本,請幫助與版本的Maven鏈接 – Rajesh

2

如果您有以下錯誤信息:「註釋@XmlElement不允許這個位置」,機會是你使用了錯誤的import語句。

將其更改爲:

import javax.xml.bind.annotation.XmlElement; 

由於蝕意味着另一個包作爲第一個選項,這是一個非常常見的錯誤。

相關問題