Added couple more descriptive things to the readme
This commit is contained in:
20
README.md
20
README.md
@@ -1,17 +1,12 @@
|
||||
# spring-restful-exception-handler
|
||||
|
||||
An annotation for the Spring framework to handle HTTP responses for custom exceptions.
|
||||
|
||||
## How to use spring-restful-exception-handler
|
||||
|
||||
Under PROJECT/resources create a file called *error.template*. Inside this file, place the formatted error template you want to return when a custom exception is thrown. If no custom template is given, the following default template will be used:
|
||||
An annotation for the Spring framework to handle HTTP responses for custom exceptions. The way it works is you have to change the default exception resolver for Spring to our custom exception resolver by modifying your servlet xml file for Spring:
|
||||
|
||||
```xml
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<error> %s </error>
|
||||
<bean id="exceptionResolver" class="com.raychatter.common.annotation.AnnotationHandler" />
|
||||
```
|
||||
|
||||
Annotate the custom exception class with `@ExceptionHandler(*httpStatus*, *contentType*)`. The defaults are `httpStatus = HttpStatus.INTERNAL_SERVER_ERROR` and `contentType = MediaType.APPLICATION_XML_VALUE`. So an example exception class would be:
|
||||
After overriding the exception resolving mechanism, just annotate the custom exception classes with `@ExceptionHandler(*httpStatus*, *contentType*)`. The defaults are `httpStatus = HttpStatus.INTERNAL_SERVER_ERROR` and `contentType = MediaType.APPLICATION_XML_VALUE`. So an example exception class would be:
|
||||
|
||||
```java
|
||||
@ExceptionHandler(httpStatus = HttpStatus.NOT_FOUND, contentType = MediaType.APPLICATION_XML_VALUE)
|
||||
@@ -24,13 +19,16 @@ public class MyCustomException extends Exception {
|
||||
|
||||
The custom message is taken from the custom annotation class itself, so any parameters you'd like to insert need to be handled there.
|
||||
|
||||
Make sure to add following to your servlet XML:
|
||||
And that's it! Just keep in mind that the exception handler will take care of all the exceptions and by default it will return `Internal Server Error` with an `XML` body described above. If you don't specify any custom template for your error responses, following error template will be used by default:
|
||||
|
||||
```xml
|
||||
<bean id="exceptionResolver" class="com.raychatter.common.annotation.AnnotationHandler" />
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<error>
|
||||
%s
|
||||
</error>
|
||||
```
|
||||
|
||||
And that's it! Just keep in mind that the exception handler will take care of all the exceptions and by default it will return `Internal Server Error` with an `XML` body described above. If you want to override, make sure you have `error.template` in your classpath with `%s` for the message placeholder. An example for a `error.template` file in your classpath for a json response:
|
||||
If you want to override, make sure you have `error.template` in your classpath with `%s` for the message placeholder. An example for a `error.template` file in your classpath for a json response:
|
||||
|
||||
```json
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user