调用webservice出现javax.xml.bind.UnmarshalException解决办法

来源:互联网 发布:如何制作常用算法演示 编辑:程序博客网 时间:2024/06/03 17:18
最近在做Android使用ksoap2包调用Webservice,自己写了Webservice发布在Jboss上做测试时, 
出现javax.xml.bind.UnmarshalException,异常内容为
Java代码  收藏代码
  1. Caused by: javax.xml.bind.UnmarshalException: unexpected element (uri:"http://apj.vote.webservice.com", local:"arg0"). Expected elements are <{}arg0>  



原因:服务器端的Webservice的接口方法的参数没有设置好参数名称和命名空间 

解决办法,使用@WebParam注解添加参数名称和命名空间 
例如: 
接口代码: 
Java代码  收藏代码
  1. package com.apj.webservice;  
  2.   
  3. import javax.jws.WebMethod;  
  4. import javax.jws.WebParam;  
  5. import javax.jws.WebResult;  
  6. import javax.jws.WebService;  
  7.   
  8.   
  9. @WebService(targetNamespace = "http://apj.vote.com", name = "VoteWebService", serviceName = "VoteWebService")  
  10. public interface VoteWebService {  
  11.     @WebResult(name="return",targetNamespace="http://apj.vote.com")  
  12.     @WebMethod  
  13.     public String vote(@WebParam(name = "teamid",targetNamespace="http://apj.vote.com"long teamid);  
  14.   
  15. }  


实现类 
Java代码  收藏代码
  1. package com.apj.webservice.impl;  
  2.   
  3. import javax.ejb.Remote;  
  4. import javax.ejb.Stateless;  
  5. import javax.inject.Inject;  
  6. import javax.jws.WebMethod;  
  7. import javax.jws.WebParam;  
  8. import javax.jws.WebResult;  
  9. import javax.jws.WebService;  
  10. import javax.persistence.EntityManager;  
  11. import javax.persistence.PersistenceContext;  
  12.   
  13. import org.jboss.logging.Logger;  
  14.   
  15. import com.apj.model.Team;  
  16. import com.apj.webservice.VoteWebService;  
  17.   
  18. @WebService(targetNamespace = "http://apj.vote.com", name = "VoteWebService", serviceName = "VoteWebService")  
  19. @Stateless  
  20. @Remote(VoteWebService.class)  
  21. public class VoteWebServiceImpl implements VoteWebService {  
  22.   
  23.     @WebResult(name="return",targetNamespace="http://apj.vote.com")  
  24.     @WebMethod  
  25.     public String vote(@WebParam(name = "teamid",targetNamespace="http://apj.vote.com"long teamid) {  
  26.   
  27.         System.out.println("team id ====" + teamid);  
  28.       
  29.         return teamid + "";  
  30.   
  31.     }  
  32.   
  33. }  
0 0
原创粉丝点击