Dropwizard : Exception Handling, giving custom error JSON error messages to client

梦想与她 提交于 2020-01-03 10:21:36

问题


How can i configure dropwizard to give custom error messages to User. If my function is supposed to return a object myObject, but since there is an error, it should throw and exception and return a error Object to User with a custom message.


回答1:


You can add your own subclass of WebApplicationException like this:

public class ObjectNotFoundException extends WebApplicationException {

  public ObjectNotFoundException() {
    super(Responses.notFound().build());
  }

  public ObjectNotFoundException(String message) {
    super(Response.status(Responses.NOT_FOUND).
    entity(message).type("text/plain").build());
  }

}

See the documentation for more information. You can also use ExceptionMappers if that makes more sense for your application.



来源:https://stackoverflow.com/questions/23628897/dropwizard-exception-handling-giving-custom-error-json-error-messages-to-clie

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!