java 无效输入请求的 REST 服务异常
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35869110/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Exception for REST services for invalid input requests
提问by JPS
I am currently developing REST
services and throwing BadRequestException
for all of the following,
我目前正在开发REST
服务并BadRequestException
为以下所有内容投掷,
1. Path parameter is invalid
2. Query parameter is invalid
4. Input request object has missing attributes
Is there any specific exceptions for each case like InvalidParameterExceptionor so..? Is there any documentation available to learn which exceptions should be thrown on what situations?
对于每种情况,是否有任何特定的例外,例如InvalidParameterException等等......?是否有任何文档可以了解在什么情况下应该抛出哪些异常?
回答by cassiomolin
I think it's a personal decision and the answer will depend on your needs to have more detailed expceptions or not.
我认为这是个人决定,答案将取决于您是否需要更详细的例外情况。
There are two ways to handle errors with JAX-RS:
有两种方法可以使用 JAX-RS 处理错误:
Throwing a WebApplicationException
扔一个 WebApplicationException
That's the approach you are using, which allows you to map exceptions that extend WebApplicationException
to HTTP error responses.
这就是您正在使用的方法,它允许您映射扩展WebApplicationException
到 HTTP 错误响应的异常。
I think throwing a BadRequestException
is just fine for all the situations mentioned in your question. Just remember adding a detailed message explaining what was wrong.
我认为BadRequestException
对于您问题中提到的所有情况,抛出 a都很好。只记得添加一条详细的消息来解释什么是错误的。
If you need a more specific exception, you could consider extending the BadRequestException
or maybe the ClientErrorException
. The new exceptios could encapsulate the message which explains what the problem with the request. It's up to your needs.
如果您需要更具体的异常,你可以考虑扩大BadRequestException
或可能的ClientErrorException
。新的异常可以封装解释请求问题的消息。这取决于您的需求。
For more details on the exceptions provided by the JAX-RS API, have a look at the javax.ws.rs
package documentation. If they do not fit your needs, just extend them and create your specific exceptions.
有关 JAX-RS API 提供的异常的更多详细信息,请查看javax.ws.rs
包文档。如果它们不适合您的需求,只需扩展它们并创建您的特定例外。
Using an ExceptionMapper
使用 ExceptionMapper
In other cases it may not be appropriate to throw instances of WebApplicationException
, or classes that extend WebApplicationException
, and instead it may be preferable to map an existing exception to a response. For such cases it is possible to use a custom exception mapping provider.
在其他情况下,抛出 的实例WebApplicationException
或扩展的类可能不合适WebApplicationException
,相反,将现有异常映射到响应可能更可取。对于这种情况,可以使用自定义异常映射提供程序。
Consider, for example, you decide to throw an IllegalArgumentException
whenever you received an inapropriate value for your query or path parameters. You can create an ExceptionMapper
to map the IllegalArgumentException
to a response with the 400
status code:
例如,考虑一下,您决定IllegalArgumentException
在收到查询或路径参数不合适的值时抛出。您可以创建一个ExceptionMapper
以将 映射IllegalArgumentException
到具有400
状态代码的响应:
@Provider
public class IllegalArgumentExceptionMapper
implements ExceptionMapper<IllegalArgumentException> {
@Override
public Response toResponse(IllegalArgumentException exception) {
return Response.status(400).entity(exception.getMessage())
.type("text/plain").build();
}
}
For more details, have a look at the Jersey documentation.
有关更多详细信息,请查看 Jersey文档。
回答by morsor
All 3 errors sound like client errors, as the client fails to abide by the contract - so I would return a HTTP 400 Bad Request- perhaps with an explanation in the body of the response.
所有 3 个错误听起来都像是客户端错误,因为客户端未能遵守合同 - 所以我会返回一个HTTP 400 错误请求- 可能在响应正文中有一个解释。
回答by pandaadb
I believe usually you would create separate cases depending on how you would like to handle these errors. For example, you will have 3 different exceptions to represent your errors.
我相信通常您会根据您希望如何处理这些错误来创建单独的案例。例如,您将有 3 个不同的异常来表示您的错误。
Most frameworks then allow you to install ExceptionMappers. These map your exceptions to an HTTP response code. These are documented and you should follow them:
大多数框架都允许您安装 ExceptionMappers。这些将您的异常映射到 HTTP 响应代码。这些已记录在案,您应该遵循它们:
For example: http://www.restapitutorial.com/httpstatuscodes.html
例如:http: //www.restapitutorial.com/httpstatuscodes.html
In your case for example, I would throw IllegalArgumentExceptions for all those 3 cases and install a mapper, mapping this to a 400 response code with potentially some info. This can be for example important since the client consuming your service will not receive your exceptions anyway, but rather analyse the response code of the request. With a 400, a user will then know that the request was invalid and won't be retried. You can have similar cases for all sorts.
例如,在您的情况下,我会为所有这 3 种情况抛出 IllegalArgumentExceptions 并安装映射器,将其映射到 400 响应代码,其中可能包含一些信息。例如,这可能很重要,因为使用您的服务的客户端无论如何都不会收到您的异常,而是会分析请求的响应代码。使用 400,用户将知道请求无效并且不会重试。你可以有各种类似的案例。
To read about exception mappers, for example with the help of jersey:
要阅读异常映射器,例如在 jersey 的帮助下:
https://jersey.java.net/documentation/latest/representations.html
https://jersey.java.net/documentation/latest/representations.html
So to your question: No, I don't believe there is any best-practise on what Exceptions are thrown from your application. Usually REST frameworks don't have specific exception mappers other than a catch-all mapper that will return a 500 (Internal Server Error)
所以对于你的问题:不,我不相信有关于从你的应用程序抛出什么异常的任何最佳实践。通常 REST 框架除了会返回 500(内部服务器错误)的全能映射器之外没有特定的异常映射器
There is however documentation for REST and the HTTP with regards to which responses should be returned for specific use cases. You should try and design your REST endpoint to conform to those standards for maximum reusability and understandability.
然而,有 REST 和 HTTP 的文档关于应该为特定用例返回哪些响应。您应该尝试设计您的 REST 端点以符合这些标准,以获得最大的可重用性和可理解性。
I hope that helps,
我希望有帮助,
Artur
阿图尔