我有一个在 Axis 2 中开发的 Web 服务。称为 EmployeeService。
EmployeeService.java
public class EmployeeService {
public Employee getEmployee(Employee emp) {
emp.getTeam().setTeamName("TeamName");
return emp;
}
}
雇员.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 + "]";
}
}
团队.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 + "]";
}
}
对于 Team 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 对象,那么我也看不到任何可以返回对象的字段。
我还分享了我的服务,以防有人需要尝试。
https://dl.dropbox.com/u/27532041/employee.aar
谢谢,保罗