Carleto
Carleto

Reputation: 951

Apache Camel Filter bean response via REST DSL

I am currently working on a REST based java application using the new Camel REST DSL as the foundation. I'm trying to filter a result of a list of objects before return the json response for the client.

This is a example code of what i'm trying to do:

.get("/api/list").description("Search all data")
   .to("bean:apiService?method=searchAll")
.route().description("Lets suppose i need to aply a filter in return")
    .to("bean:apiService?method=filter").endRest();

But int the the second bean execution i can't access the object returned of the first bean execution.

class ApiService {

public MyResponseJSON searchAll(MyJsonObjectRequest request) {

    MyResponseJSON jsonReturn = new MyResponseJSON();

    return jsonReturn;
}


public MyResponseJSON filter(Exchange exchange) {
    //i can't do anything here. The message in exchange is empty
} 
}

And the return of rest is empty to the client.

I'm trying to not put the filter inside the method searchAll because i'm using the single responsability principle.

If i remove the .route()....endRest() the response is OK, but not filtered.

This is possible to do using the REST DSL of Apache Camel, and if it's possible, what i'm doing wrong?

Thanks.

Upvotes: 0

Views: 909

Answers (1)

Claus Ibsen
Claus Ibsen

Reputation: 55540

Only have either a to or route in the rest-dsl, not both, eg do:

.get("/api/list").description("Search all data")
   .route()
     .to("bean:apiService?method=searchAll")
     .to("bean:apiService?method=filter");

Upvotes: 2

Related Questions