camel-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Claus Ibsen <claus.ib...@gmail.com>
Subject Re: Rest DSL - OutType in case of error message problem
Date Tue, 02 Dec 2014 10:38:58 GMT
Hi

Yeah well spotted. We should improve rest-dsl to support these
use-cases where you prepare a response message yourself and set the
http status code to some error code etc.

I guess if the status code is >= 300+ then we should assume its an
error, and use the message body as-is as the response.

On Mon, Dec 1, 2014 at 2:25 PM, panzerhans <espen+camel@tjonneland.no> wrote:
> I have a setup with Swagger and the Rest DSL. Swagger uses the
> OutType(MyType.class) to annotate the API with the return type. However,
> there are times when I wish to return something else to the caller, e.g. an
> error message with a HTTP response code.
>
> Take the following Route as an example:
>
> RestDefinition privateAPI =
> rest(ServiceConstants.PRIVATE_API_VERSION_1).description("Private (internal)
> REST services for MyServices.");
>
> privateAPI.get("/fetchsomething").description("Lists stuff from some
> service")
>             .outTypeList(MyPojoForSomething.class)
>             .route().routeId("A descriptive ID")
>             .choice()
>
> .when(header(ServiceConstants.REQUEST_VALIDATED).isEqualTo("TRUE"))
>                    .bean(GoAheadAndFillAMyPojoForSomething-into-body.class)
>                 .otherwise().bean(ErrorMessageConstructor.class);
>
> -------
>
> * An Interceptor validates the incoming message and sets the Header constant
> to TRUE or FALSE.
> * The ErrorMessageConstructor constructs a proper message and puts it as a
> String into the body together with a correct HTTP code (E.g. Not-Found).
>
> Problem: The marshaller, uses outTypeList to determine the way to convert
> the Message body to Json. If in the above example, the request was "FALSE",
> the marshaller (I am using Jackson) tries to marshall a String to a
> MyPojoForSomething.class.
>
> The content of the String I have constructed for the error is in this case:
> "Missing properties in HTTP Header."
>
> This results in an error:
> com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'Missing':
> was expecting ('true', 'false' or 'null')
>  at [Source: java.io.ByteArrayInputStream@67618968; line: 1, column: 9]
>         at
> com.fasterxml.jackson.core.JsonParser._constructError(JsonParser.java:1419)
>         ........
>
>
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/Rest-DSL-OutType-in-case-of-error-message-problem-tp5759864.html
> Sent from the Camel - Users mailing list archive at Nabble.com.



-- 
Claus Ibsen
-----------------
Red Hat, Inc.
Email: cibsen@redhat.com
Twitter: davsclaus
Blog: http://davsclaus.com
Author of Camel in Action: http://www.manning.com/ibsen
hawtio: http://hawt.io/
fabric8: http://fabric8.io/

Mime
View raw message