当前位置: 代码迷 >> Web前端 >> webservice客户端调用复杂对象报错,求相助啊
  详细解决方案

webservice客户端调用复杂对象报错,求相助啊

热度:154   发布时间:2012-10-08 19:54:56.0
webservice客户端调用复杂对象报错,求帮助啊!!!

axis2调用一般类型的方法是正确的,但是在调用复杂类型的时候总是报错,不知道是为什么,现在把代码粘贴如下,希望高手帮忙解决一下啊!

?

------------------------------------

服务端代码:

package com.hanweb.service;

import java.text.SimpleDateFormat;
import java.util.Date;

import com.hanweb.entity.Customer;
import com.hanweb.entity.Project;

public class WebServiceTest {

?public String getTestResult(String username){
??
??return username + " :getTestResult 成功";
?}
?
?public Customer getCustomerInfo(){
??
??Customer cus = new Customer();
??cus.setCid("11111111");
??cus.setCname("开心人生");
??cus.setAddress("南京市天极");
??cus.setCompany("南京天极有限公司");
??
??return cus;
?}
?
?public Project getSimpleProject(Customer cus){
??
??Project pt = new Project();
??
??cus = new Customer();
??cus.setCid("1");
??cus.setCname("11111");
??cus.setAddress("222222222222");
??cus.setCompany("333333333");
??
??pt.setCus(cus);
??pt.setPdate(new SimpleDateFormat("yyyy-mm-dd").format(new Date(System.currentTimeMillis())));
??pt.setPname("0000000");
??return pt;
?}
}

?

------------------------------------------

实体类:

package com.hanweb.entity;

import java.io.Serializable;

public class Customer implements Serializable{

?private static final long serialVersionUID = 1L;

?private String cid;
?
?private String cname;
?
?private String company;
?
?public String getCid() {
??return cid;
?}

?public void setCid(String cid) {
??this.cid = cid;
?}

?public String getCname() {
??return cname;
?}

?public void setCname(String cname) {
??this.cname = cname;
?}

?public String getCompany() {
??return company;
?}

?public void setCompany(String company) {
??this.company = company;
?}

?public String getAddress() {
??return address;
?}

?public void setAddress(String address) {
??this.address = address;
?}

?private String address;
}

-------------------------------------

package com.hanweb.entity;

import java.io.Serializable;

public class Project implements Serializable{

?private static final long serialVersionUID = 1L;

?private String pname;
?
?private String pdate;
?
?private Customer cus;

?public String getPname() {
??return pname;
?}

?public void setPname(String pname) {
??this.pname = pname;
?}

?public String getPdate() {
??return pdate;
?}

?public void setPdate(String pdate) {
??this.pdate = pdate;
?}

?public Customer getCus() {
??return cus;
?}

?public void setCus(Customer cus) {
??this.cus = cus;
?}
}

?

---------------------------------------------------------

客户端代码:

package com.hanweb.client;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import javax.xml.namespace.QName;
import javax.xml.rpc.ServiceException;

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.rpc.client.RPCServiceClient;

import com.hanweb.entity.Customer;

public class WebServiceClient {

?private static final String SIMPLE_SERVICE_ENDPOINT =? "http://localhost:8088/webservice2.1/services/webServiceTest?wsdl";?
???
??? public static void main(String[] args){
???????
??? ?WebServiceClient client = new WebServiceClient();
??? ?client.getTestResultAxis();
??? ?client.getTestResultAxis2();
??? ?client.getTestObjectResultAxis2();
??? }
???
???
??? public void getTestResultAxis(){
???????
??????? try{
??????????? Service service = new Service();
??????????? Call call = (Call)service.createCall();
???????????
??????????? //创建访问路径
??????????? URL url = new URL(SIMPLE_SERVICE_ENDPOINT);
??????????? call.setTargetEndpointAddress(url);
???????????
??????????? //方法体调用
??????????? QName qname = new QName("http://webservice.hanweb.com","getTestResult");
??????????? call.setOperationName(qname);
???????????
??????????? //传参
??????????? call.addParameter("username",org.apache.axis.Constants.XSD_STRING,javax.xml.rpc.ParameterMode.IN);
??????????? call.setReturnType(org.apache.axis.Constants.XSD_STRING);
??????????? try{
??????????????? //参数传值
??????????????? String returnValue = (String)call.invoke(new Object[]{"axis 调用一般类型方法"});
??????????????
??????????????? System.out.println(returnValue);
???????????????
??????????? }catch(IOException e){
??????????????? e.printStackTrace();
??????????? }
???????????
??????? }catch(MalformedURLException e){
??????????? e.printStackTrace();
??????? }catch(ServiceException e){
??????????? e.printStackTrace();
??????? }
???????
??? }
???
??? public void getTestResultAxis2(){
??? ?try {
??????????? RPCServiceClient client = new RPCServiceClient();
??????????? Options option = client.getOptions();
??????????? EndpointReference EPR = new EndpointReference(SIMPLE_SERVICE_ENDPOINT);
??????????? option.setTo(EPR);
??????????? Class[] objClass = new Class[]{String.class};
??????????? QName qname = new QName("http://ws.apache.org/axis2","getTestResult");
??????????? System.out.println(client.invokeBlocking(qname, new Object[]{"axis2 调用一般类型方法"}, objClass)[0]);
???????????
??????? } catch (AxisFault e1) {
??????????? e1.printStackTrace();
??????? }
??? }
???
??? public void getTestObjectResultAxis2(){
??? ?
??? ?try{
??????????? //调用复杂类型 getCustomerInfo 方法
??????????? RPCServiceClient serviceClient = new RPCServiceClient();
??????????? Options option = serviceClient.getOptions();
???????????
??????????? EndpointReference address = new EndpointReference(SIMPLE_SERVICE_ENDPOINT);
??????????? option.setTo(address);
???????????
??????????? QName qname = new QName("http://ws.apache.org/axis2","getCustomerInfo");
???????????
??????????? Customer customer = (Customer)serviceClient.invokeBlocking(qname, new Object[]{}, new Class[]{Customer.class})[0];
??????????? System.out.println("Customer: " + customer.getCid() + " - " + customer.getCompany());

??????? } catch (AxisFault e) {
??????????? e.printStackTrace();
??????? }
??? }
}

?

------------------------------------

报错:

getTestObjectResultAxis2在调用的时候报错!

org.apache.axis2.AxisFault: javax.xml.stream.XMLStreamException: ParseError at [row,col]:[1,1]
Message: Premature end of file.
?at org.apache.axis2.AxisFault.makeFault(AxisFault.java:430)
?at org.apache.axis2.transport.TransportUtils.createSOAPMessage(TransportUtils.java:90)
?at org.apache.axis2.description.OutInAxisOperationClient.handleResponse(OutInAxisOperation.java:353)
?at org.apache.axis2.description.OutInAxisOperationClient.send(OutInAxisOperation.java:416)
?at org.apache.axis2.description.OutInAxisOperationClient.executeImpl(OutInAxisOperation.java:228)
?at org.apache.axis2.client.OperationClient.execute(OperationClient.java:163)
?at org.apache.axis2.client.ServiceClient.sendReceive(ServiceClient.java:548)
?at org.apache.axis2.client.ServiceClient.sendReceive(ServiceClient.java:528)
?at org.apache.axis2.rpc.client.RPCServiceClient.invokeBlocking(RPCServiceClient.java:102)
?at com.hanweb.client.WebServiceClient.getTestObjectResultAxis2(WebServiceClient.java:94)
?at com.hanweb.client.WebServiceClient.main(WebServiceClient.java:28)
Caused by: org.apache.axiom.om.OMException: javax.xml.stream.XMLStreamException: ParseError at [row,col]:[1,1]
Message: Premature end of file.
?at org.apache.axiom.om.impl.builder.StAXOMBuilder.next(StAXOMBuilder.java:249)
?at org.apache.axiom.soap.impl.builder.StAXSOAPModelBuilder.getSOAPEnvelope(StAXSOAPModelBuilder.java:156)
?at org.apache.axiom.soap.impl.builder.StAXSOAPModelBuilder.<init>(StAXSOAPModelBuilder.java:105)
?at org.apache.axis2.builder.SOAPBuilder.processDocument(SOAPBuilder.java:53)
?at org.apache.axis2.transport.TransportUtils.createDocumentElement(TransportUtils.java:164)
?at org.apache.axis2.transport.TransportUtils.createSOAPMessage(TransportUtils.java:112)
?at org.apache.axis2.transport.TransportUtils.createSOAPMessage(TransportUtils.java:88)
?... 9 more
Caused by: javax.xml.stream.XMLStreamException: ParseError at [row,col]:[1,1]
Message: Premature end of file.
?at com.sun.org.apache.xerces.internal.impl.XMLStreamReaderImpl.next(Unknown Source)
?at org.apache.axiom.om.impl.builder.StAXOMBuilder.parserNext(StAXOMBuilder.java:506)
?at org.apache.axiom.om.impl.builder.StAXOMBuilder.next(StAXOMBuilder.java:161)
?... 15 more

?

?

希望大侠帮忙啊,急需啊

1 楼 cd1989929 2012-06-29  
请问这个问题解决了吗?我也遇到了
  相关解决方案