如何在发展的RESTful API与Java控制异常? [英] How to control exception while developing RESTful API with java?
问题描述
我发展为我们的客户的RESTful API。结果
我很担心,如果发生某些错误,显示错误信息。结果
该错误信息的协议看起来像下面。
I'm developing RESTful API for our clients.
I'm about to show error information if some error occurs.
The error information protocol look like below.
{
"status": "failure",
"error": {
"message": "",
"type": "",
"code": 0000
}
}
在规划层面,如何控制好例外?结果
现在我做了定制的异常类扩展例外
类。 (不RuntimeException的)结果
这是方法好不好?它是更好的使用RuntimeExcepion?结果
我的自定义异常类是...
In programming level, how to controll exceptions?
Now I've made custom exception class extending Exception
class. (not RuntimeException)
Is this approach good or not? Is it better using RuntimeExcepion?
My custom exception class is ...
public class APIException extends Exception {
public enum Code {
// duplicated exceptions
ALREADY_REGISTERED(1001),
// size exceptions
OVER_KEYWORD_LIMIT(2001),
OVER_CATEGORY_LIMIT(2002),
TOO_SHORT_CONTENTS_LENGTH(2003),
TOO_SHORT_TITLE_LENGTH(2004),
// database exceptions
DB_ERROR(3001),
// unregistered exceptions
UNREGISTERED_NAME(4001),
// missing information exceptions
MISSING_PARAMETER(5001),
// invalid information exceptions
INVALID_PARAMETER(6001),
INVALID_URL_PATTERN(6002);
private final Integer value;
private Code(Integer value) {
this.value = value;
}
public Integer getType() {
return value;
}
}
private final Code code;
public APIException(Code code) {
this.code = code;
}
public APIException(Code code, Throwable cause) {
super(cause);
this.code = code;
}
public APIException(Code code, String msg, Throwable cause) {
super(msg, cause);
this.code = code;
}
public APIException(Code code, String msg) {
super(msg);
this.code = code;
}
public Code getCode() {
return code;
}
}
和使用APIException类像这样...
And using APIException class like this...
public void delete(int idx) throws APIException {
try {
Product product = productDao.findByIdx(idx);
if (product.getCount() > 0) {
throw new APIException(Code.ALREADY_REGISTERED,
"Already registered product.");
}
productDao.delete(idx);
} catch (Exception e) {
throw new APIException(Code.DB_ERROR,
"Cannot delete product. " + e.getMessage());
}
}
这是更好的做定制的异常类,或使用存在异常抛出:IllegalArgumentException一样..结果
如果进行自定义异常类比较好,我应该异常或RuntimeException的延残喘?结果
请推荐我很好的例子,像我的情况。结果
先谢谢了。
Which is better make custom exception class or using exist exception like illegalargumentexception..
If making custom exception class is better, what should I extend among Exception or RuntimeException?
Please recommend me good example like my situation.
Thanks in advance.
推荐答案
由于您使用的春天,我建议:
Since you're using Spring, I'd recommend:
-
RuntimeException的扩展,让例外通过拖放到控制器
Extending RuntimeException and letting the exceptions drop through to the controller
如果您的异常类的模型要在错误的XML返回,注释异常,因此它可以作为响应(包括返回的属性的 @ResponseStatus
如果他们都具有相同的地位code)。
If your exception class models the attributes you want to return in your error XML, annotate the exception so it can be returned as a response (including a @ResponseStatus
if they all have the same status code).
实现一个或多个 @ExceptionHandler
您的控制器上返回异常的方法 @ResponseBody
并确保HttpServletResponse的是正确的。是这样的:
Implement one or more @ExceptionHandler
methods on your controller that returns the exception as the @ResponseBody
and ensures the HttpServletResponse is correct. Something like:
@ExceptionHandler
@ResponseBody
public ErrorResponse handleAPIException(APIException e, HttpServletResponse response) {
// Set any response attributes you need...
return e; // or some other response
}
这篇关于如何在发展的RESTful API与Java控制异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!