2

I'm trying to send message from rest API to new route, and even thou I receive request in JSON format on my REST API and binding is set to JSON, when I forward it to new route it will be shown as InputStream and I will have to marshal it to JSON in order to use it.

I already tried using streamCaching and other components in RestConfiguration (consumes, produces, type, dataType). Also i'm using all dependencies in POM.

public void configure() {
    restConfiguration().component("servlet")
            .bindingMode(RestBindingMode.json)
            .skipBindingOnErrorCode(false);

    rest("/resttest")
       .patch("/t1")
            .id("t1")
            .description("t1")
            .consumes("application/json")
            .produces("application/json")
            .param()
                .name("body")
               .type(RestParamType.body)
               .dataType("json")
                .required(true)
            .endParam()
            .to("direct:test2");

This route is in other class:

 from("direct:test2").id("test2")

            .marshal().json(JsonLibrary.Jackson,SomePOJO.class)
            .unmarshal().json(JsonLibrary.Jackson, SomePOJO.class)
            .choice()
            .when(simple("${body.getStatus()} =~ 'Closed'"))
            .....

I was expecting to get JSON message on test2 route, and somehow I get InputStream, so i have to do marshaling first. Anybody know how I can make REST API forward me to route message in JSON format, not as stream?

nenad92
  • 21
  • 2

2 Answers2

0

Try:

.convertBodyTo(String.class)

before your unmarshal.

Jakmoore
  • 59
  • 8
  • Not sure how that would help? But I tried it and its not working. I know problem is somewhere in REST API, since its not binding received message to JSON, hence message in $body is in InputStream format. – nenad92 Jun 07 '19 at 13:43
0

I was having the same issue, and I had to apply the marshal method for incoming values and unmarshal method for response.

This was the response in postman: enter image description here

And I was using this code:

rest(service.service)
.description(ContentCategory.api("Service Test"))           
.post("/product/{productType}/{enterpriseId}")
.description("service for saving a product")
.consumes(MediaType.APPLICATION_JSON)
.type(Product.class)
.bindingMode(RestBindingMode.json)
.produces(MediaType.APPLICATION_JSON)
.outType(String.class)
.route()
.bean("productService", "saveProduct")
.marshal()
.json(JsonLibrary.Jackson)
.setHeader(Exchange.CONTENT_TYPE, constant(MediaType.APPLICATION_JSON))
.endRest();

I just added the unmarshall method after realizing that I was receiving an InputStream

rest(service.service)
.description(ContentCategory.api("Service Test"))           
.post("/product/{productType}/{enterpriseId}")
.description("service for saving a product")
.consumes(MediaType.APPLICATION_JSON)
.type(Product.class)
.bindingMode(RestBindingMode.json)
.produces(MediaType.APPLICATION_JSON)
.outType(String.class)
.route()
.bean("productService", "saveProduct")
.marshal()
.json(JsonLibrary.Jackson)
.unmarshal()
.json(JsonLibrary.Jackson)
.setHeader(Exchange.CONTENT_TYPE, constant(MediaType.APPLICATION_JSON))
.endRest();

In fact, I just added this two lines

.unmarshal()
.json(JsonLibrary.Jackson)

And it starts working: enter image description here

It think is probably you need to use the unmarshal method for the resttest service.