You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by Gary Kennedy <ga...@apnic.net> on 2014/11/05 07:02:09 UTC
Setting rest response json directly
Greetings all,
I'm using the camel rest dsl in a karaf osgi blueprint to define my route (and I love it, so easy), and I've got an onException block properly handling my (known) exceptions for an 'application/json' POST endpoint.
I've set it to produce 'application/json' and when things go well it works fine.
However, on the exception case I want to respond with a json body containing the exception message.
e.g., { "error": "${exception.message}" }
When I use setBody to do the above, the response is a json string of the above instead of the above as json. (Does that make sense??)
So how can I set a json response body from a simple (dsl) string?
Cheers,
Gary
Re: Setting rest response json directly
Posted by Claus Ibsen <cl...@gmail.com>.
Hi
>From Camel 2.14.1 onwards we have made this easier with
https://issues.apache.org/jira/browse/CAMEL-8104
So if you have a custom error message you just set the HTTP error code
to a value 300+ and rest-dsl will not do output binding. There is a
new option that is turned on by default, you can turn it off to use
the old behavior.
And the rest-dsl documentation has a little example in the bottom
https://cwiki.apache.org/confluence/display/CAMEL/Rest+DSL
On Sun, Nov 9, 2014 at 11:24 PM, Gary Kennedy <ga...@apnic.net> wrote:
> I changed the way I used the servlet/rest component and it works for me now.
>
> I'm still using the same bean setup as before:
> <reference id="httpService"
> interface="org.osgi.service.http.HttpService"/>
> <bean id="camelServlet" class="...CamelHttpTransportServlet"/>
> <bean id="registration" class="...OsgiServletRegisterer"
> init-method="register" destory-method="unregister">
> <property name="alias" value="/example"/>
> <property name="servletName" value="MyServlet"/>
> <property name="servlet" ref="camelServlet"/>
> <property name="httpService" ref="httpService"/>
> </bean>
>
> But now I've dropped the rest part, and using servlet endpoints to start the
> routes:
> <camelContext>
> <dataFormats>
> <json id="json" .../>
> </dataFormats>
> <route>
> <from uri="servlet:///route?servletName=MyServlet"/>
> <unmarshal ref="json"/>
> < !-- lots of magic -->
> <onException>
> <exception>net.me.MyException</exception>
> <handled><constant>true</constant></handled>
> < !-- more magic -->
> <setHeader headerName="Content-Type">
> <constant>application/json</constant>
> </setHeader>
> <setBody>
> <simple>{ "error": "${exception.message}" }</simple>
> </setBody>
> </onException>
> </route>
> </camelContext>
>
> The bonus to using this method is that the unmarshalling is covered by the
> in-route onException block as well.
>
> The downside is that now I have to work on the http method filtering (ie,
> making the route POST/GET specific). It might be easier to play with the
> consumes/produces part of the rest dsl to bring the above theory used above
> into the rest dsl. Anyways,
>
> Cheers,
> Gary
>
>
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/Setting-rest-response-json-directly-tp5758518p5758801.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/
Re: Setting rest response json directly
Posted by Gary Kennedy <ga...@apnic.net>.
I changed the way I used the servlet/rest component and it works for me now.
I'm still using the same bean setup as before:
<reference id="httpService"
interface="org.osgi.service.http.HttpService"/>
<bean id="camelServlet" class="...CamelHttpTransportServlet"/>
<bean id="registration" class="...OsgiServletRegisterer"
init-method="register" destory-method="unregister">
<property name="alias" value="/example"/>
<property name="servletName" value="MyServlet"/>
<property name="servlet" ref="camelServlet"/>
<property name="httpService" ref="httpService"/>
</bean>
But now I've dropped the rest part, and using servlet endpoints to start the
routes:
<camelContext>
<dataFormats>
<json id="json" .../>
</dataFormats>
<route>
<from uri="servlet:///route?servletName=MyServlet"/>
<unmarshal ref="json"/>
< !-- lots of magic -->
<onException>
<exception>net.me.MyException</exception>
<handled><constant>true</constant></handled>
< !-- more magic -->
<setHeader headerName="Content-Type">
<constant>application/json</constant>
</setHeader>
<setBody>
<simple>{ "error": "${exception.message}" }</simple>
</setBody>
</onException>
</route>
</camelContext>
The bonus to using this method is that the unmarshalling is covered by the
in-route onException block as well.
The downside is that now I have to work on the http method filtering (ie,
making the route POST/GET specific). It might be easier to play with the
consumes/produces part of the rest dsl to bring the above theory used above
into the rest dsl. Anyways,
Cheers,
Gary
--
View this message in context: http://camel.465427.n5.nabble.com/Setting-rest-response-json-directly-tp5758518p5758801.html
Sent from the Camel - Users mailing list archive at Nabble.com.
Re: Setting rest response json directly
Posted by Gary Kennedy <ga...@apnic.net>.
I'm using blueprint, but yes, it does not work.
<cml:setBody>
<cml:simple>{ "exception": "${exception.message}" }</cml:simple>
</cml:setBody>
Produces response content of:
"{ \"error\": \"${exception.message}\" }"
Instead of:
{ "error": "${exception.message}" }
I'm using the servlet/rest config, ie:
<camelContext>
<restConfiguration component="servlet" bindingMode="json"/>
<rest consumes="application/json" produces="application/json">
<post>
<route>
< !-- lots of magic -->
<onException>
<exception>net.me.MyException</exception>
<handled><constant>true</constant></handled>
< !-- more magic -->
<setBody>
<simple>{ "error": "${exception.message}"
}</simple>
</setBody>
</onException>
</route>
</post>
</rest>
</camelContext>
(Sorry. I should have included this snippet in the first email)
Cheers,
Gary
--
View this message in context: http://camel.465427.n5.nabble.com/Setting-rest-response-json-directly-tp5758518p5758563.html
Sent from the Camel - Users mailing list archive at Nabble.com.
Re: Setting rest response json directly
Posted by Claus Ibsen <cl...@gmail.com>.
Hi
And using
.setBody(simple("{ \"error\": \"${exception.message}\" }"))
Does not work?
On Wed, Nov 5, 2014 at 7:02 AM, Gary Kennedy <ga...@apnic.net> wrote:
> Greetings all,
>
> I'm using the camel rest dsl in a karaf osgi blueprint to define my route (and I love it, so easy), and I've got an onException block properly handling my (known) exceptions for an 'application/json' POST endpoint.
>
> I've set it to produce 'application/json' and when things go well it works fine.
>
> However, on the exception case I want to respond with a json body containing the exception message.
> e.g., { "error": "${exception.message}" }
>
> When I use setBody to do the above, the response is a json string of the above instead of the above as json. (Does that make sense??)
>
> So how can I set a json response body from a simple (dsl) string?
>
> Cheers,
> Gary
>
--
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/