Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Optional null with Apache Camel to get header parameters

Good morning.

Currently I am developing an API in Apache Camel in which I have to use queryParameters for the query and some parameters may be mandatory and others not, so I need to handle in case I don’t send the parameter have some way to handle the nullPointerException, I was using Optional However, I don’t know if I’m using it incorrectly, but I’m not getting the error:

Currently I am using the following line that in theory if it receives a null value, it would return an empty optional, however, I get an error:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Optional<String> optionalServices= Optional.ofNullable(exchange.getIn().getHeader("services").toString());

Exception

Stacktrace
---------------------------------------------------------------------------------------------------------------------------------------: java.lang.NullPointerException: Cannot invoke "Object.toString()" because the return value of "org.apache.camel.Message.getHeader(String)" is null
    at org.tmve.customer.ms.processor.UserProfileProcessorQueryParamReq.process(UserProfileProcessorQueryParamReq.java:35)
    at org.apache.camel.support.processor.DelegateSyncProcessor.process(DelegateSyncProcessor.java:65)
    at org.apache.camel.impl.engine.CamelInternalProcessor.process(CamelInternalProcessor.java:392)
    at org.apache.camel.processor.Pipeline$PipelineTask.run(Pipeline.java:104)
    at org.apache.camel.impl.engine.DefaultReactiveExecutor$Worker.schedule(DefaultReactiveExecutor.java:181)
    at org.apache.camel.impl.engine.DefaultReactiveExecutor.scheduleMain(DefaultReactiveExecutor.java:59)
    at org.apache.camel.processor.Pipeline.process(Pipeline.java:165)
    at org.apache.camel.impl.engine.CamelInternalProcessor.process(CamelInternalProcessor.java:392)
    at org.apache.camel.component.platform.http.vertx.VertxPlatformHttpConsumer.lambda$handleRequest$2(VertxPlatformHttpConsumer.java:201)
    at io.vertx.core.impl.ContextBase.lambda$null$0(ContextBase.java:137)
    at io.vertx.core.impl.ContextInternal.dispatch(ContextInternal.java:264)
    at io.vertx.core.impl.ContextBase.lambda$executeBlocking$1(ContextBase.java:135)
    at org.jboss.threads.ContextHandler$1.runWith(ContextHandler.java:18)
    at org.jboss.threads.EnhancedQueueExecutor$Task.run(EnhancedQueueExecutor.java:2449)
    at org.jboss.threads.EnhancedQueueExecutor$ThreadBody.run(EnhancedQueueExecutor.java:1478)
    at org.jboss.threads.DelegatingRunnable.run(DelegatingRunnable.java:29)
    at org.jboss.threads.ThreadLocalResettingRunnable.run(ThreadLocalResettingRunnable.java:29)
    at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
    at java.base/java.lang.Thread.run(Thread.java:840)

This is how I am testing from postman when it does not send the value:

enter image description here

In this case, how can I handle the value of the Query Parameters if it is not sent?

>Solution :

According to the error log, it is , Message.getHeader(String) returns null.

Your statement could be corrected as the following.

Optional<String> optionalServices= Optional.ofNullable(exchange.getIn().getHeader("services"))
                .map(Object::toString)

If I explain the above in multiple steps.

Optional<Object> optObj = Optional.ofNullable(exchange.getIn().getHeader("services")); 
Optional<String> optStr = optObj.map(obj -> obj.toString());

So, the function (obj -> obj.toString()) within Optional.map() is called only when Optional has a value.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading