gpt4 book ai didi

java - 将对象传递给 JAX WS

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:34:49 26 4
gpt4 key购买 nike

我强调了一种情况,在这种情况下无法调用以 java.lang.Object 作为参数的 Web 服务方法。

我正在使用 Netbeans 7.1 和 Tomcat 7。

为了创建、部署和访问 Web 服务,我遵循了以下给出的步骤: http://java.sun.com/developer/technicalArticles/J2SE/jax_ws_2/

网络服务端点如下:

package com.metrows;

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

/**
*
* @author 298790
*/
@WebService(serviceName = "NewWebService")
public class NewWebService {

/**
* This is a sample web service operation
*/
@WebMethod(operationName = "hello")
public String hello(@WebParam(name = "name") String txt) {
return "Hello " + txt + " !";
}

/**
* Web service operation
*/
@WebMethod(operationName = "sendObjParam")
public Object sendObjParam(@WebParam(name = "paramObj") Object paramObj) {
//TODO write your implementation code here:
System.out.println("In NewWebService.sendObjParam(...) "+paramObj);

return paramObj;
}

/**
* Web service operation
*/
@WebMethod(operationName = "passCustomParams")
@Oneway
public void passCustomParams(@WebParam(name = "tenant") Tenant tenant) {

System.out.println("In NewWebService.passCustomParams(...) : tenant = " + tenant);

System.out.println(": " + tenant.getMap());
}

}

租户如下:

package com.valueobjects;

import java.util.HashMap;

/**
*
* @author 298790
*/
public class Tenant {

private String tenantId;
private String tenantName;
private HashMap map;

public String getTenantId() {
return tenantId;
}

public void setTenantId(String tenantId) {
this.tenantId = tenantId;
}

public String getTenantName() {
return tenantName;
}

public void setTenantName(String tenantName) {
this.tenantName = tenantName;
}

public HashMap getMap() {
return map;
}

public void setMap(HashMap map) {
this.map = map;
}


}

客户端如下:

package metro_ws_client;

/**
*
* @author 298790
*/
public class Metro_WS_Client {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
System.out.println(hello("Aao Thakur !!!"));

Tenant tenant = new Tenant();
tenant.setTenantId("lnt");
tenant.setMap(new Tenant.Map());

passCustomParams(tenant);
Object o = new Object();
//String s = "Shot";
sendObjParam(o);
}


private static String hello(java.lang.String name) {
metro_ws_client.NewWebService_Service service = new metro_ws_client.NewWebService_Service();
metro_ws_client.NewWebService port = service.getNewWebServicePort();
return port.hello(name);
}

private static Object sendObjParam(java.lang.Object paramObj) {
com.metrows.NewWebService_Service service = new com.metrows.NewWebService_Service();
com.metrows.NewWebService port = service.getNewWebServicePort();
return port.sendObjParam(paramObj);
}

private static void passCustomParams(com.metrows.Tenant tenant) {
com.metrows.NewWebService_Service service = new com.metrows.NewWebService_Service();
com.metrows.NewWebService port = service.getNewWebServicePort();
port.passCustomParams(tenant);
}
}

在服务器启动期间,我得到以下异常(不是完整的堆栈跟踪,因为它是重复的):

WARNING: Unable to load class [com.sun.codemodel.JArrayClass$1] to check against the @HandlesTypes annotation of one or more ServletContentInitializers. 
java.lang.ClassNotFoundException: com.sun.codemodel.JArrayClass$1
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1678)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1523)
at org.apache.catalina.startup.ContextConfig.checkHandlesTypes(ContextConfig.java:2006)
at org.apache.catalina.startup.ContextConfig.processAnnotationsStream(ContextConfig.java:1969)
at org.apache.catalina.startup.ContextConfig.processAnnotationsJar(ContextConfig.java:1858)
at org.apache.catalina.startup.ContextConfig.processAnnotationsUrl(ContextConfig.java:1826)
at org.apache.catalina.startup.ContextConfig.processAnnotations(ContextConfig.java:1812)
at org.apache.catalina.startup.ContextConfig.webConfig(ContextConfig.java:1306)
at org.apache.catalina.startup.ContextConfig.configureStart(ContextConfig.java:896)
at org.apache.catalina.startup.ContextConfig.lifecycleEvent(ContextConfig.java:322)
at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:119)
at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:90)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5103)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:148)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:812)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:787)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:607)
at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:633)
at org.apache.catalina.startup.HostConfig.deployDescriptors(HostConfig.java:558)
at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:468)
at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1329)
at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:311)
at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:119)
at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:90)
at org.apache.catalina.util.LifecycleBase.setStateInternal(LifecycleBase.java:389)
at org.apache.catalina.util.LifecycleBase.setState(LifecycleBase.java:334)
at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:1041)
at org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:774)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:148)
at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:1033)
at org.apache.catalina.core.StandardEngine.startInternal(StandardEngine.java:291)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:148)
at org.apache.catalina.core.StandardService.startInternal(StandardService.java:443)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:148)
at org.apache.catalina.core.StandardServer.startInternal(StandardServer.java:727)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:148)
at org.apache.catalina.startup.Catalina.start(Catalina.java:621)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:322)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:450)
Feb 13, 2012 4:46:50 PM org.apache.catalina.startup.ContextConfig checkHandlesTypes
WARNING: Unable to load class [com.sun.codemodel.JCodeModel$1] to check against the @HandlesTypes annotation of one or more ServletContentInitializers.
java.lang.ClassNotFoundException: com.sun.codemodel.JCodeModel$1
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1678)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1523)
at org.apache.catalina.startup.ContextConfig.checkHandlesTypes(ContextConfig.java:2006)
at org.apache.catalina.startup.ContextConfig.processAnnotationsStream(ContextConfig.java:1969)
at org.apache.catalina.startup.ContextConfig.processAnnotationsJar(ContextConfig.java:1858)
at org.apache.catalina.startup.ContextConfig.processAnnotationsUrl(ContextConfig.java:1826)
at org.apache.catalina.startup.ContextConfig.processAnnotations(ContextConfig.java:1812)
at org.apache.catalina.startup.ContextConfig.webConfig(ContextConfig.java:1306)
at org.apache.catalina.startup.ContextConfig.configureStart(ContextConfig.java:896)
at org.apache.catalina.startup.ContextConfig.lifecycleEvent(ContextConfig.java:322)
at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:119)
at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:90)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5103)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:148)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:812)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:787)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:607)
at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:633)
at org.apache.catalina.startup.HostConfig.deployDescriptors(HostConfig.java:558)
at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:468)
at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1329)
at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:311)
at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:119)
at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:90)
at org.apache.catalina.util.LifecycleBase.setStateInternal(LifecycleBase.java:389)
at org.apache.catalina.util.LifecycleBase.setState(LifecycleBase.java:334)
at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:1041)
at org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:774)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:148)
at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:1033)
at org.apache.catalina.core.StandardEngine.startInternal(StandardEngine.java:291)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:148)
at org.apache.catalina.core.StandardService.startInternal(StandardService.java:443)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:148)
at org.apache.catalina.core.StandardServer.startInternal(StandardServer.java:727)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:148)
at org.apache.catalina.startup.Catalina.start(Catalina.java:621)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:322)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:450)
Feb 13, 2012 4:46:50 PM org.apache.catalina.startup.ContextConfig checkHandlesTypes
WARNING: Unable to load class [com.sun.codemodel.JExpr$3] to check against the @HandlesTypes annotation of one or more ServletContentInitializers.
java.lang.ClassNotFoundException: com.sun.codemodel.JExpr$3
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1678)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1523)
at org.apache.catalina.startup.ContextConfig.checkHandlesTypes(ContextConfig.java:2006)
at org.apache.catalina.startup.ContextConfig.processAnnotationsStream(ContextConfig.java:1969)
at org.apache.catalina.startup.ContextConfig.processAnnotationsJar(ContextConfig.java:1858)
at org.apache.catalina.startup.ContextConfig.processAnnotationsUrl(ContextConfig.java:1826)
at org.apache.catalina.startup.ContextConfig.processAnnotations(ContextConfig.java:1812)
at org.apache.catalina.startup.ContextConfig.webConfig(ContextConfig.java:1306)
at org.apache.catalina.startup.ContextConfig.configureStart(ContextConfig.java:896)
at org.apache.catalina.startup.ContextConfig.lifecycleEvent(ContextConfig.java:322)
at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:119)
at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:90)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5103)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:148)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:812)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:787)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:607)
at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:633)
at org.apache.catalina.startup.HostConfig.deployDescriptors(HostConfig.java:558)
at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:468)
at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1329)
at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:311)
at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:119)
at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:90)
at org.apache.catalina.util.LifecycleBase.setStateInternal(LifecycleBase.java:389)
at org.apache.catalina.util.LifecycleBase.setState(LifecycleBase.java:334)
at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:1041)
at org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:774)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:148)
at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:1033)
at org.apache.catalina.core.StandardEngine.startInternal(StandardEngine.java:291)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:148)
at org.apache.catalina.core.StandardService.startInternal(StandardService.java:443)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:148)
at org.apache.catalina.core.StandardServer.startInternal(StandardServer.java:727)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:148)
at org.apache.catalina.startup.Catalina.start(Catalina.java:621)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:322)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:450)

现在对网络方法“hello”的调用是成功的,但是当调用“sendObjParam”时出现以下异常;此外,无法在客户端中创建 java.util.Map 并将其设置在租户对象中:

Hello Aao Thakur !!! !
Exception in thread "main" javax.xml.ws.WebServiceException: java.lang.ClassCastException: java.lang.Object cannot be cast to org.w3c.dom.Element
at com.sun.xml.internal.ws.transport.http.client.HttpTransportPipe.process(HttpTransportPipe.java:185)
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:105)
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 $Proxy32.sendObjParam(Unknown Source)
at metro_ws_client.Metro_WS_Client.sendObjParam(Metro_WS_Client.java:35)
at metro_ws_client.Metro_WS_Client.main(Metro_WS_Client.java:22)
Caused by: java.lang.ClassCastException: java.lang.Object cannot be cast to org.w3c.dom.Element
at com.sun.xml.internal.bind.v2.runtime.AnyTypeBeanInfo.serializeURIs(AnyTypeBeanInfo.java:138)
at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.childAsXsiType(XMLSerializer.java:665)
at com.sun.xml.internal.bind.v2.runtime.property.SingleElementNodeProperty.serializeBody(SingleElementNodeProperty.java:141)
at com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl.serializeBody(ClassBeanInfoImpl.java:321)
at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.childAsXsiType(XMLSerializer.java:687)
at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:263)
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)
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)
... 14 more
Java Result: 1
BUILD SUCCESSFUL (total time: 1 second)

请指导我如何将标准 Java 对象——具体或接口(interface)、具有非原始成员的自定义对象传递给 Web 服务?

最佳答案

您传递给 Web 服务客户端方法的对象需要由 JAXB 序列化。这至少意味着它们需要用 @XMLRootElement 进行注释。这当然意味着您不能传入 new Object()(无论如何这几乎没有用)。

关于java - 将对象传递给 JAX WS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9262911/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com