Search code examples
xmlcxfapache-camelcxfrs

REST API in Camel - POST not working (XML parsing error)


I'm trying to create a sample Camel route, as the seed for a RESTful API built on a simple resource class, providing XML payloads.

The issue I've got is that my GET is working (all it does is to build a piece of XML) but my POST is returning the following error:-

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

I'm using a class, built from an XSD via xjc, to define the XML. I know it's not an issue with the XML payload structure as it even fails when I copy the XML returned by the GET into the POST! That said, however, given that JAXB is complaining about the very first character, I wonder if it is complaining about the encoding. I've used both cURL and Chrome Postman as clients, both receiving the same response.

I think I'm just missing a simple annotation or setting that enables the POST method (newCustomer) to parse the incoming XML payload.

Here's my route 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>

...and my resource class...

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();
    }
}

And, finally, here's my sample XML:-

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

Thanks for any pointers. If it's easier to review, the whole project is packaged up on my github site.

J.


Solution

  • After many hours of head-scratching, I've solved this. The problem was that no message body was being presented to the resource class. After I added streamCache="true" to the route node within the Camel XML, it started working.

    Here's the relevant page on the Camel site - http://camel.apache.org/stream-caching.html.

    I think that the pattern using cxfrs:// as a from endpoint does this implicitly. However, I've been unable to get this to work within Blueprint XML, which was why I deployed with Jetty up-front.

    J.