Camel中的REST API - POST不工作(XML解析错误)

Camel中的REST API - POST不工作(XML解析错误)

问题描述:

我试图创建一个示例Camel路由,作为构建在简单资源类上的RESTful API的种子,提供XML有效载荷。Camel中的REST API - POST不工作(XML解析错误)

我有问题是我的GET工作(它所做的是建立了一块XML),但我的帖子还是返回了以下错误: -

JAXBException occurred : ParseError at [row,col]:[1,1] 
Message: Premature end of file.. ParseError at [row,col]:[1,1] 
Message: Premature end of file.. 

我使用的是类,由XSD通过xjc构建,以定义XML。我知道这不是XML有效负载结构的问题,因为当我将GET返回的XML复制到POST时,它甚至会失败!但是,这就是说,鉴于JAXB正在抱怨第一个字符,我不知道它是否抱怨编码。我使用cURL和Chrome邮差作为客户端,都收到相同的回应。

我想我只是缺少一个简单的注释或设置,使POST方法(newCustomer)能够解析传入的XML负载。

这里是我的路线XML: -

<?xml version="1.0" encoding="UTF-8"?> 
<blueprint 
    xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:jaxrs="http://cxf.apache.org/blueprint/jaxrs" 
    xmlns:cxf="http://cxf.apache.org/blueprint/core" 
    xmlns:camel="http://camel.apache.org/schema/blueprint" 
    xsi:schemaLocation=" 
http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd 
http://cxf.apache.org/blueprint/jaxrs http://cxf.apache.org/schemas/blueprint/jaxrs.xsd 
http://cxf.apache.org/blueprint/core http://cxf.apache.org/schemas/blueprint/core.xsd 
http://camel.apache.org/schema/blueprint http://camel.apache.org/schema/blueprint/camel-blueprint.xsd 
"> 

    <camelContext id="goochjs-cameltest-customer" trace="false" xmlns="http://camel.apache.org/schema/blueprint"> 

     <route id="jetty"> 
      <from uri="jetty:http://0.0.0.0:8892/rest?matchOnUriPrefix=true" /> 
      <log logName="goochjs" loggingLevel="INFO" message="Request received 1: ${body}" /> 
      <to uri="cxfbean:customerResource" /> 
     </route> 
    </camelContext> 

    <bean id="customerResource" class="org.goochjs.cameltest.CustomerResourceImpl" /> 
</blueprint> 

...和我的资源类...

package org.goochjs.cameltest; 

import javax.ws.rs.Consumes; 
import javax.ws.rs.DefaultValue; 
import javax.ws.rs.GET; 
import javax.ws.rs.POST; 
import javax.ws.rs.Path; 
import javax.ws.rs.PathParam; 
import javax.ws.rs.Produces; 
import javax.ws.rs.QueryParam; 
import javax.ws.rs.core.MediaType; 
import javax.ws.rs.core.Response; 

@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON }) 
@Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON }) 
@Path("/customers") 
public class CustomerResourceImpl { 

    @POST 
    @Path("/{type}") 
    public Response newCustomer(Customer customer, @PathParam("type") String type, @QueryParam("active") @DefaultValue("true") boolean active) { 
     return Response.ok(type).build(); 
    } 

    @GET 
    @Path("/{type}") 
    public Response getCustomer(@PathParam("type") String type) { 
     Customer output = new Customer(); 
     output.setId(987654); 
     output.setName("Willy Wonka"); 

     return Response.ok(output).build(); 
    } 
} 

最后,这里是我的示例XML: -

<?xml version="1.0" encoding="UTF-8"?> 
<Customer> 
    <name>Willy Wonka</name> 
    <id>987654</id> 
</Customer> 

感谢您的指点。如果更容易查看,整个项目打包在my github site

J.

+0

虽然,如果我更改POST方法签名以删除Customer对象,它将起作用。新签名看起来像'public Response newCustomer(@PathParam(“type”)String type,@QueryParam(“active”)@DefaultValue(“true”)boolean active)''。这似乎将我指向解析错误。 – 2013-04-23 08:16:44

经过很多小时的头部划伤,我已经解决了这个问题。问题是没有消息体正在呈现给资源类。在将驼峰XML中的路由节点添加streamCache="true"之后,它开始工作。

以下是骆驼网站上的相关页面 - http://camel.apache.org/stream-caching.html

我认为the pattern using cxfrs://作为端点做这隐含。但是,我一直无法在Blueprint XML中使用这个功能,这就是为什么我预先使用Jetty进行部署的原因。

J.