Route:
route()
.from("/cars")
.on(RequestMethod.GET)
.produces(MediaType.JSON)
.paged().offset()
.to(Cars.class).findCarsBy(param("offset"), param("color"), param("limit", "10"));
Call:
curl -i --header "Accept: application/json" "http://controllerdemo-danbev.rhcloud.com/aerogear-controller-demo/cars?color=red&limit=5"
Response:
HTTP/1.1 400 Bad Request
Content-Type: application/json;charset=UTF-8
Content-Length: 77
Connection: close
{"error":"AG_CONTROLLER000010: Parameter: 'offset' was missing from Request"}
Route:
route()
.from("/cars")
.on(RequestMethod.GET)
.produces(MediaType.JSON)
.paged().offset()
.to(Cars.class).findCarsBy(param("offsset", "0"), param("color"), param("limit", "10"));
Notice that offset on the route was missspelled. Using paged().offset()
is using the default values for the offset
and limit
parameter names.
These can be changed by using offsetParamName
or limitParamName
to avoid this error but is useful for troubleshooting during development.
Call:
curl -i --header "Accept: application/json" "http://controllerdemo-danbev.rhcloud.com/aerogear-controller-demo/cars?offset=0&color=red&limit=5"
Response:
HTTP/1.1 500 Internal Server Error
Content-Type: application/json;charset=UTF-8
Content-Length: 174
Connection: close
{"error":"AG_CONTROLLER000014: The paging parameter 'offset' was not found in the request. The route is expecting the following arguments: '{offsset=0, color=red, limit=5}'"}
Route:
route()
.from("/cars")
.on(RequestMethod.GET)
.produces(MediaType.JSON, CustomMediaTypeResponder.MEDIA_TYPE)
.paged().offset("myoffset").limitParamName("mylimit")
.to(Cars.class).findCarsBy(param("offset", "0"), param("color"), param("limit", "10"));
Call:
curl -i --header "Accept: application/json" "http://controllerdemo-danbev.rhcloud.com/aerogear-controller-demo/cars?offset=0&color=red&limit=5"
Response:
HTTP/1.1 400 Bad Request
Content-Type: application/json;charset=UTF-8
Content-Length: 82
Connection: close
{"error":"AG_CONTROLLER000015: Paging parameter 'myoffset' missing from request."}
The error message and response status code can be controlled by the use of Error Routes. For example, the examples above use the following error routes:
route()
.on(PagingRequestException.class)
.produces(MediaType.JSON)
.to(Error.class).handlePagingRequestException(param(PagingRequestException.class));
route()
.on(PagingRouteException.class)
.produces(MediaType.JSON)
.to(Error.class).handlePagingRouteException(param(PagingRouteException.class));
route()
.on(MissingRequestParameterException.class)
.produces(MediaType.JSON)
.to(Error.class).handleMissingRequestParameter(param(MissingRequestParameterException.class));
And the corresponding method implementations look like this:
public ErrorResponse handlePagingRequestException(final PagingRequestException e) {
return new JsonErrorResponse(HttpServletResponse.SC_BAD_REQUEST)
.message("error", e.getMessage());
}
public ErrorResponse handlePagingRouteException(final PagingRouteException e) {
return new JsonErrorResponse(HttpServletResponse.SC_INTERNAL_SERVER_ERROR)
.message("error", e.getMessage());
}
public ErrorResponse handleMissingRequestParameter(final MissingRequestParameterException e) {
return new JsonErrorResponse(HttpServletResponse.SC_BAD_REQUEST)
.message("error", e.getMessage());
}
You could choose to return a different error message, change the returned response status code as you see fit.