There is no time to clean up, first give the data, how to implement a custom exception handling framework, the purpose is to throw out the definition exception in the rest request, and return the response of the abnormal JSON data.
has been using the predecessor in the Apache CXF framework encapsulated code to write the rest API, it is time to delve into the exception handling framework, and so the cottage of a spring MVC exception handling framework, feel a little bit of meaning, know a little bit of fur.
1 Customizing the Exceptionmapper of the JSR 311 (Restful) in CXF
http://blog.163.com/java_star/blog/static/117714805201201333531865/
2 Spring MVC Exception Unified processing (including demo source)
http://gaojiewyh.iteye.com/blog/1297746
Note: In this demo, if you want to return the JSON-formatted exception, you need to introduce the Jsonobject class, plus in the Controller
@ResponseBody
else {//JSON format returns
response.setstatus (401);
try {
Jsonobject jso = new Jsonobject ();
try {
jso.put ("Error", Ex.getmessage ());
\ catch (jsonexception e) {
//TODO auto-generated catch
block E.printstacktrace ();
}
PrintWriter writer = Response.getwriter ();
Writer.write (Jso.tostring ());
Writer.flush ();
} catch (IOException e) {
e.printstacktrace ();
}
}
Look at the picture.
Add: spring3.0 Exception handling Advanced
http://blog.csdn.net/sinlff/article/details/5872724