简体   繁体   中英

Camel REST DSL API - Multiple Content Type - XML Error

I am trying to create a Camel REST service via Java DSL that can consume/produce both json and xml and the JSON works but I receive the an error when I try to retrieve the result in XML.

RestMethods Class:

public class RestMethods extends RouteBuilder {

@Override
public void configure() throws Exception {
    rest()
            .bindingMode(RestBindingMode.json_xml)
            .consumes("application/json, application/xml")
            .produces("application/json, application/xml")
            .get("/rating")
            .toD("direct:getRatingByClient");
}

Route Implementation:

public class GetRatingByClientRoute extends RouteBuilder{

// Use to created the mock
private final ObjectMapper objectMapper = new ObjectMapper();

@Override
public void configure() throws Exception {

    from("direct:getRatingByClient")

    // Request
    .to("log:init")

    // Mediation
    .process(exchange -> {

        // TODO Implement route
        TestEntity test = new TestEntity();
        test.setTestAttribute("Teste");
        exchange.getOut().setBody(test);

    // Response
    .to("log:end");

}

}

When I run with Content-Type application/json works like a charm. But, when i run with Content-Type application/xml i got this error: java.io.IOException: org.apache.camel.InvalidPayloadException: No body available of type: javax.xml.bind.JAXBElement but has value: class GetRatingByClientRouteResponse

Follow the requisition that I´m testing: curl -X GET \\ ' http://localhost:8080/credits/v1/rating?client_cpf_cnpj=11111111111 ' \\ -H 'Accept: application/xml' \\ -H 'Cache-Control: no-cache' \\ -H 'Content-Type: application/xml'

I´ve open a bug for camel and the response worked:

eed to use jaxb.index files for your JAXB classes. Or you can turn off the data format to require a JAXBElement by

.dataFormatProperty("mustBeJAXBElement", "false");

This configuration goes on the RestConfiguration DSL, like this:

    @Override
    public void configure() throws Exception {
        restConfiguration()
            .component("servlet")
            .contextPath("/credits/v1")
            .enableCORS(true)
            .apiContextPath("/api-doc")
            .apiProperty("api.title", "REST API")
            .apiProperty("api.version", "v1")
            .apiContextRouteId("doc-api")
            .bindingMode(RestBindingMode.auto)
            .dataFormatProperty("prettyPrint", "true")
            .dataFormatProperty("mustBeJAXBElement", "false");

    }

For more information check this: https://issues.apache.org/jira/browse/CAMEL-12217

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM