SOAP 响应架构

发布于 2024-11-03 07:29:45 字数 1654 浏览 1 评论 0原文

我现在正在探索 SOAP WS,我创建了一个非常简单的 我将其公开为 Web 服务的类。

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

谢谢。

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);
      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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

回忆追雨的时光 2024-11-10 07:29:45

我不知道你是否已经解决了这个问题,但我最近开始从事 WS 工作并遇到了完全相同的问题。无论如何我解决了它:

你需要创建 2 个 Bean 类
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
    ....
        ....
}

并按照您的操作进行。
我希望这能解决您的问题。

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 .

清泪尽 2024-11-10 07:29:45

如果您想将 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;

}

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;

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文