Could this zero-body treatment in Apache Camel be more elegant?

I am new to Camel and try to learn idioms and best practices. I am writing web services that should handle several different errors. Here is my error handling and routing:

onException(JsonParseException.class).inOut("direct:syntaxError").handled(true);
onException(UnrecognizedPropertyException.class).inOut("direct:syntaxError").handled(true);

// Route service through direct to allow testing.
from("servlet:///service?matchOnUriPrefix=true").inOut("direct:service");
from("direct:service")
    .choice()
        .when(body().isEqualTo(null))
            .inOut("direct:syntaxError")
        .otherwise()
            .unmarshal().json(lJsonLib, AuthorizationParameters.class).inOut("bean:mybean?method=serviceMethod").marshal().json(lJsonLib);

As you can see, I have special processing (content based routing) for processing a request with a zero body. Is there a way to handle this more elegantly? I write several services of this type, and it seems that they can be much cleaner.

+3
source share
3 answers

You can use an interceptor such as interceptFrom with when to check for an empty pool, as there is an example here: http://camel.apache.org/intercept

stop, :

interceptFrom("servlet*").when(body().isNull()).to("direct:syntaxError").stop();
+5

body().isNull() null Dead Letter Channel :). , , DLC, - , .

choice().
   when(body().isNull()).to("jms:deadLetterChannel").
   otherwise().to("jms:regularProcessing").
endChoice();
+5

You can use a bean that checks for null and throws an exception in case of null. This way you can handle this case when handling exceptions.

0
source

All Articles