Abhishek Chatterjee
Abhishek Chatterjee

Reputation: 2049

How to set path variables in camel-http4

I have a camel-spring-boot project where I load the destination url from a yml file with Spring's @ConfigurationProperties. As my destination is a HTTP url I am using camel-http4 component.

Now my URL is https://example.com/students/{id}/subject/{name}, which means I have to pass id and name parameter as path variables (not query parameter). My question is how can I pass these parameters? [Note: I can not put the URL in DSL or XML, it must be there in application.yml]

However, as a solution

//in some processor before toD()
headers.put("id", id);
headers.put("name", name);

//in yml
destination: https4://example.com/students/${header.id}/subject/${header.name}

But while loading this property from yml, Spring tries to evaluate ${header.id} as Spel expression (and throwing error that it could not find it) where as I mentioned it as Camel's simple expression. This same expression works with toD() if I use DSL, but not from yml.

Please let me know, if my approach is proper or not? If it is the way, then how can I get rid of this problem. Thanks in advance.

Upvotes: 0

Views: 3008

Answers (2)

If I'm not mistaken, we should take care of using dynamic routing due to the cache size.

A cleaner solution might be:

YAML file:

cfg:
  target:
    url: 'https4://example.com'

Java DSL:

Expression dynamicPathExpression = constant("students/")
    .append(header("id"))
    .append(constant("/subject/"))
    .append(header("name"));

from("direct://whatever")
  .setHeader(Exchange.HTTP_PATH, dynamicPathExpression)
  .to("{{cfg.target.url}}");

Would that help you out?

Upvotes: 1

Abhishek Chatterjee
Abhishek Chatterjee

Reputation: 2049

I got the answer of second question that is how to distinguise simple expression from Spel

destination: https4://example.com/students/$simple{header.id}/subject/$simple{header.name}

$simple{exp} is another way of ${exp}

But my first question still remains, is it the recommended approach to invoke http endpoint with path variables?

Upvotes: 0

Related Questions