【问题标题】:How can i get the xml response ( CXFRs webservice)?如何获得 xml 响应(CXFRs 网络服务)?
【发布时间】:2015-08-28 09:28:23
【问题描述】:

我从 uri 中获取 id,并将其作为条件放在我的 sql 请求中 我把结果转换成 XML 格式后

这是函数:

package com.mycompany.camel.blueprint;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
public class Testws {
    @GET
    @Path("/test/{id}")
    @Produces(MediaType.APPLICATION_XML)
    public Integer getAssets(@PathParam("id") int id){

        return id;
    }
  }

这是路线:

<camelContext id="camel"    xmlns="http://camel.apache.org/schema/blueprint">
     <route>
       <from uri="cxfrs://bean://rsServer"/>
       <log message="${body}"/>
       <convertBodyTo type="java.lang.Integer"/>
       <to uri="sql:select * from customers where id=:#${body}?exchangePattern=InOut&amp;dataSource=moodleDB"/>
            </route>    </camelContext>

之后的错误:http://localhost:5070/route/test/1

This page contains the following errors:

error on line 1 at column 1: Document is empty
Below is a rendering of the page up to the first error.

如何获取 xml 文档?谢谢?

【问题讨论】:

  • 您使用的是什么版本的 apache-camel?你确定这个错误不是因为浏览器试图将响应解释为 XML 并且是无效的 XML?
  • 我正在使用 Jboss Fuse ESB

标签: xml web-services rest apache-camel cxfrs


【解决方案1】:

要使用 cxfrs 从骆驼路线返回 xml,您需要做一些事情:

  • 在类上使用 @XmlRootElement 注释的客户对象和其他 xml 注释告诉 jaxb 如何编组它
  • 在 cxf 服务器中定义的 xml 提供程序如下:

    <cxf:rsServer id="rsServer" address="${url}">
        <cxf:serviceBeans>
            <bean class="com.mycompany.camel.blueprint.Testws"/>
        </cxf:serviceBeans>
        <cxf:providers>
            <bean class="com.fasterxml.jackson.jaxrs.xml.JacksonJaxbJsonProvider"/>
        </cxf:providers>
    </cxf:rsServer>
    

您需要将来自 SQL 调用的响应处理为客户对象列表,然后 cxf 可以使用此提供程序将其编组为 xml。

您将需要对 jackson-jaxrs-xml-provider 的依赖才能使用它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-23
    • 2019-09-11
    • 2016-11-01
    • 1970-01-01
    相关资源
    最近更新 更多