当前位置: 首页 > 知识库问答 >
问题:

测试web服务时获取强制转换异常

太叔京
2023-03-14

我是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;
}

}

使用JBossDeveloperStudio生成的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服务获取对象数组,以下是WSDLhtml" target="_blank">定义,对吗?

<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没有更改,它保留了旧的文件,所以这应该是问题所在!找不到wsdl文件没有更改的原因!

共有3个答案

窦国源
2023-03-14

由于它是一个自底向上的服务,编辑wsdl是徒劳的,因此我从wsdl(使用JBossStudio)生成java骨架类,并在实现类中编写我的业务。现在服务很好。无论如何,我不知道为什么当wsdl最初从java类生成时,maxOccurs=“unbounded”不是自动生成的。感谢各位的回复@gkzmin@mhan@dan

薄高懿
2023-03-14

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

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

我如何做到这一点,是得到一个表示我的表/实体的类。然后我有一个包装器类,用于\u实体的列表\u;这是来自web服务的返回。因此,总的来说:

  1. 具有所需注释的实体类
  2. 具有所需注释的包装类
  3. 具有所需注释的Web服务

我已经为下面的每个示例提供了一个可能无法运行的示例。他们只是给你一个理解。

//*********************** 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;
}

}

//************* 2。对象包装器类 ****************

// 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. web服务************

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 web服务本地接口********

import javax.ejb.Local;

@Local
public interface ServiceLocal {
    ExampleList listAll();
}

//**************************

甄煜
2023-03-14

我认为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。

 类似资料:
  • 作为spring boot和microservices架构的新手,尝试使用spring boot cloud starter构建一个简单的应用程序。 我使用了来外部化配置文件(. Properties) 我这里有一个spring云配置,它带来了。来自git存储库和另一个微服务(产品微服务)的属性文件试图从spring云配置服务器访问其配置。 一切正常,除了属性抛出(在第二个微服务中) 以下是配置文

  • 我以json字符串的形式生成输入数据。 对于主题-myinput 我的班级是这样的: 我得到下面类铸造异常: 线程“countries-streaming-analysis-app-f7f95119-4401-4a6e-8060-5A138FFADB2-StreamThread-1”组织中的异常。阿帕奇。Kafka。溪流。错误。异常:流程中捕获到异常。taskId=0_0,processor=KS

  • 例外情况: java.lang.ClassCastException:com.interconnect.library.gcm.util.checkplayServices(util.java:96),com.interconnect.library.gcm.regiseter.handleRegister(regiseter.java:53),com.interconnect.library.g

  • 问题内容: 试图通过类强制转换异常在这里: 抛出此异常: 我不明白这一点-因为该类是由xjc.bat工具生成的-并且它生成的类我完全没有改变-所以这里应该没有强制转换问题- 解组员应该真的给了我一个类可以强制转换为FooClass。 关于我在做什么错的任何想法吗? 问题答案: 是否有注释?如果没有,请尝试: 这基于非官方的JAXB指南。

  • 我试图从java 9上的jListBox中删除一个项目,却一直得到一个异常提示 当我调用DefaultListModel=(DefaultListModel)list2.getModel(); 程序。。 例外在这里出现 …

  • 问题内容: 是否可以 在调试时 强制抛出异常。 让我举一个例子:我正在调试一些代码,这些代码是我从互联网上下载页面的地方。当Internet连接断开或网卡断开时,应抛出IOException,但执行会被阻止(在第二行) 我需要一种在代码阻塞时 在调试 时强制引发异常的方法,以便可以跳转到catch块。 我正在使用netbeans BTW。 While Debugging =执行线程暂停时手动添加。