调用SOAP服务出现object is not an instance of declaring class错误求助
Hey there, let's dig into this serialization issue you're facing with your Axis 1.x SOAP client. First, let's recap the problem details clearly:
Your Code & Context
When calling the SOAP service with this line:
java.lang.Object _resp = _call.invoke(new java.lang.Object[] {XMLRequest});
Your XMLRequest is an instance of com.webservices.MaintenanceRequest@1b834def, but you're hitting this error:
2018-05-17 11:18:59.703 ERROR 7104 --- [nio-8080-exec-1] o.a.a.e.s.BeanSerializer : Exception: java.lang.IllegalArgumentException: object is not an instance of declaring class at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_144] at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_144] at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_144] at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_144] at org.apache.axis.utils.BeanPropertyDescriptor.get(BeanPropertyDescriptor.java:127) ~[axis-1.4.jar:?] at org.apache.axis.encoding.ser.BeanSerializer.serialize(BeanSerializer.java:192) [axis-1.4.jar:?] at org.apache.axis.encoding.SerializationContext.serializeActual(SerializationContext.java:1504) [axis-1.4.jar:?]
Root Cause Breakdown
This error pops up because Axis's BeanSerializer is trying to use reflection to call a getter method on your MaintenanceRequest instance, but the method's declaring class doesn't match the actual type of the object (or vice versa). Common culprits include JavaBean rule violations, class loader mismatches, or incorrect Axis configuration.
Fix Suggestions
Here are actionable steps to resolve this:
Validate JavaBean Compliance
Double-check that yourMaintenanceRequestclass follows JavaBean rules strictly:- All properties have public getter methods (use
isXXX()for boolean properties,getXXX()for others) - Getter method names match the property name exactly (e.g.,
getXmlData()for a property namedxmlData—no typos allowed) - Getters return the correct type matching the property, and there are no conflicting method signatures
- All properties have public getter methods (use
Verify Axis Type Mapping Configuration
If you're using a WSDD file (client-config.wsdd or service-config.wsdd) to define type mappings, ensure it correctly mapscom.webservices.MaintenanceRequestto its corresponding XML type, and specifiesBeanSerializeras the serializer. For example:<typeMapping xmlns:ns="http://your-webservice-namespace" qname="ns:MaintenanceRequest" type="java:com.webservices.MaintenanceRequest" serializer="org.apache.axis.encoding.ser.BeanSerializerFactory" deserializer="org.apache.axis.encoding.ser.BeanDeserializerFactory" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />If you generated the client from WSDL, confirm the generated stubs and type mappings align with your actual
MaintenanceRequestclass.Check for Class Loader Issues
This is a frequent gotcha in container environments (like Tomcat):- Ensure
MaintenanceRequestisn't present in multiple locations (e.g., both WEB-INF/classes and a JAR in WEB-INF/lib) - Confirm that the class loader loading
MaintenanceRequestis the same one used by Axis's classes. If not, the JVM treats them as entirely different classes even if the package and name match.
- Ensure
Test Manual Serialization
Isolate the problem by writing a small test to serialize yourMaintenanceRequestinstance directly withBeanSerializer:MaintenanceRequest req = new MaintenanceRequest(); // populate req with test data BeanSerializer serializer = new BeanSerializer(MaintenanceRequest.class, null); SerializationContext ctx = new SerializationContext(System.out, null); serializer.serialize(null, req, ctx);If this test throws the same error, you know the issue is with the class itself or its serialization setup, not the SOAP call context.
Rule Out Inheritance Problems
IfMaintenanceRequestinherits from a parent class, ensure the parent class also follows JavaBean rules. Axis'sBeanSerializermay try to serialize parent class properties, and any invalid getters there could trigger the same error.
内容的提问来源于stack exchange,提问作者Rajeswari Reddy

