RESTful Error Handling with Spring
来源:互联网 发布:android多屏互动源码 编辑:程序博客网 时间:2024/05/16 05:12
This post will illustrate a way in which exception handling can be implemented for RESTful web services in Spring in such a manner that the exception handling concerns are separated from the application logic.
Taking advantage of the @ControllerAdvice
annotation we are able to create aglobal helper class for all controllers. By adding methods that we annotate with both@ExceptionHandler
and @ResponseStatus
we can specify which type of exception gets mapped to which HTTP response status. E.g., our customNotFoundException
can be mapped to a HTTP response of 404 Not Found, or all exceptions that are not caught elsewhere will result in HTTP status500 Internal Server Error by catching java.lang.Exception
, or anIllegalArgumentException
can result in 400 Bad Request, or… well, I’m sure you got the general idea.
If required, you can also send back some more detail to the client by adding@ResponseBody
into the mix.
Below is a very limited example to get you started.
GeneralRestExceptionHandler
package it.jdev.examples.spring.rest.exceptions;import java.lang.invoke.MethodHandles;import org.slf4j.*;import org.springframework.core.Ordered;import org.springframework.core.annotation.Order;import org.springframework.http.HttpStatus;import org.springframework.web.bind.annotation.*;import org.springframework.web.context.request.ServletWebRequest;@ControllerAdvice@Order(Ordered.LOWEST_PRECEDENCE)public class GeneralRestExceptionHandler { private static final Logger LOGGER = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); @ResponseStatus(HttpStatus.NOT_FOUND) @ExceptionHandler(CustomNotFoundException.class) public void handleNotFoundException(final Exception exception) { logException(exception); } @ResponseStatus(HttpStatus.FORBIDDEN) @ExceptionHandler(CustomForbiddenException.class) public void handleForbiddenException(final Exception exception) { logException(exception); } @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) @ExceptionHandler({ CustomException.class, Exception.class }) public void handleGeneralException(final Exception exception) { logException(exception); } @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) @ExceptionHandler(Exception.class) public void handleGeneralException(final Exception exception) { logException(exception); } @ResponseStatus(HttpStatus.BAD_REQUEST) @ExceptionHandler({ CustomBadRequestException.class, IllegalArgumentException.class }) @ResponseBody public String handleBadRequestException(final Exception exception) { logException(exception); return exception.getMessage(); } // Add more exception handling as needed… // … private void logException(final Exception exception) { // ... }}
Reference: RESTful Error Handling with Spring from ourJCG partner Wim van Haaren at theJDev blog.
转载:http://www.javacodegeeks.com/2015/06/restful-error-handling-with-spring.html
- RESTful Error Handling with Spring
- Error Handling with exceptions
- Error Handling with Exceptions【1】
- Error Handling with Exceptions【2】
- Error Handling with Exceptions【3】
- Error Handling with Exceptions【4】
- Error Handling with Exceptions【5】
- 第九章: Error Handling with Exceptions
- Error Handling with C++ Exceptions, Part 1
- WCF4.0 -- RESTful WCF Services 错误处理 (Error Handling)
- WCF4.0 -- RESTful WCF Services 错误处理 (Error Handling)
- Spring global exception handling with standalone app
- Spring MVC handling multipage forms with AbstractWizardFormController
- Error Handling
- Error Handling
- Using R — Easier Error Handling with try()
- Think In Java 笔记8 Error Handling with Exceptions
- Building a RESTful api with Spring note1
- Apache Rewrite重写模块(一):一个入门的例子
- 菜单栏显示效果
- 2015061908 - 人来人往
- uva10635 王子和公主(把lcs转化为lis)
- sgu 213
- RESTful Error Handling with Spring
- 欢迎使用CSDN-markdown编辑器
- 数据库学习之--Oracle 架构与MySQL架构对比
- 30 Java Programming Tips and Best Practices for Beginners
- 2015061909 - 将List中数据随机变化位置
- Lua相关的扩展库
- javase 1506-19
- android5.1 系统alarmManger轮训间隔最小是1分钟了
- 如何学好C语言