问题
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