2017-08-01 16 views
0

以下のコードを使用しようとしていますが、値を取得できません。JAXBを使用していますが値を取得していません

main 
{ 
String example="<SOAP:Envelope xmlns:SOAP=\"http://schemas.xmlsoap.org/soap/envelope/\"><SOAP:Body><ns0:findCustomerResponse xmlns:ns0=\"http://service.jaxws.blog/\"><return id=\"123\"><firstName>Jane</firstName><lastName>Doe</lastName></return></ns0:findCustomerResponse></SOAP:Body></SOAP:Envelope>"; 
ByteArrayInputStream bas=new ByteArrayInputStream(example.getBytes()); 
     JAXBContext jc = JAXBContext.newInstance(Customer.class); 
     Unmarshaller unmarshaller = jc.createUnmarshaller(); 
     SOAPMessage message = MessageFactory.newInstance().createMessage(null, 
       bas); 
     JAXBElement<Customer> jb = unmarshaller.unmarshal(message.getSOAPBody(), Customer.class); 

     Customer customer = jb.getValue(); 
     System.out.println(customer.id); 
     System.out.println(customer.firstName); 
     System.out.println(customer.lastName); 
     } 

@XmlAccessorType(XmlAccessType.FIELD) 
public class Customer { 

    @XmlAttribute 
    int id; 

    String firstName; 

    String lastName; 

} 

@Andreasは、あなたがそれは、あなたが以下のように使用することができます取得したいので、もしお客様は、SOAPボディの深い2ネストされている、言ったように私は値

+0

を得ました。また、firstNameとlastNameには@XmlElementを定義する必要があります。 – additionster

+0

@additionsterいいえ、あなたはいません。 '@XmlAccessorType(XmlAccessType.FIELD)'アノテーションは、特に注釈がない限り、すべてのフィールドが暗黙的に '@ XmlElement'であることを意味し、JAXBはgetterとsetterを使用せずにフィールド値を直接読み書きします。 – Andreas

+1

なぜ ''要素があなたの 'Customer'クラスに合っていると思いますか? 'id'属性は' '要素の上にあり、それは' ns0:findCustomerResponse>要素を挟んで2つ深くネストされています。 – Andreas

答えて

1

を取得していない午前理由を示唆してください私はそれがとてもエレガントではないと言うだろう:

JAXBElement<Customer> jb = unmarshaller.unmarshal(message.getSOAPBody().getFirstChild().getFirstChild(), Customer.class); 
+0

しかしその場合は123だけが来ていますが、他はnullとして来ています – user3428736

+0

奇妙なことに、私はちょうど試してみました。あなたが提供した同じスニペットで試しましたか? – mgyongyosi

+0

私はパッケージにパッケージ情報を設定しているので、問題があり、別のpackage.Thanksの値を取得する – user3428736

0

は、ほんの少しの変更を加えた:

を置き換え

JAXBElement jb = unmarshaller.unmarshal(message.getSOAPBody()、Customer.class);

unmarshaller.unmarshal JAXBElementののJB =(。。message.getSOAPBody()GETFIRSTCHILD()GETFIRSTCHILD()、Customer.class)と

と私は答えUは、JAXBクラス内のすべての変数のためのgetterメソッドとsetterメソッドを定義する必要があり

System.out.println(customer.id); // printed 123 
System.out.println(customer.firstName); // printed Jane 
System.out.println(customer.lastName); // printed Doe 
関連する問題