Skip to content

Instantly share code, notes, and snippets.

@danbev
Last active December 10, 2015 13:29
Show Gist options
  • Save danbev/4441337 to your computer and use it in GitHub Desktop.
Save danbev/4441337 to your computer and use it in GitHub Desktop.
JSON Response for Error Routes

JSON Responses for Error Routes

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.
But this only worked for normal routes and not for error routes which is what AEROGEAR-515 has added.

You can now specify that your error routes produce different types:

route()
      .on(Exception.class)
      .produces("text/html", "application/json")
      .to(Error.class).index(param(Exception.class));
      
route()
      .from("/throwException")
      .on(RequestMethod.GET)
      .produces("text/html", "application/json")
      .to(Error.class).throwException();

This would enable you to produce a html response by accessing throwException in aerogear-controller-demo.

You can also specify that you want the response as application/json and calling the same route as above:

curl -i -H "Accept: application/json" -X GET  "http://localhost:8080/aerogear-controller-demo/throwException"

GET Exception application/json:
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Content-Type: application/json;charset=UTF-8
Content-Length: 28
Date: Thu, 03 Jan 2013 06:52:56 GMT

"{exception:Demo Exception}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment