SOAP 响应架构
我现在正在探索 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不知道你是否已经解决了这个问题,但我最近开始从事 WS 工作并遇到了完全相同的问题。无论如何我解决了它:
你需要创建 2 个 Bean 类
Bean 1.
Bean 2.
并按照您的操作进行。
我希望这能解决您的问题。
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.
Bean 2.
and proceed as u do .
I hope this solves your problem .
如果您想将 gpa 作为属性,则必须创建两个类并使用
@XmlAttribute
注释...本示例中的注释只是说明性的
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