15

JAX-WS を使用して Web サービスを構築しています。@XmlElement(required=true)注釈が一部のクラスでは@WebParam機能するが、他の一部のクラスでは機能しないという奇妙な問題があり@WebServiceます。

@WebService私は2つのクラスで非常によく似たコードを持っています。この問題の原因は何ですか? パラメータタイプまたはエンティティクラス?

編集:サンプルコードを追加

2 つの 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>

2 番目の 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>

したがって、最初のものは正常に機能しますが、2 番目のものは機能しません。それはどのように可能ですか?:(

4

5 に答える 5

5

私の同様の問題を解決@XmlElement(required=true,nillable=false)した後に追加します。@WebParamCXF 2.7.9 を使用。最初に入れようとしなかっ@XmlElementたのですが、そんなに簡単でしょうか?

于 2014-03-18T10:00:33.650 に答える
4

私は同じ問題を抱えています。サービスメソッドのパラメーターに別のクラスを使用することで解決策を見つけました。

例えば

@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) {

多分これは役立つでしょう

于 2012-07-14T17:10:00.747 に答える
2

次のようなエラー メッセージが表示された場合: "この注釈 @XmlElement は、この場所では許可されていません" というメッセージが表示された場合は、間違った import ステートメントを使用している可能性があります。

次のように変更します。

import javax.xml.bind.annotation.XmlElement;

Eclipse が最初のオプションとして別のパッケージを提案しているように、これは非常によくある間違いです。

于 2016-12-10T19:24:18.363 に答える