java - Spring Boot how to ignore HttpStatus Exceptions -
i'm building application using spring boot. application distributed, means have multiple api's call each others.
one of underlying services interacts database , responds requested data. if request unexisting id made, response 404 httpstatus:
return new responseentity<>(httpstatus.not_found);
(same 400 error on operations, or 204 deleting entry etc).
the problem have other spring boot applications call these api's, throw org.springframework.web.client.httpclienterrorexception: 404 not found
exception when request, in example, unexisting entry. 404 status code intended , should not return exception (causing hystrix circuit breaker call fallback function).
how can solve problem?
the call service implemented in code: responseentity<object> data = resttemplate.getforentity(url, object.class);
my resttemplate set this:
private resttemplate resttemplate = new resttemplate();
spring's resttemplate
uses responseerrorhandler
handle errors in responses. interface provides both way determine if response has error (responseerrorhandler#haserror(clienthttpresponse)
) , how handle (responseerrorhandler#handleerror(clienthttpresponse)
).
you can set resttemplate
's responseerrorhandler
resttemplate#seterrorhandler(responseerrorhandler)
javadoc states
by default,
resttemplate
usesdefaultresponseerrorhandler
.
this default implementation
[...] checks status code on
clienthttpresponse
: code serieshttpstatus.series.client_error
orhttpstatus.series.server_error
considered error. behavior can changed overridinghaserror(httpstatus)
method.
in case of error, throws exception seeing.
if want change behavior, can provide own responseerrorhandler
implementation (maybe overriding defaultresponseerrorhandler
) doesn't consider 4xx error or doesn't throw exception.
for example
resttemplate.seterrorhandler(new responseerrorhandler() { @override public boolean haserror(clienthttpresponse response) throws ioexception { return false; // or whatever consider error } @override public void handleerror(clienthttpresponse response) throws ioexception { // nothing, or } });
you can check status code of responseentity
returned getforentity
, handle yourself.
Comments
Post a Comment