spring mvc - 406 Status on Response when invoking setErrorResult on DeferredResult -
spring mvc - 406 Status on Response when invoking setErrorResult on DeferredResult -
i trying create simple spring restcontroller uses rxjava's observable class perform async processing. here code:
@restcontroller @requestmapping("/users") public class usercontroller { @autowired private userasyncrepository repository; @requestmapping(value="/{username}", method=get) public deferredresult<responseentity<user>> getbyusername( @pathvariable string username) { final deferredresult<responseentity<user>> deferred = new deferredresult<responseentity<user>>(); repository .findbyusername(username) // returns observable<user> .singleordefault(null) .timeout(1, timeunit.seconds) .subscribe(u -> { if(u == null) { deferred.seterrorresult(responseentity.notfound()); } else { deferred.setresult(responseentity.ok(u)); } }, t -> { deferred.seterrorresult(responseentity.status(httpstatus.internal_server_error)); } ); homecoming deferred; } }
when method invoked valid username, response expect status 200. when method invoked invalid username, response 406 instead of 404, expect. allow me know if have thought why happening.
thanks in advance help.
by passing instance of exception seterrorresult method of deferredresult class, able 404 response expected. here exception class:
@responsestatus(httpstatus.not_found) public class resourcenotfoundexception extends runtimeexception { private static final long serialversionuid = 1l; public resourcenotfoundexception(string message) { super(message); } }
the key making sure add together @responsestatus annotation custom exception class.
hth.
spring-mvc rx-java
Comments
Post a Comment