简体   繁体   English

java.lang.reflect.Method invoke()参数类型不匹配异常

[英]java.lang.reflect.Method invoke() argument Type mismatch exception

I am dynamically consuming a webservice by JAX-WS API. 我正在通过JAX-WS API动态使用Web服务。 Instantiating service class and invoking by web service by reflection API. 通过反射API实例化服务类并通过Web服务调用。

Though the expected and actual argument is same its getting "Argument type mismatch" Exception. 尽管期望的参数和实际的参数相同,但它会收到“参数类型不匹配”异常。

Quick help is appreciated. 快速帮助表示赞赏。

Class<?>[] paramTypes = serviceClassMethod.getParameterTypes();
System.out.println("Expected Param Class : "+paramTypes[0].getName());
System.out.println("Actual Param Class : "+reqVals[0].getClass());
System.out.println("Expected number : "+paramTypes.length);
System.out.println("Actual number : "+reqVals.length);
Object wsResponse = serviceClassMethod.invoke(service, reqVals);
System.out.println("Invocation successful...");

Output: 输出:

Expected Param Class : com.bla.bla.ws.User
Actual Param Class : class com.bla.bla.ws.User
Expected number : 1
Actual number : 1

This sounds like a classloading issue. 这听起来像一个类加载问题。 The JRE treats the same class loaded by different classloaders as different class. JRE将不同的类加载器加载的同一类视为不同的类。 It depends on how you create your parameter object. 这取决于您如何创建参数对象。 You can test this using: 您可以使用以下方法进行测试:

paramTypes[0].equals(reqVals[0].getClass())

If this is not true, you are using two different classes. 如果不是这样,则使用两个不同的类。

A solution would be to ensure your parameter object is created with the correct class using paramTypes[0].newInstance(); 一种解决方案是使用paramTypes[0].newInstance();确保使用正确的类创建参数对象paramTypes[0].newInstance();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM