diff --git a/.idea/workspace.xml b/.idea/workspace.xml
index 8a740f3..61d31c9 100644
--- a/.idea/workspace.xml
+++ b/.idea/workspace.xml
@@ -57,20 +57,7 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
@@ -136,7 +123,7 @@
-
+
@@ -148,6 +135,16 @@
+
+
+
+
+
+
+
+
+
+
@@ -158,27 +155,9 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
-
@@ -195,8 +174,8 @@
-
+
@@ -423,17 +402,17 @@
-
-
-
+
+
+
@@ -502,16 +481,6 @@
-
-
-
-
-
-
-
-
-
-
@@ -522,106 +491,13 @@
-
+
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
diff --git a/SpringMVC/.idea/.name b/SpringMVC/.idea/.name
index 651d87a..79bd3e8 100644
--- a/SpringMVC/.idea/.name
+++ b/SpringMVC/.idea/.name
@@ -1 +1 @@
-annotationSpike
\ No newline at end of file
+SpringMVC
\ No newline at end of file
diff --git a/SpringMVC/.idea/artifacts/SpringMVC_war.xml b/SpringMVC/.idea/artifacts/SpringMVC_war.xml
deleted file mode 100644
index 204a2fd..0000000
--- a/SpringMVC/.idea/artifacts/SpringMVC_war.xml
+++ /dev/null
@@ -1,8 +0,0 @@
-
-
- $PROJECT_DIR$/target
-
-
-
-
-
\ No newline at end of file
diff --git a/SpringMVC/.idea/artifacts/SpringMVC_war_exploded.xml b/SpringMVC/.idea/artifacts/SpringMVC_war_exploded.xml
deleted file mode 100644
index 7faff29..0000000
--- a/SpringMVC/.idea/artifacts/SpringMVC_war_exploded.xml
+++ /dev/null
@@ -1,27 +0,0 @@
-
-
- $PROJECT_DIR$/target/SpringMVC
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/SpringMVC/.idea/compiler.xml b/SpringMVC/.idea/compiler.xml
index ef7bf86..5636681 100644
--- a/SpringMVC/.idea/compiler.xml
+++ b/SpringMVC/.idea/compiler.xml
@@ -14,6 +14,7 @@
+
diff --git a/SpringMVC/.idea/misc.xml b/SpringMVC/.idea/misc.xml
index 90fec2a..b49eba8 100644
--- a/SpringMVC/.idea/misc.xml
+++ b/SpringMVC/.idea/misc.xml
@@ -14,25 +14,6 @@
-
-
-
-
-
-
-
-
- JSP Inspections
-
-
-
-
- Abstraction issues
-
-
-
-
-
http://www.w3.org/1999/xhtml
diff --git a/SpringMVC/.idea/workspace.xml b/SpringMVC/.idea/workspace.xml
index 8a740f3..63041a1 100644
--- a/SpringMVC/.idea/workspace.xml
+++ b/SpringMVC/.idea/workspace.xml
@@ -58,18 +58,101 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
+
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
@@ -80,30 +163,30 @@
-
-
+
+
@@ -120,6 +203,25 @@
+
+
+
+
+
+
+
+
+ JSP Inspections
+
+
+
+
+ Abstraction issues
+
+
+
+
+
@@ -148,6 +250,184 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
@@ -158,27 +438,6 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
@@ -186,10 +445,11 @@
-
+
+
@@ -203,6 +463,7 @@
+
@@ -410,7 +671,7 @@
-
+
@@ -429,8 +690,8 @@
-
-
+
+
@@ -472,7 +733,7 @@
-
+
@@ -489,6 +750,40 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
@@ -497,107 +792,73 @@
-
+
-
+
-
+
-
+
-
+
-
+
+
+
-
+
+
+
-
+
+
+
-
+
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
-
-
-
-
-
-
-
+
-
-
-
-
-
-
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
@@ -607,17 +868,32 @@
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
+
-
@@ -645,7 +921,7 @@
-
+
@@ -682,7 +958,6 @@
-
@@ -706,7 +981,7 @@
-
+
diff --git a/SpringMVC/SpringMVC.iml b/SpringMVC/SpringMVC.iml
index bbd0c34..87fa477 100644
--- a/SpringMVC/SpringMVC.iml
+++ b/SpringMVC/SpringMVC.iml
@@ -16,6 +16,7 @@
+
diff --git a/SpringMVC/src/java/com/raychatter/common/annotation/ExceptionHandler.java b/SpringMVC/src/java/com/raychatter/common/annotation/ExceptionHandler.java
index 773fde0..a802283 100644
--- a/SpringMVC/src/java/com/raychatter/common/annotation/ExceptionHandler.java
+++ b/SpringMVC/src/java/com/raychatter/common/annotation/ExceptionHandler.java
@@ -19,5 +19,4 @@ import java.lang.annotation.Target;
public @interface ExceptionHandler {
HttpStatus httpStatus() default HttpStatus.OK;
String contentType();
- String message() default "Oh shit!";
}
diff --git a/SpringMVC/src/java/com/raychatter/common/annotation/SupportedExceptions.java b/SpringMVC/src/java/com/raychatter/common/annotation/SupportedExceptions.java
deleted file mode 100644
index 1b5c8cb..0000000
--- a/SpringMVC/src/java/com/raychatter/common/annotation/SupportedExceptions.java
+++ /dev/null
@@ -1,20 +0,0 @@
-package com.raychatter.common.annotation;
-
-import java.lang.annotation.ElementType;
-import java.lang.annotation.Retention;
-import java.lang.annotation.RetentionPolicy;
-import java.lang.annotation.Target;
-
-@Retention(RetentionPolicy.RUNTIME)
-@Target({
- ElementType.ANNOTATION_TYPE,
- ElementType.CONSTRUCTOR,
- ElementType.FIELD,
- ElementType.LOCAL_VARIABLE,
- ElementType.METHOD,
- ElementType.PARAMETER,
- ElementType.TYPE})
-public @interface SupportedExceptions {
- Class extends Throwable>[] value();
-}
-
diff --git a/SpringMVC/src/java/com/raychatter/common/controller/HelloController.java b/SpringMVC/src/java/com/raychatter/common/controller/HelloController.java
index 5cf20ac..ed0f399 100755
--- a/SpringMVC/src/java/com/raychatter/common/controller/HelloController.java
+++ b/SpringMVC/src/java/com/raychatter/common/controller/HelloController.java
@@ -1,7 +1,5 @@
package com.raychatter.common.controller;
-import com.raychatter.common.annotation.SupportedExceptions;
-import com.raychatter.common.exception.CustomException;
import com.raychatter.common.exception.MyNegativeArraySizeException;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
@@ -10,16 +8,15 @@ import org.springframework.web.bind.annotation.RequestMethod;
@Controller
-@SupportedExceptions(value={CustomException.class, MyNegativeArraySizeException.class})
public class HelloController {
@RequestMapping(value = "/welcome", method = RequestMethod.GET)
public void printWelcome(ModelMap model) throws Exception {
model.addAttribute("message", "Hello, World!");
- throw new CustomException("It's broken!");
-// throw new TypeMismatchException("message",Integer.class);
-// throw new MyNegativeArraySizeException("oops");
+// throw new CustomException("It's broken!");
+// throw new TypeMismatchException("messageParameters",Integer.class);
+ throw new MyNegativeArraySizeException("oops");
}
}
\ No newline at end of file
diff --git a/SpringMVC/src/java/com/raychatter/common/exception/AnnotationHandler.java b/SpringMVC/src/java/com/raychatter/common/exception/AnnotationHandler.java
index f203676..89db636 100644
--- a/SpringMVC/src/java/com/raychatter/common/exception/AnnotationHandler.java
+++ b/SpringMVC/src/java/com/raychatter/common/exception/AnnotationHandler.java
@@ -1,16 +1,14 @@
package com.raychatter.common.exception;
import com.raychatter.common.annotation.ExceptionHandler;
-import com.raychatter.common.annotation.SupportedExceptions;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
-import java.util.Collections;
-import java.util.HashSet;
-import java.util.Set;
+import java.io.InputStream;
+import java.util.Scanner;
public class AnnotationHandler implements HandlerExceptionResolver {
@@ -26,36 +24,34 @@ public class AnnotationHandler implements HandlerExceptionResolver {
// handlerClass = router.getBeanType();
// }
- final SupportedExceptions supportedExceptionsAnnotation = handler.getClass().getAnnotation(SupportedExceptions.class);
- if (supportedExceptionsAnnotation == null) {
- // TODO: test what you get in UI when you return here, also test what happens if you return 'null';
- return new ModelAndView();
- }
-
- final Set> exceptions = new HashSet>();
- Collections.addAll(exceptions, supportedExceptionsAnnotation.value());
-
- if(exceptions.contains(thrownException.getClass())) {
- return doStuffWithAnnotation(thrownException,response);
- }
-
- return doStuffWithAnnotation(thrownException,response);
- }
-
- private ModelAndView doStuffWithAnnotation(final Exception thrownException, final HttpServletResponse response) {
final ExceptionHandler exceptionHandlerAnnotation = thrownException.getClass().getAnnotation(ExceptionHandler.class);
if (exceptionHandlerAnnotation == null) {
// TODO: test what you get in UI when you return here, also test what happens if you return 'null';
return new ModelAndView();
}
- response.setStatus(exceptionHandlerAnnotation.httpStatus().value());
+
+ return doStuffWithAnnotation(exceptionHandlerAnnotation, thrownException, response);
+ }
+
+ private ModelAndView doStuffWithAnnotation(final ExceptionHandler exceptionHandlerAnnotation, final Exception thrownException, final HttpServletResponse response) {
+
response.setContentType(exceptionHandlerAnnotation.contentType());
+ response.setStatus(exceptionHandlerAnnotation.httpStatus().value());
try {
- response.getWriter().write(String.format(exceptionHandlerAnnotation.message(), thrownException.getMessage()));
+ response.getWriter().write(formatMessage(thrownException));
} catch (IOException e) {
e.printStackTrace();
}
return new ModelAndView();
}
+ private String formatMessage(final Exception thrownException) {
+ return String.format(readTemplate(), thrownException.getMessage());
+ }
+
+ private String readTemplate() {
+ final InputStream templateFile = getClass().getResourceAsStream("/error.template");
+ return new Scanner(templateFile, "UTF-8").useDelimiter("\\A").next().trim();
+ }
+
}
diff --git a/SpringMVC/src/java/com/raychatter/common/exception/CustomException.java b/SpringMVC/src/java/com/raychatter/common/exception/CustomException.java
index 896ce40..bb78d0a 100644
--- a/SpringMVC/src/java/com/raychatter/common/exception/CustomException.java
+++ b/SpringMVC/src/java/com/raychatter/common/exception/CustomException.java
@@ -5,7 +5,7 @@ package com.raychatter.common.exception;
import com.raychatter.common.annotation.ExceptionHandler;
import org.springframework.http.HttpStatus;
-@ExceptionHandler(httpStatus = HttpStatus.NOT_FOUND,contentType = "text/html", message = "Custom error!")
+@ExceptionHandler(httpStatus = HttpStatus.NOT_FOUND,contentType = "text/html")
public class CustomException extends Exception {
public static final String ERROR_MESSAGE = "a lot of good things";
diff --git a/SpringMVC/src/java/com/raychatter/common/exception/MyNegativeArraySizeException.java b/SpringMVC/src/java/com/raychatter/common/exception/MyNegativeArraySizeException.java
index 1a870c2..ed0bc81 100644
--- a/SpringMVC/src/java/com/raychatter/common/exception/MyNegativeArraySizeException.java
+++ b/SpringMVC/src/java/com/raychatter/common/exception/MyNegativeArraySizeException.java
@@ -3,7 +3,7 @@ package com.raychatter.common.exception;
import com.raychatter.common.annotation.ExceptionHandler;
import org.springframework.http.HttpStatus;
-@ExceptionHandler(httpStatus = HttpStatus.BAD_REQUEST,contentType = "application/JSON", message = "{\"errors\": \"%s\" }")
+@ExceptionHandler(httpStatus = HttpStatus.BAD_REQUEST,contentType = "application/JSON")
public class MyNegativeArraySizeException extends NegativeArraySizeException {
public MyNegativeArraySizeException(String s) {
super(s);
diff --git a/SpringMVC/src/resources/META-INF/services/javax.annotation.processing.Processor b/SpringMVC/src/resources/META-INF/services/javax.annotation.processing.Processor
deleted file mode 100644
index 8ad4b6f..0000000
--- a/SpringMVC/src/resources/META-INF/services/javax.annotation.processing.Processor
+++ /dev/null
@@ -1 +0,0 @@
-com.mkyong.common.controller.annotation.processor.ConsoleSurpriseProcessor
diff --git a/SpringMVC/src/resources/META-INF/services/error.json.template b/SpringMVC/src/resources/error.json.template
similarity index 100%
rename from SpringMVC/src/resources/META-INF/services/error.json.template
rename to SpringMVC/src/resources/error.json.template
diff --git a/SpringMVC/src/resources/error.template b/SpringMVC/src/resources/error.template
new file mode 100644
index 0000000..9ed3e1e
--- /dev/null
+++ b/SpringMVC/src/resources/error.template
@@ -0,0 +1 @@
+{"errors": "%s"}
\ No newline at end of file
diff --git a/SpringMVC/target/classes/com/raychatter/common/annotation/ExceptionHandler.class b/SpringMVC/target/classes/com/raychatter/common/annotation/ExceptionHandler.class
index dc6a42e..9adf2b0 100644
Binary files a/SpringMVC/target/classes/com/raychatter/common/annotation/ExceptionHandler.class and b/SpringMVC/target/classes/com/raychatter/common/annotation/ExceptionHandler.class differ
diff --git a/SpringMVC/target/classes/com/raychatter/common/annotation/SupportedExceptions.class b/SpringMVC/target/classes/com/raychatter/common/annotation/SupportedExceptions.class
deleted file mode 100644
index df18649..0000000
Binary files a/SpringMVC/target/classes/com/raychatter/common/annotation/SupportedExceptions.class and /dev/null differ
diff --git a/SpringMVC/target/classes/com/raychatter/common/controller/HelloController.class b/SpringMVC/target/classes/com/raychatter/common/controller/HelloController.class
index 6d45700..5466803 100644
Binary files a/SpringMVC/target/classes/com/raychatter/common/controller/HelloController.class and b/SpringMVC/target/classes/com/raychatter/common/controller/HelloController.class differ
diff --git a/SpringMVC/target/classes/com/raychatter/common/exception/AnnotationHandler.class b/SpringMVC/target/classes/com/raychatter/common/exception/AnnotationHandler.class
index 6bb1a1d..41227db 100644
Binary files a/SpringMVC/target/classes/com/raychatter/common/exception/AnnotationHandler.class and b/SpringMVC/target/classes/com/raychatter/common/exception/AnnotationHandler.class differ
diff --git a/SpringMVC/target/classes/com/raychatter/common/exception/CustomException.class b/SpringMVC/target/classes/com/raychatter/common/exception/CustomException.class
index 8ed2410..8189390 100644
Binary files a/SpringMVC/target/classes/com/raychatter/common/exception/CustomException.class and b/SpringMVC/target/classes/com/raychatter/common/exception/CustomException.class differ
diff --git a/SpringMVC/target/classes/com/raychatter/common/exception/MyNegativeArraySizeException.class b/SpringMVC/target/classes/com/raychatter/common/exception/MyNegativeArraySizeException.class
index bf387d1..0f495db 100644
Binary files a/SpringMVC/target/classes/com/raychatter/common/exception/MyNegativeArraySizeException.class and b/SpringMVC/target/classes/com/raychatter/common/exception/MyNegativeArraySizeException.class differ
diff --git a/SpringMVC/target/classes/error.json.template b/SpringMVC/target/classes/error.json.template
new file mode 100644
index 0000000..c263fa2
--- /dev/null
+++ b/SpringMVC/target/classes/error.json.template
@@ -0,0 +1,14 @@
+{
+ "code": %d,
+ "error": %s,
+ "description": %s
+}
+----------------
+
+
+ %d
+ %s
+
+----------------
+code: %d
+error: %s
\ No newline at end of file
diff --git a/SpringMVC/target/classes/error.template b/SpringMVC/target/classes/error.template
new file mode 100644
index 0000000..9ed3e1e
--- /dev/null
+++ b/SpringMVC/target/classes/error.template
@@ -0,0 +1 @@
+{"errors": "%s"}
\ No newline at end of file
diff --git a/SpringMVC/target/web/WEB-INF/classes/com/raychatter/common/annotation/ExceptionHandler.class b/SpringMVC/target/web/WEB-INF/classes/com/raychatter/common/annotation/ExceptionHandler.class
index dc6a42e..9adf2b0 100644
Binary files a/SpringMVC/target/web/WEB-INF/classes/com/raychatter/common/annotation/ExceptionHandler.class and b/SpringMVC/target/web/WEB-INF/classes/com/raychatter/common/annotation/ExceptionHandler.class differ
diff --git a/SpringMVC/target/web/WEB-INF/classes/com/raychatter/common/annotation/SupportedExceptions.class b/SpringMVC/target/web/WEB-INF/classes/com/raychatter/common/annotation/SupportedExceptions.class
deleted file mode 100644
index df18649..0000000
Binary files a/SpringMVC/target/web/WEB-INF/classes/com/raychatter/common/annotation/SupportedExceptions.class and /dev/null differ
diff --git a/SpringMVC/target/web/WEB-INF/classes/com/raychatter/common/controller/HelloController.class b/SpringMVC/target/web/WEB-INF/classes/com/raychatter/common/controller/HelloController.class
index 6d45700..5466803 100644
Binary files a/SpringMVC/target/web/WEB-INF/classes/com/raychatter/common/controller/HelloController.class and b/SpringMVC/target/web/WEB-INF/classes/com/raychatter/common/controller/HelloController.class differ
diff --git a/SpringMVC/target/web/WEB-INF/classes/com/raychatter/common/exception/AnnotationHandler.class b/SpringMVC/target/web/WEB-INF/classes/com/raychatter/common/exception/AnnotationHandler.class
index 6bb1a1d..41227db 100644
Binary files a/SpringMVC/target/web/WEB-INF/classes/com/raychatter/common/exception/AnnotationHandler.class and b/SpringMVC/target/web/WEB-INF/classes/com/raychatter/common/exception/AnnotationHandler.class differ
diff --git a/SpringMVC/target/web/WEB-INF/classes/com/raychatter/common/exception/CustomException.class b/SpringMVC/target/web/WEB-INF/classes/com/raychatter/common/exception/CustomException.class
index 8ed2410..8189390 100644
Binary files a/SpringMVC/target/web/WEB-INF/classes/com/raychatter/common/exception/CustomException.class and b/SpringMVC/target/web/WEB-INF/classes/com/raychatter/common/exception/CustomException.class differ
diff --git a/SpringMVC/target/web/WEB-INF/classes/com/raychatter/common/exception/MyNegativeArraySizeException.class b/SpringMVC/target/web/WEB-INF/classes/com/raychatter/common/exception/MyNegativeArraySizeException.class
index bf387d1..0f495db 100644
Binary files a/SpringMVC/target/web/WEB-INF/classes/com/raychatter/common/exception/MyNegativeArraySizeException.class and b/SpringMVC/target/web/WEB-INF/classes/com/raychatter/common/exception/MyNegativeArraySizeException.class differ
diff --git a/SpringMVC/target/web/WEB-INF/classes/error.json.template b/SpringMVC/target/web/WEB-INF/classes/error.json.template
new file mode 100644
index 0000000..c263fa2
--- /dev/null
+++ b/SpringMVC/target/web/WEB-INF/classes/error.json.template
@@ -0,0 +1,14 @@
+{
+ "code": %d,
+ "error": %s,
+ "description": %s
+}
+----------------
+
+
+ %d
+ %s
+
+----------------
+code: %d
+error: %s
\ No newline at end of file
diff --git a/SpringMVC/target/web/WEB-INF/classes/error.template b/SpringMVC/target/web/WEB-INF/classes/error.template
new file mode 100644
index 0000000..9ed3e1e
--- /dev/null
+++ b/SpringMVC/target/web/WEB-INF/classes/error.template
@@ -0,0 +1 @@
+{"errors": "%s"}
\ No newline at end of file