0

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.

Abhishek Chatterjee
  • 1,962
  • 2
  • 23
  • 31

2 Answers2

1

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?

  • thanks for helping. I admit this is another way of doing it, but I think we are still hardcoding "students" and "/subject/" in java code in this way. – Abhishek Chatterjee Jun 12 '20 at 12:32
  • Nice! But... Aren't they hardcoded in the YAML file example you wrote? You may replace them with their keys or even solve them in the Processor you're setting the values. We actually have many ways of solving this. Hehehe – Fábio Carmo Santos Jun 12 '20 at 18:07
0

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?

Abhishek Chatterjee
  • 1,962
  • 2
  • 23
  • 31