iteye_9831 2013-04-11 17:05 采纳率: 100%
浏览 481
已采纳

camel中发布webservice的问题

camel自带的例子:camel-example-cxf-tomcat

运行OK。

但是把WebService接口方法的返回值由对象改为List,
当然,实现中也调整为返回List。

但执行错误:
Exception in thread "main" javax.xml.ws.soap.SOAPFaultException: org.apache.camel.example.cxf.incident.OutputReportIncident cannot be cast to java.util.List

同样的,含有List返回值的WebService,如果使用CXF发布和实现,执行正常。
<jaxws:endpoint id="foo" implementorClass="my.FooImpl" address="/foo" />

但是调整为camel发布的方式:
<bean id="fooImpl" class="my.FooImpl"/>
<camelContext xmlns="http://camel.apache.org/schema/spring">
  <route>
    <from uri="cxf:/foo?serviceClass=my.IFoo"/>
    <to uri="bean:fooImpl"/>
  </route>
</camelContext>

就会出现cannot be cast to java.util.List的错误。
如果把List返回方式改为普通对象,就执行正常。

有人帮看一下吗?
多谢!
  • 写回答

3条回答 默认 最新

  • Dead_Knight 2013-04-15 13:13
    关注

    我试了下,你这个错误不是camel,也不是cxf的,而是jaxb的错误。jaxb解析的时候,是需要被解析对象添加了jaxb的注解:(@XmlRootElement)
    如果你想返回List,可以把list的值放到对象中,然后通过对象获取list列表。
    这是我的测试代码,如下:
    [code="java"]
    OutputReportIncident output = new OutputReportIncident();
    output.setCode("OK;");
    List result = new ArrayList();
    result.add(output);

        JAXBContext context;
        try {
            context = JAXBContext.newInstance(ArrayList.class);
            Marshaller marshaller = context.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_ENCODING, "GBK");
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
            marshaller.marshal(result, new File("d:/test.xml"));
        } catch (JAXBException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    

    [/code]
    这样的代码运行错误信息为:
    unable to marshal type "java.util.ArrayList" as an element because it is missing an @XmlRootElement annotation]

    而cxf是封装了这样的错误

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站