package com.aemreunal.controller;

/*
 ***************************
 * Copyright (c) 2014      *
 *                         *
 * This code belongs to:   *
 *                         *
 * @author Ahmet Emre Ünal *
 * S001974                 *
 *                         *
 * aemreunal@gmail.com     *
 * emre.unal@ozu.edu.tr    *
 *                         *
 * aemreunal.com           *
 ***************************
 */

import net.minidev.json.JSONObject;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import com.aemreunal.exception.MalformedRequestException;
import com.aemreunal.helper.JsonBuilder;

@ControllerAdvice
public class GeneralControllerAdvice {
    @ExceptionHandler(MalformedRequestException.class)
    public ResponseEntity<JSONObject> malformedRequestExceptionHandler(MalformedRequestException ex) {
        JSONObject responseBody = new JsonBuilder().add("reason", "request")
                                                   .add("error", ex.getLocalizedMessage())
                                                   .build();
        return new ResponseEntity<JSONObject>(responseBody, HttpStatus.BAD_REQUEST);
    }
}

Add exception handler