Web Services 使用JAX-WS从Web服务返回自定义对象

0ejtzxu1  于 2022-11-15  发布在  其他
关注(0)|答案(1)|浏览(175)

这个问题有点长,但我想提前提供所有信息
我有下面的类:

package test.api.soap.server;

public class TestClassA {

    public TestClassA() {

    }

    public String doA() {
        System.out.println("Start doA()");
        return "In do A";
    }
}

我想把它公开为Web服务,但不是直接公开,所以我创建了 Package 器类:服务器;

import javax.jws.WebMethod;
import javax.jws.WebService;

@WebService
public class MyClientApiV2_5 {

    public MyClientApiV2_5() {

    }

    @WebMethod
    public TestClassA getTestClassA() {
        return new TestClassA();
    }
}

我在ant task中使用标准wsgen创建服务器WSDL:

<target name="Create-JAXWS-SOAP-Server">
    <echo message="Generate SOAP server stubs" />
    <exec executable="${wsgen-cmd}">
        <arg value="-verbose" />
        <arg value="-classpath" />
        <arg value="${build}" />
        <arg value="-wsdl" />
        <arg value="-d" />
        <arg value="${build}" />
        <arg value="-r" />
        <arg value="wsdl" />
        <arg value="-s" />
        <arg value="${src}" />
        <arg value="-keep" />
        <arg value="test.api.soap.server.MyClientApiV2_5" />
    </exec>
</target>

结果是2个文件:我的客户端ApiV 25服务. wsdl

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<!-- Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.2.3-b01-. -->
<definitions targetNamespace="http://server.soap.api.test/" name="MyClientApiV2_5Service" xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:wsp="http://www.w3.org/ns/ws-policy" xmlns:tns="http://server.soap.api.test/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsp1_2="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
  <types>
    <xsd:schema>
      <xsd:import namespace="http://server.soap.api.test/" schemaLocation="MyClientApiV25Service_schema1.xsd"/>
    </xsd:schema>
  </types>
  <message name="getTestClassA">
    <part name="parameters" element="tns:getTestClassA"/>
  </message>
  <message name="getTestClassAResponse">
    <part name="parameters" element="tns:getTestClassAResponse"/>
  </message>
  <portType name="MyClientApiV2_5">
    <operation name="getTestClassA">
      <input wsam:Action="http://server.soap.api.test/MyClientApiV2_5/getTestClassARequest" message="tns:getTestClassA"/>
      <output wsam:Action="http://server.soap.api.test/MyClientApiV2_5/getTestClassAResponse" message="tns:getTestClassAResponse"/>
    </operation>
  </portType>
  <binding name="MyClientApiV2_5PortBinding" type="tns:MyClientApiV2_5">
    <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
    <operation name="getTestClassA">
      <soap:operation soapAction=""/>
      <input>
        <soap:body use="literal"/>
      </input>
      <output>
        <soap:body use="literal"/>
      </output>
    </operation>
  </binding>
  <service name="MyClientApiV2_5Service">
    <port name="MyClientApiV2_5Port" binding="tns:MyClientApiV2_5PortBinding">
      <soap:address location="REPLACE_WITH_ACTUAL_URL"/>
    </port>
  </service>
</definitions>

我的客户端ApiV 25服务架构1.xsd

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema version="1.0" targetNamespace="http://server.soap.api.test/" xmlns:tns="http://server.soap.api.test/" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="getTestClassA" type="tns:getTestClassA"/>
  <xs:element name="getTestClassAResponse" type="tns:getTestClassAResponse"/>
  <xs:complexType name="getTestClassA">
    <xs:sequence/>
  </xs:complexType>
  <xs:complexType name="getTestClassAResponse">
    <xs:sequence>
      <xs:element name="return" type="tns:testClassA" minOccurs="0"/>
    </xs:sequence>
  </xs:complexType>
  <xs:complexType name="testClassA">
    <xs:sequence/>
  </xs:complexType>
</xs:schema>

我创建了客户端存根:

<target name="Create-JAXWS-SOAP-Client" >
    <echo message="Generate SOAP client stubs" />
    <exec executable="${wsimport-cmd}">
        <arg value="-verbose" />
        <arg value="-keep" />
        <arg value="-s" />
        <arg value="${src}" />
        <arg value="-d" />
        <arg value="${build}" />
        <arg value="-p" />
        <arg value="test.api.soap.client.stubs" />
        <arg value="wsdl\MyClientApiV25Service.wsdl" />
    </exec>
</target>

然后编写简单的客户端类来获取Web服务:

package test.api.soap.client;

import java.net.MalformedURLException;
import java.net.URL;

import javax.xml.namespace.QName;

import test.api.soap.client.stubs.MyClientApiV25;
import test.api.soap.client.stubs.MyClientApiV25Service;
import test.api.soap.client.stubs.TestClassA;

public class SampleClient {

    private MyClientApiV25         api         = null;
    static MyClientApiV25Service service;

    public SampleClient() throws MalformedURLException {
        api = service.getMyClientApiV25Port();

        TestClassA testClass = api.getTestClassA();
        testClass.doA();
    }
}

问题是:编写testClass.doA();退货:

未定义类型TestClassA的方法doA()
有什么建议吗?

2ul0zpep

2ul0zpep1#

添加@XmlRootElement注解。JaxB将处理复杂类型的封送/解送。默认情况下,公共字段将自动绑定,但您也可以自己使用@XmlElement注解它们。

package test.api.soap.server;

@XmlRootElement
public class TestClassA {

    public TestClassA() {

    }

    public String doA() {
        System.out.println("Start doA()");
        return "In do A";
    }
}

相关问题