最近在做Android使用ksoap2包调用Webservice,自己写了Webservice发布在Jboss上做测试时,
出现javax.xml.bind.UnmarshalException,异常内容为
Caused by: javax.xml.bind.UnmarshalException: unexpected element (uri:"http://apj.vote.webservice.com", local:"arg0"). Expected elements are <{}arg0>
原因:服务器端的Webservice的接口方法的参数没有设置好参数名称和命名空间
解决办法,使用@WebParam注解添加参数名称和命名空间
例如:
接口代码:
package com.apj.webservice; import javax.jws.WebMethod; import javax.jws.WebParam; import javax.jws.WebResult; import javax.jws.WebService; @WebService(targetNamespace = "http://apj.vote.com", name = "VoteWebService", serviceName = "VoteWebService") public interface VoteWebService { @WebResult(name="return",targetNamespace="http://apj.vote.com") @WebMethod public String vote(@WebParam(name = "teamid",targetNamespace="http://apj.vote.com") long teamid); }
实现类
package com.apj.webservice.impl; import javax.ejb.Remote; import javax.ejb.Stateless; import javax.inject.Inject; import javax.jws.WebMethod; import javax.jws.WebParam; import javax.jws.WebResult; import javax.jws.WebService; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import org.jboss.logging.Logger; import com.apj.model.Team; import com.apj.webservice.VoteWebService; @WebService(targetNamespace = "http://apj.vote.com", name = "VoteWebService", serviceName = "VoteWebService") @Stateless @Remote(VoteWebService.class) public class VoteWebServiceImpl implements VoteWebService { @WebResult(name="return",targetNamespace="http://apj.vote.com") @WebMethod public String vote(@WebParam(name = "teamid",targetNamespace="http://apj.vote.com") long teamid) { System.out.println("team id ====" + teamid); return teamid + ""; } }