Details
-
Bug
-
Status: Resolved
-
Major
-
Resolution: Invalid
-
2.20.1, 2.20.2
-
None
-
- Windows 10
- JVM 1.8
- IntelliJ IDEA
- Maven 3.3.3
-
Unknown
Description
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.
RestConfiguration:
/** * This configuration is for start the Apache Camel application with the Spring Boot servlet * @author rafael.manzoni */ public class RestConfiguration extends RouteBuilder { @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", "true"); } }
RestMethods:
/** * Rest methods configuration. Apply all HTTP routes to Camel Routes * @author REST Methods Configuration */ public class RestMethods extends RouteBuilder { @Override public void configure() throws Exception { rest() .bindingMode(RestBindingMode.auto) .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.getIn().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:
Success:
curl -X GET \ 'http://localhost:8080/credits/v1/rating?client_cpf_cnpj=11111111111' \ -H 'Accept: application/json' \ -H 'Cache-Control: no-cache' \ -H 'Content-Type: application/json'
Failure:
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´m attaching the project to this issue.