2011-04-25 100 views
0

我正在研究SOAP WS,並且創建了一個非常簡單的 類,我將其公開爲Web服務。SOAP響應模式

@WebService 
public class StudentWS { 
    @WebMethod 
    public Student getStudent(){ 
     Student stud = new Student(); 
     stud.setId(99); 
     stud.setFirstName("John"); 
     stud.setLastName("Doe"); 
     stud.setGpa(2.1); 
     return stud; 
    } 
} 

當我把這個Web服務,返回的SOAP響應如下 這種格式。

<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"> 
    <S:Body> 
     <ns2:getStudentResponse xmlns:ns2="http://annotation/"> 
     <return> 
      <firstName>John</firstName> 
      <gpa>2.1</gpa> 
      <id>99</id> 
      <lastName>Doe</lastName> 
     </return> 
     </ns2:getStudentResponse> 
    </S:Body> 
</S:Envelope> 

我的問題是,是否有某種方式可以影響SOAP響應,以遵循如下所示的某種架構。

<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"> 
    <S:Body> 
     <ns2:getStudentResponse xmlns:ns2="http://annotation/"> 
     <student gpa="2.1"> 
      <id>99</id> 
      <name> 
       <firstName></firstName> 
       <lastName></lastName> 
      </name> 
     </student> 
     </ns2:getStudentResponse> 
    </S:Body> 
</S:Envelope> 

我的數據來自於像這樣的POJO類。

@XmlRootElement 
public class Student { 
    private int id; 
    private String firstName; 
    private String lastName; 
    private double gpa; 
    //getters and setters 
} 

謝謝。

回答

0

我不知道你是否已經解決了它,但我最近開始在WS上工作,並面臨完全相同的問題。我解決了它反正:

您需要創建2 bean類 豆1

public class ResultBean { 

    private String id; 
     private String student; 
    private StudentName name = new StudentName(); 

//corresponding getter setter methods 
    .... 
     .... 
     .... 
} 

豆2

public class StudentName { 

    private String firstName; 
    private String lastName; 
//corresponding getter setter methods 
    .... 
     .... 
} 

,並繼續爲u做。 我希望這可以解決您的問題。

0

你要創建兩個類,如果你想擁有GPA作爲屬性使用@XmlAttribute註釋...

批註在這個例子只是說明

public class Student { 

    @XmlAttribute 
    private String gpa; 

    @XmlElement 
    private String id; 

    @XmlElement 
    private Name name; 

} 

public class Name { 

    @XmlElement 
    private String firstName; 

    @XmlElement 
    private String lastName; 

}