java - HttpClientErrorException: 404 null -


i try call address in controller using resttemplate , result want ok or not found status in controller

@getmapping(value = "/thanks") public modelandview confirmaccount(         @requestparam string token,         uricomponentsbuilder uricomponentsbuilder ) {     resttemplate resttemplate = new resttemplate();     httpentity<object> entity = new httpentity<>(new httpheaders());      uricomponents uricomponents             = uricomponentsbuilder.path("/register/token/{token}").buildandexpand(token);      responseentity<boolean> response = resttemplate             .exchange(uricomponents.touri(),                       httpmethod.put,                       entity,                       boolean.class);      return response.getstatuscode().tostring().equals("200")             ?  new modelandview("redirect:/signin") : new modelandview("tokennotfound"); } 

i call address of controller.

@requestmapping(value = "/register/token/{token}", method = requestmethod.put) public httpentity<boolean> confirmaccount(         @pathvariable string token ) {     optional<user> useroptional = userservice.findbyactivationtoken(token);      if(useroptional.ispresent()) {         user user = useroptional.get();          user.setactivationtoken(null);         user.setenabled(true);          userservice.saveuser(user);     } else {         return responseentity.notfound().build();     }      return responseentity.ok(true); } 

as result, throws me out in console

org.springframework.web.client.httpclienterrorexception: 404 null @ org.springframework.web.client.defaultresponseerrorhandler.handleerror(defaultresponseerrorhandler.java:63) ~[spring-web-4.3.10.release.jar:4.3.10.release] @ org.springframework.web.client.resttemplate.handleresponse(resttemplate.java:700) ~[spring-web-4.3.10.release.jar:4.3.10.release] @ org.springframework.web.client.resttemplate.doexecute(resttemplate.java:653) ~[spring-web-4.3.10.release.jar:4.3.10.release] @ org.springframework.web.client.resttemplate.execute(resttemplate.java:628) ~[spring-web-4.3.10.release.jar:4.3.10.release] @ org.springframework.web.client.resttemplate.exchange(resttemplate.java:549) ~[spring-web-4.3.10.release.jar:4.3.10.release] @ com.service.app.controller.registercontroller.confirmaccount(registercontroller.java:40) ~[classes/:na] 

why resttemplate not want return status 404 result? enter code here

a simple explanation can httpclienterrorexception unchecked exception. java docs exception tells, 'exception thrown when http 4xx received' ref - https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/client/httpclienterrorexception.html

so try handling exception appropriate try/catch , throws


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -