English 中文(简体)
利用日本宇宙航空研究开发机构-世界宇宙航空研究开发机构网络服务的回归习俗物体
原标题:Return custom object from Web services using JAX-WS

问题太长了,但我希望预先提供所有信息。

我有以下几类:

package test.api.soap.server;

public class TestClassA {

    public TestClassA() {

    }

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

I want to expose it as web service but not directly so i created wrapper class: package test.api.soap.server;

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

@WebService
public class MyClientApiV2_5 {

    public MyClientApiV2_5() {

    }

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

我在ant任务中使用标准载荷创建服务器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> 

The out come are 2 files: MyClientApiV25Service.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>

<>MyClientApiV25SER_schema1.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="wsdlMyClientApiV25Service.wsdl" />
    </exec>
</target> 

然后,我写上简单的客户类别,以获得网络服务:

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();
    }
}

The problem is: Writing testClass.doA(); returns:

测试ClassA的类型没有定义。

任何建议?

问题回答

添加“XmlRootElement annotation”。 之后,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";
    }
}




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签