<div dir="ltr"><div><span style="color:rgb(0,0,0);font-family:Helvetica,arial,freesans,clean,sans-serif;font-size:18px">JSON Responses for Error Routes</span><br></div><div><p style="margin:0px 0px 15px;color:rgb(0,0,0);font-family:Helvetica,arial,freesans,clean,sans-serif;font-size:14px;line-height:22px">
We recently added support for routes to be able to produce responses, other than original MVC forwarding to a view. This addition meant that a route could specify the media types it produces, and a client could specify the desired media types using the HTTP Accept header, and get back a response body with that type.<br>
But this only worked for normal routes and not for error routes which is what <a href="https://issues.jboss.org/browse/AEROGEAR-515" style="color:rgb(65,131,196);text-decoration:initial">AEROGEAR-515</a> has added.</p><p style="margin:15px 0px;color:rgb(0,0,0);font-family:Helvetica,arial,freesans,clean,sans-serif;font-size:14px;line-height:22px">
You can now specify that your error routes produce different types:</p><div class="" style="border:none;padding:0px;color:rgb(0,0,0);font-family:Helvetica,arial,freesans,clean,sans-serif;font-size:14px;line-height:22px"><pre style="font-size:13px;line-height:19px;font-family:Consolas,&#39;Liberation Mono&#39;,Courier,monospace;word-wrap:break-word;margin-top:15px;margin-bottom:15px;background-color:rgb(248,248,248);border:1px solid rgb(204,204,204);overflow:auto;padding:6px 10px;border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px">
<span class="">route</span><span class="" style="font-weight:bold">()</span>
      <span class="" style="font-weight:bold">.</span><span class="" style="color:rgb(0,128,128)">on</span><span class="" style="font-weight:bold">(</span><span class="">Exception</span><span class="" style="font-weight:bold">.</span><span class="" style="color:rgb(0,128,128)">class</span><span class="" style="font-weight:bold">)</span>
      <span class="" style="font-weight:bold">.</span><span class="" style="color:rgb(0,128,128)">produces</span><span class="" style="font-weight:bold">(</span><span class="" style="color:rgb(221,17,68)">&quot;text/html&quot;</span><span class="" style="font-weight:bold">,</span> <span class="" style="color:rgb(221,17,68)">&quot;application/json&quot;</span><span class="" style="font-weight:bold">)</span>
      <span class="" style="font-weight:bold">.</span><span class="" style="color:rgb(0,128,128)">to</span><span class="" style="font-weight:bold">(</span><span class="">Error</span><span class="" style="font-weight:bold">.</span><span class="" style="color:rgb(0,128,128)">class</span><span class="" style="font-weight:bold">).</span><span class="" style="color:rgb(0,128,128)">index</span><span class="" style="font-weight:bold">(</span><span class="">param</span><span class="" style="font-weight:bold">(</span><span class="">Exception</span><span class="" style="font-weight:bold">.</span><span class="" style="color:rgb(0,128,128)">class</span><span class="" style="font-weight:bold">));</span>

<span class="">route</span><span class="" style="font-weight:bold">()</span>
      <span class="" style="font-weight:bold">.</span><span class="" style="color:rgb(0,128,128)">from</span><span class="" style="font-weight:bold">(</span><span class="" style="color:rgb(221,17,68)">&quot;/throwException&quot;</span><span class="" style="font-weight:bold">)</span>
      <span class="" style="font-weight:bold">.</span><span class="" style="color:rgb(0,128,128)">on</span><span class="" style="font-weight:bold">(</span><span class="">RequestMethod</span><span class="" style="font-weight:bold">.</span><span class="" style="color:rgb(0,128,128)">GET</span><span class="" style="font-weight:bold">)</span>
      <span class="" style="font-weight:bold">.</span><span class="" style="color:rgb(0,128,128)">produces</span><span class="" style="font-weight:bold">(</span><span class="" style="color:rgb(221,17,68)">&quot;text/html&quot;</span><span class="" style="font-weight:bold">,</span> <span class="" style="color:rgb(221,17,68)">&quot;application/json&quot;</span><span class="" style="font-weight:bold">)</span>
      <span class="" style="font-weight:bold">.</span><span class="" style="color:rgb(0,128,128)">to</span><span class="" style="font-weight:bold">(</span><span class="">Error</span><span class="" style="font-weight:bold">.</span><span class="" style="color:rgb(0,128,128)">class</span><span class="" style="font-weight:bold">).</span><span class="" style="color:rgb(0,128,128)">throwException</span><span class="" style="font-weight:bold">();</span>
</pre></div><p style="margin:15px 0px;color:rgb(0,0,0);font-family:Helvetica,arial,freesans,clean,sans-serif;font-size:14px;line-height:22px">This would enable you to produce a html response by accessing <a href="http://localhost:8080/aerogear-controller-demo/throwException" style="color:rgb(65,131,196);text-decoration:initial">throwException</a> in aerogear-controller-demo.</p>
<p style="margin:15px 0px;color:rgb(0,0,0);font-family:Helvetica,arial,freesans,clean,sans-serif;font-size:14px;line-height:22px">You can also specify that you want the response as <em>application/json</em> and calling the same route as above:</p>
<div class="" style="border:none;padding:0px;color:rgb(0,0,0);font-family:Helvetica,arial,freesans,clean,sans-serif;font-size:14px;line-height:22px;margin-bottom:0px!important"><pre style="font-size:13px;line-height:19px;font-family:Consolas,&#39;Liberation Mono&#39;,Courier,monospace;word-wrap:break-word;margin-top:15px;margin-bottom:15px;background-color:rgb(248,248,248);border:1px solid rgb(204,204,204);overflow:auto;padding:6px 10px;border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px">
curl -i -H <span class="" style="color:rgb(221,17,68)">&quot;Accept: application/json&quot;</span> -X GET  <span class="" style="color:rgb(221,17,68)">&quot;<a href="http://localhost:8080/aerogear-controller-demo/throwException">http://localhost:8080/aerogear-controller-demo/throwException</a>&quot;</span>

GET Exception application/json:
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Content-Type: application/json;charset<span class="" style="font-weight:bold">=</span>UTF-8
Content-Length: 28
Date: Thu, 03 Jan 2013 06:52:56 GMT

<span class="" style="color:rgb(221,17,68)">&quot;{exception:Demo Exception}&quot;</span></pre></div></div></div>