0

我有一个在 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

谢谢,保罗

4

2 回答 2

0

遇到了这个bug。 https://issues.apache.org/jira/browse/AXIS-119 请参考 Tom Jordahl 的评论“此时 Axis 1.x 不支持这些限制。” 和“没有计划在 Axis 1.x 中支持这一点。Axis2 正在使用 XMLBeans,我相信它会生成代码来强制执行限制。”

于 2012-10-09T09:58:57.890 回答
0

您的 WSDL 基本上是不正确的。我查看了 AAR 文件中的 WSDL,simpleType定义及其后代没有正确的命名空间。在 Eclipse 中,只需右键单击 WSDL 文件并选择“验证”,然后更正报告的错误。一旦 WSDL 文件正确,Axis 1.x 应该能够生成允许您设置teamName属性的存根。但是(正如我在对 K Vikas Chandran 的评论中所解释的),Axis 1.x 不会强制执行minLength/maxLength限制方面。

于 2012-10-10T18:53:26.047 回答