2011-04-25 102 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; 

}