我有一个在Axis 2中开发的Web服务。名为EmployeeService。
EmployeeService.java
public class EmployeeService {
public Employee getEmployee(Employee emp) {
emp.getTeam().setTeamName("TeamName");
return emp;
}
}
Employee.java
public class Employee {
private String name;
private int age;
private String email;
private Team team;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Team getTeam() {
return team;
}
public void setTeam(Team team) {
this.team = team;
}
@Override
public String toString() {
return "Employee [name=" + name + ", age=" + age + ", email=" + email
+ ", team=" + team + "]";
}
}
Team.java
public class Team {
private String businessUnitName;
private String teamName;
public String getBusinessUnitName() {
return businessUnitName;
}
public void setBusinessUnitName(String businessUnitName) {
this.businessUnitName = businessUnitName;
}
public String getTeamName() {
return teamName;
}
public void setTeamName(String teamName) {
this.teamName = teamName;
}
@Override
public String toString() {
return "Team [businessUnitName=" + businessUnitName + ", teamName="
+ teamName + "]";
}
}
对于团队POJO中的teamName字段,我在WSDL中添加了一个限制,我正在使用自定义WSDL。
<simpleType name="teamNameType">
<restriction base="string">
<minLength value="0"/>
<maxLength value="80"/>
</restriction>
</simpleType>
服务已成功部署。
我正在使用Axis 1访问该服务。
EmployeeServiceLocator locator = new EmployeeServiceLocator();
EmployeeServicePortType endpoint = locator.getEmployeeServiceHttpSoap11Endpoint();
Employee emp = new Employee();
emp.setName( "SomeName" );
emp.setAge( new Integer( 21 ) );
emp.setEmail( "test@test.com" );
Team team = new Team();
team.setBusinessUnitName( "businessUnitName" );
emp.setTeam( team );
Employee ret = endpoint.getEmployee( emp );
TeamNameType resTeamName = ret.getTeam().getTeamName();
System.out.println( resTeamName.toString() );
从结果中,resTeamName没有任何函数来获取响应中的数据。
如果我将响应打印到控制台,我可以看到TeamName将作为响应的一部分。
打印回复的代码。
Call call = ( ( org.apache.axis.client.Stub ) endpoint )._getCall();
SOAPEnvelope envelope = call.getResponseMessage().getSOAPEnvelope();
Document doc = envelope.getAsDocument();
Transformer trans = TransformerFactory.newInstance().newTransformer();
trans.setOutputProperty( OutputKeys.METHOD, "xml" );
trans.setOutputProperty( OutputKeys.INDENT, "yes" );
trans.setOutputProperty( "{http://xml.apache.org/xslt}indent-amount", Integer.toString( 2 ) );
StringWriter sw = new StringWriter();
StreamResult result = new StreamResult( sw );
DOMSource source = new DOMSource( doc );
trans.transform( source, result );
String xmlString = sw.toString();
System.out.println( xmlString );
打印的回复是
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<ns:getEmployeeResponse xmlns:ns="http://employee">
<ns:return>
<ns:age>21</ns:age>
<ns:email>test@test.com</ns:email>
<ns:name>SomeName</ns:name>
<ns:team>
<ns:businessUnitName>businessUnitName</ns:businessUnitName>
<ns:teamName>TeamName</ns:teamName>
</ns:team>
</ns:return>
</ns:getEmployeeResponse>
</soapenv:Body>
</soapenv:Envelope>
但我找不到任何方法可以从Web服务的结果中访问TeamName数据。
如果我检查TeamName对象,那么我也无法看到任何可以返回Object的字段。
我也分享了我的服务,万一有人需要尝试一下。
https://dl.dropbox.com/u/27532041/employee.aar
谢谢, 保罗
答案 0 :(得分:0)
碰到了这个错误。 https://issues.apache.org/jira/browse/AXIS-119 请参阅Tom Jordahl的评论 “在这一点上,Axis 1.x只是不支持这些限制。” 和 “在Axis 1.x中没有计划支持这一点.Axis2正在使用XMLBeans,我相信它会生成代码以强制执行限制。”
答案 1 :(得分:0)
您的WSDL基本上是不正确的。我查看了AAR文件中的WSDL,simpleType
定义及其后代没有正确的命名空间。在Eclipse中,只需右键单击WSDL文件并选择“验证”,然后更正报告的错误。一旦WSDL文件正确,Axis 1.x应该能够生成一个存根,允许您设置teamName
属性。但是(正如我对K Vikas Chandran的评论中所解释的),Axis 1.x不会强制执行minLength
/ maxLength
限制方面。