开发者

SOAP Response Schema

开发者 https://www.devze.com 2023-02-28 17:07 出处:网络
I am exploring SOAP WS right now and I created a very simple class that I am exposing as a Web Service.

I am exploring SOAP WS right now and I created a very simple class that I am exposing as a Web Service.

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

When I call this Web Service, the returned SOAP response follows this format.

<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>

My question is, is there some way that I could influence the SOAP response to follow some sort of Schema like below.

<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>

My data comes from a POJO class like this.

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

Thanks.


I dont know if you have already solved it but i recently started working on WS and faced exactly same problem . I solved it anyways :

You need to create 2 Bean classes Bean 1.

public class ResultBean {

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

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

Bean 2.

public class StudentName {

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

and proceed as u do . I hope this solves your problem .


You have to create two classes and use @XmlAttribute annotation if you want to have gpa as a attribute...

Annotations in this example are just illustrative

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;

}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号