webService返回值无法强转成类对象
第一次用WEBSERVICE遇到了一点问题,希望了解的高手给予指点,多谢!
下面这段是SERVICE端代码 public UMUser getUserInfo(String userName, int scope, boolean withRoles,
String applicationName) throws Exception {
UMUser umUser = new UMUser();
Employee employee = new Employee();
List<String> roles = null;
// 根据应用名称判断应用是否存在,如不存在或者应用处于停效状态,抛出ServiceNotActiveException
try {
employee = (Employee) employeeDao.findByUID(userName);
roles = roleDao.findUserRolesByService(employee,applicationName);
} catch (Exception e) {
e.printStackTrace();
throw e;
}
if(!this.isAppActive(applicationName)){
Exception exc = new Exception("ServiceNotActiveException application not exist or inactive");
exc.printStackTrace();
throw exc;
}else{
umUser.setManagedService(employee.getManagedService());
umUser.setUserResources(null);
if(withRoles){
umUser.setUserRoles(roles);
}
}
return umUser;
}
这里是模拟客户端接收的代码:
public class CXFTest {
private static final String SERVICE_ADDRESS = "http://10.0.22.137:9001/UMService/services/userManageService?wsdl";
public static void main(String[] args) {
CXFTest cxftest = new CXFTest();
cxftest.getUserInfo("linb",1,false,"UM");
}
public void getUserInfo(String userName, int scope, boolean withRoles,
String applicationName){
JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
Client client = dcf.createClient(SERVICE_ADDRESS);
try {
Object[] objects = client.invoke("getUserInfo",userName,scope,withRoles,applicationName);
UmUser test = (UmUser) objects[0];
System.out.println(test.getManagedService());
} catch (Exception e) {
e.printStackTrace();
}
}
}
好了,现在问题开始,我在接收返回值时无法强转成对象,必需将我客户端对象路径放到错误提示时给出的路径中!只有这样才能接收。问题是我这个SERVICE需要给多个客户端。我也不能规定每个客户端都必需按照某一个特定路径来定义这个对象类呀!这是错误提示:
java.lang.ClassCastException: com.minsheng.um.service.service.UmUser cannot be cast to UmUser
at CXFTest.getUserInfo(CXFTest.java:18)
at CXFTest.main(CXFTest.java:9)
服务端的对象类我已经序列化了!
------解决方案--------------------
webservice可以传对象,相同语言的webservice也可以用对象互相通信,但不同语言的就不行,传非基本类型就会限制你的webservice的客户范围。