在测试Web服务时获取Cast Exception

时间:2012-08-21 08:43:52

标签: java web-services wsdl

我是Java Web服务的新手,过去4天我一直坚持这一点。 这就是问题,我打算创建供其他应用程序使用的Web服务。要求是Web服务必须从表中返回多个记录,所以我创建了一个示例Web服务,这里是代码

接口

@WebService 
@SOAPBinding(style = Style.DOCUMENT)
public interface TestClassInt {

/**
 * @param args
 */

@WebMethod
SampleClass[] getCaseId (String country);
}

包含两个字符串对象的自定义类

public class SampleClass {
protected String caseid;
protected String dummy;

public SampleClass(){
    super();
}
public SampleClass(String caseid,String dummy){
    this.caseid=caseid;
    this.dummy=dummy;
}
public String getDummy() {
    return dummy;
}

public void setDummy(String dummy) {
    this.dummy = dummy;
}

public String getCaseid() {
    return caseid;
}

public void setCaseid(String caseid) {
    this.caseid = caseid;
}
}

这里是实现

@WebService(endpointInterface = "com.org.ccb.test.TestClassInt")
public class TestClassImpl implements TestClassInt {

public SampleClass[] getCaseId(String country) {
    // TODO Auto-generated method stub
    System.out.println("Console " + country);
    SampleClass tempSc[] = new SampleClass[2];
    tempSc[0]=new SampleClass();
    tempSc[1]=new SampleClass();
    tempSc[0].setCaseid(country);
    tempSc[0].setDummy(country);
    tempSc[1].setCaseid(country);
    tempSc[1].setDummy(country);
    return tempSc;
}

}

使用JBoss Developer Studio生成的WSDL是

<?xml version="1.0" encoding="UTF-8"?>
<wsdl:definitions name="TestClassImplService targetNamespace="http://test.ccb.org.com/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://test.ccb.org.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/">
<wsdl:types>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://test.ccb.org.com/" targetNamespace="http://test.ccb.org.com/" version="1.0">
<xs:element name="getCaseId" type="tns:getCaseId"/>
<xs:element name="getCaseIdResponse" type="tns:getCaseIdResponse"/>
<xs:complexType name="getCaseId">
<xs:sequence>
<xs:element minOccurs="0" name="arg0" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="getCaseIdResponse">
<xs:sequence>
<xs:element maxOccurs="unbounded" name="sampleClass" type="tns:sampleClass"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="sampleClass">
<xs:sequence>
<xs:element maxOccurs="1" name="caseid" type="xs:string"/>
<xs:element maxOccurs="1" name="dummy" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
</wsdl:types>
<wsdl:message name="getCaseIdResponse">
<wsdl:part name="parameters" element="tns:getCaseIdResponse">
</wsdl:part>
</wsdl:message>
<wsdl:message name="getCaseId">
<wsdl:part name="parameters" element="tns:getCaseId">
</wsdl:part>
</wsdl:message>
<wsdl:portType name="TestClassInt">
<wsdl:operation name="getCaseId">
  <wsdl:input name="getCaseId" message="tns:getCaseId">
</wsdl:input>
  <wsdl:output name="getCaseIdResponse" message="tns:getCaseIdResponse">
</wsdl:output>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="TestClassImplServiceSoapBinding" type="tns:TestClassInt">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="getCaseId">
  <soap:operation soapAction="" style="document"/>
  <wsdl:input name="getCaseId">
    <soap:body use="literal"/>
  </wsdl:input>
  <wsdl:output name="getCaseIdResponse">
    <soap:body use="literal"/>
  </wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="TestClassImplService">
<wsdl:port name="TestClassImplPort" binding="tns:TestClassImplServiceSoapBinding">
  <soap:address location="http://localhost:8080/rc_ccb/TestClass"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>

当给出SOAP请求时,我得到了强制转换异常 的 [Lcom.org.ccb.test.SampleClass;无法转换为com.org.ccb.test.SampleClass

要从Web服务获取对象数组,以下是WSDL定义吗?

<xs:complexType name="getCaseIdResponse">  
<xs:sequence>  
<xs:element maxOccurs="unbounded" name="sampleClass" type="tns:sampleClass"/>  
</xs:sequence>  
</xs:complexType>  
<xs:complexType name="sampleClass">  
<xs:sequence>  
<xs:element maxOccurs="1" name="caseid" type="xs:string"/>  
<xs:element maxOccurs="1" name="dummy" type="xs:string"/>  
</xs:sequence>  
</xs:complexType>

服务器:JBoss eap 5.1

有什么想法吗?

堆栈跟踪

14:31:46,743 WARNING [PhaseInterceptorChain] Interceptor for {http://test.ccb.org.com  /}TestClassImplService#{http://test.ccb.org.com/}getCaseId has thrown exception, unwinding now
org.apache.cxf.interceptor.Fault: [Lcom.org.ccb.test.SampleClass; cannot be cast to   com.org.ccb.test.SampleClass
at   org.apache.cxf.jaxws.interceptors.WrapperClassOutInterceptor.handleMessage(WrapperClassOutInterceptor.java:119)
at  org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:243)
at org.apache.cxf.interceptor.OutgoingChainInterceptor.handleMessage(OutgoingChainInterceptor.java:76)
at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:243)
at org.apache.cxf.transport.ChainInitiationObserver.onMessage(ChainInitiationObserver.java:109)
at org.apache.cxf.transport.servlet.ServletDestination.invoke(ServletDestination.java:98)
at org.apache.cxf.transport.servlet.ServletController.invokeDestination(ServletController.java:406)
at org.jboss.wsf.stack.cxf.ServletControllerExt.invoke(ServletControllerExt.java:173)
at org.jboss.wsf.stack.cxf.RequestHandlerImpl.handleHttpRequest(RequestHandlerImpl.java:61)
at org.jboss.wsf.stack.cxf.CXFServletExt.invoke(CXFServletExt.java:163)
at org.apache.cxf.transport.servlet.AbstractHTTPServlet.handleRequest(AbstractHTTPServlet.java:179)
at org.apache.cxf.transport.servlet.AbstractHTTPServlet.doPost(AbstractHTTPServlet.java:103)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
at org.apache.cxf.transport.servlet.AbstractHTTPServlet.service(AbstractHTTPServlet.java:159)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:235)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:183)
at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:95)
at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.process(SecurityContextEstablishmentValve.java:126)
at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.invoke(SecurityContextEstablishmentValve.java:70)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:158)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:330)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:829)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:598)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:451)
at java.lang.Thread.run(Thread.java:662)
  Caused by: java.lang.ClassCastException: [Lcom.org.ccb.test.SampleClass; cannot be cast to com.org.ccb.test.SampleClass
at   com.org.ccb.test.jaxws.GetCaseIdResponse_WrapperTypeHelper1.createWrapperObject(Unknown Source)
at  org.apache.cxf.jaxws.interceptors.WrapperClassOutInterceptor.handleMessage(WrapperClassOutInterceptor.java:103)
... 33 more

**主要编辑**创建Web服务向导生成了esdl文件,但它不包含maxOccurs =“unbounded”,所以我手动添加,保存并重新启动,认为它会改变,但发现wsdl isn不改变,它保留旧的,所以应该是问题!找不到为什么wsdl文件没有改变!

3 个答案:

答案 0 :(得分:0)

我认为JBoss生成的WSDL文件错误并且代码

<xs:complexType name="getCaseId">
<xs:sequence>
<xs:element minOccurs="0" name="arg0" type="xs:string"/>
</xs:sequence>
</xs:complexType>

应该是这样的

<xs:complexType name="getCaseId">
<xs:sequence>
<xs:element minOccurs="0" maxOccurs="unbounded" name="arg0" type="xs:string"/>
</xs:sequence>
</xs:complexType>

默认maxOccurs属性值为1,您的SimpleClass数组只是SingleClass的单个实例,导致类转换错误(尝试将数组转换为实例)。

我没有JBoss工作室的经验,但我建议你使用一些集合代替数组 - 也许JBoss可以比数组更好地处理集合。您也可以尝试JAX-WS。

答案 1 :(得分:0)

您需要了解一些先进的概念。

http://weblogs.java.net/blog/kohsuke/archive/2005/04/xmladapter_in_j.html

http://blog.bdoughan.com/2011/06/using-jaxbs-xmlaccessortype-to.html

http://docs.oracle.com/javaee/5/tutorial/doc/bnazf.html

我是如何做到的,是获得一个代表我的表/实体的类。然后我有一个list_of_entities的包装类;这是从Web服务返回的。所以,总的来说:

  1. 带有所需注释的实体类
  2. 包含所需注释的包装类
  3. 带有所需注释的Web服务
  4. 我已为下面的每一个提供了一个示例, MIGHT NOT RUN 。他们只是为了让你理解。

    //*********************** 1. your Object/Structure/table record you want to return
    
    //since you'll like to send the object across thus it should be serializable
    import java.io.Serializable;
    
    //The following two imports are specific to your case. Read about them
    import javax.xml.bind.annotation.XmlElement;
    import javax.xml.bind.annotation.XmlType;
    
    //The code related to the following classes is just to give you a taster of the JPA side of it
    import javax.persistence.Table;
    import javax.persistence.Column;
    import javax.persistence.Id;
    import javax.persistence.Entity;
    
    
    /**
     *  The class that will be the Type of your list, I have used it as an entity too for the JPA Layer
     *
     */
    @XmlType(namespace = "myNamespace")
    
    @Entity
    @Table(name="MY_TABLE"
        ,schema="MY_DB_SCHEMA"
    )
    
    public class SampleClass implements Serializable {
    
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    protected String caseid;
    protected String dummy;
    /**
     * @return the dummy
     */
    @XmlElement(namespace = "myNamespace")
    @Column(name="TYPE", nullable=true, length=225) 
    public String getDummy() {
        return dummy;
    }
    /**
     * @return the caseid
     */
    @Id
    @Column(name="TYPE", nullable=false, length=225)
    
    
    @XmlElement(namespace = "myNamespace")
    
    public String getCaseid() {
        return caseid;
    }
    /**
     * @param caseid the caseid to set
     */
    public void setCaseid(String caseid) {
        this.caseid = caseid;
    }
    /**
     * @param dummy the dummy to set
     */
    public void setDummy(String dummy) {
        this.dummy = dummy;
    }
    

    }

    // <强> * ** * ** * ** * *** < em> 2.对象包装类 ** * ** * < / EM> ** * ** * ****

    // This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v2.1-b02-fcs 
    // See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a> 
    // Any modifications to this file will be lost upon recompilation of the source schema. 
    // Generated on: 2009.02.12 at 12:55:08 PM GMT 
    //
    import java.util.ArrayList;
    import java.util.List;
    
    import javax.xml.bind.annotation.XmlAccessType;
    import javax.xml.bind.annotation.XmlAccessorType;
    import javax.xml.bind.annotation.XmlElement;
    import javax.xml.bind.annotation.XmlType;
    
    import YOURPACKAGE.SampleClass;
    
    /**
     * Wrapper class for List, makes it possible for JAXB to marshal/unmarshal java.util.List.
     * 
     * @param <T>
     *          Generic List
     */
    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(name = "myListType", propOrder = { "listReturn" })
    public class ExampleList {
    
      /**
       * instructing JAXB about the xml element and the name to use in the resulting xml structure.
       */
      @XmlElement(name = "sampleClass")
      protected List<SampleClass> listReturn;
    
      public ExampleList() {
        listReturn = new ArrayList<SampleClass>();
      }
    
      /**
       * @param local_list
       *          a generic list
       */
      public ExampleList(final List<SampleClass> local_list) {
        listReturn = local_list;
      }
    
      public List<SampleClass> getList() {
        return listReturn;
      }
    
    }
    

    // <强> * ** * ** * ** * ** * ** * *

    // <强> * ** * ** * *** 3.网络服务 ** * ** * ** * ** *

    import javax.ejb.Stateless;
    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 javax.persistence.Query;
    
    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;
    
    
    
    @Stateless(name = "myEJB")
    @WebService(serviceName = "myService", targetNamespace = "mynamespace", name = "myServicePT", portName = "myServicePort")
    public class ServiceImpl implements ServiceLocal {
    
      @SuppressWarnings("unused")
      private final Log log = LogFactory.getLog(ServiceImpl.class);
    
      @PersistenceContext(name = "MyContext")
      private EntityManager em;
    
    
      @WebMethod(operationName = "listAll")
      @WebResult(name = "myList", targetNamespace = "mynamespace")
      public ExampleList listAll() throws EastException {
        try {
          Query query = em.createNamedQuery("myQueryToGetMyListFromDB");
          return new ExampleList(query.getResultList());
        }
        catch (Exception e) {
          log.error("Error in retrieving  list", e);
    
        }
      }
    
    
    
    }
    

    // * ** * *** 3.1网络服务本地界面 * ** * ****

    import javax.ejb.Local;
    
    @Local
    public interface ServiceLocal {
        ExampleList listAll();
    }
    

    // <强> * ** * ** * ** * ** * ** * ** * ** * ****

答案 2 :(得分:0)

由于编辑wsdl的自下而上的服务是徒劳的,因此我从wsdl(使用JBoss Studio)生成了java骨架类,并在实现类中编写了我的业务。现在服务工作正常。无论如何,我不知道为什么在首先从java类生成wsdl时自动生成maxOccurs="unbounded"。感谢回复人@gkzmin @mhan @dan