19

我正在尝试将一些实体发送到基于 ejb 的 Web 服务。这是我的网络服务客户端代码:

package ejbserviceclient;

import java.net.MalformedURLException;
import java.net.URL;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;

public class EjbServiceClient {

  public static void main(String[] args) throws MalformedURLException, JAXBException {

    JAXBContext context = JAXBContext.newInstance(EjbServiceClient.class);


    URL url = new URL("http://ipsedin:8080/WebServiceBeanService/WebServiceBean?wsdl");
    QName serviceName = new QName(
            "http://ejb.rpt.softcomputer.com/",
            "WebServiceBeanService");
    QName portName = new QName(
            "http://ejb.rpt.softcomputer.com/",
            "WebServiceBeanPort");

    Service service = Service.create(url, serviceName);
    Echo ejbService = service.getPort(portName, Echo.class);

    System.out.println(ejbService.echo("lololo"));
    System.out.println(ejbService.processExCourse(new ExCourse()));

  }
}

这是实体:

package ejbserviceclient;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class ExCourse {

int id;

}

但也有例外:

Exception in thread "main" javax.xml.ws.WebServiceException: javax.xml.bind.MarshalException
 - with linked exception:
[javax.xml.bind.JAXBException: class ejbserviceclient.ExCourse nor any of its super class is known to this context.]
    at com.sun.xml.internal.ws.message.jaxb.JAXBMessage.writePayloadTo(JAXBMessage.java:318)
    at com.sun.xml.internal.ws.message.AbstractMessageImpl.writeTo(AbstractMessageImpl.java:131)
    at com.sun.xml.internal.ws.encoding.StreamSOAPCodec.encode(StreamSOAPCodec.java:98)
    at com.sun.xml.internal.ws.encoding.SOAPBindingCodec.encode(SOAPBindingCodec.java:249)
    at com.sun.xml.internal.ws.transport.http.client.HttpTransportPipe.process(HttpTransportPipe.java:144)
    at com.sun.xml.internal.ws.transport.http.client.HttpTransportPipe.processRequest(HttpTransportPipe.java:83)
    at com.sun.xml.internal.ws.transport.DeferredTransportPipe.processRequest(DeferredTransportPipe.java:78)
    at com.sun.xml.internal.ws.api.pipe.Fiber.__doRun(Fiber.java:587)
    at com.sun.xml.internal.ws.api.pipe.Fiber._doRun(Fiber.java:546)
    at com.sun.xml.internal.ws.api.pipe.Fiber.doRun(Fiber.java:531)
    at com.sun.xml.internal.ws.api.pipe.Fiber.runSync(Fiber.java:428)
    at com.sun.xml.internal.ws.client.Stub.process(Stub.java:211)
    at com.sun.xml.internal.ws.client.sei.SEIStub.doProcess(SEIStub.java:124)
    at com.sun.xml.internal.ws.client.sei.SyncMethodHandler.invoke(SyncMethodHandler.java:98)
    at com.sun.xml.internal.ws.client.sei.SyncMethodHandler.invoke(SyncMethodHandler.java:78)
    at com.sun.xml.internal.ws.client.sei.SEIStub.invoke(SEIStub.java:107)
    at $Proxy19.processExCourse(Unknown Source)
    at ejbserviceclient.EjbServiceClient.main(EjbServiceClient.java:30)
Caused by: javax.xml.bind.MarshalException
 - with linked exception:
[javax.xml.bind.JAXBException: class ejbserviceclient.ExCourse nor any of its super class is known to this context.]
    at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:268)
    at com.sun.xml.internal.bind.v2.runtime.BridgeImpl.marshal(BridgeImpl.java:89)
    at com.sun.xml.internal.bind.api.Bridge.marshal(Bridge.java:130)
    at com.sun.xml.internal.ws.message.jaxb.JAXBMessage.writePayloadTo(JAXBMessage.java:310)
    ... 17 more
Caused by: javax.xml.bind.JAXBException: class ejbserviceclient.ExCourse nor any of its super class is known to this context.
    at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.reportError(XMLSerializer.java:214)
    at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.reportError(XMLSerializer.java:229)
    at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.childAsXsiType(XMLSerializer.java:621)
    at com.sun.xml.internal.bind.v2.runtime.BridgeImpl.marshal(BridgeImpl.java:136)
    at com.sun.xml.internal.bind.v2.runtime.CompositeStructureBeanInfo.serializeBody(CompositeStructureBeanInfo.java:96)
    at com.sun.xml.internal.bind.v2.runtime.CompositeStructureBeanInfo.serializeBody(CompositeStructureBeanInfo.java:44)
    at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.childAsXsiType(XMLSerializer.java:664)
    at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:263)
    ... 20 more
Caused by: javax.xml.bind.JAXBException: class ejbserviceclient.ExCourse nor any of its super class is known to this context.
    at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getBeanInfo(JAXBContextImpl.java:554)
    at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.childAsXsiType(XMLSerializer.java:616)
    ... 25 more
Java Result: 1
4

1 回答 1

43

看起来你没有JAXBContext正确初始化你的。您需要通知它所有可能@XmlRootElement被解析的类名。

因此你需要改变

JAXBContext context = JAXBContext.newInstance(EjbServiceClient.class)

JAXBContext context = JAXBContext.newInstance(ejbserviceclient.ExCourse.class)
于 2012-04-23T15:39:55.303 回答