JHipster: Managing server response on client
How do we handle server responses on the client side? A simple example is, say for example, a delete
operation has failed on an entity - currently JHipster is returning void
. This may not be ideal in a practical scenario.
I looked at this SO post which describes similar question: REST API error return good practices
Am planning to implement an error DTO and return something like what Twitter does:
{"errors":[{"message":"Sorry, your delete operation failed due to following business constraint...","code":100}]}
The question still remains, how can we make it generic across all API calls ie, Do we have to add this error DTO to all domain objects?
I personally think having this wired into the basic JHipster framework will be quite helpful instead of reinventing the wheel.
The fact that JHIpster's REST controller returns void for delete method does not mean it cannot send information to the client, if something is wrong an exception will be thrown probably by the underlying service or repository. This exception will be translated by the ExceptionTranslator as a ControllerAdvice into adequate HTTP error status.
If it does do what you want, it's up to you to code your own exception resolver in your controller and return what you want.
I'm not sure that returning status error 200 (OK) when delete failed just to be able to pass a message is necessarily the best approach. An error is not a 200, at least not for HTTP.
链接地址: http://www.djcxy.com/p/45336.html上一篇: 来自REST请求的错误消息