java - Throwing ResourceAccessException vs HttpClientErrorException for RestTemplate client in Spring -


using spring 4.3.1 on apache tomcat 8.5, have implemented rest server handles get, post & put requests. post request handled in form:

@post @consumes({mediatype.application_xml, mediatype.application_json}) @transactional public response postmethod(final mydomain object) {      domainhandler.createdomain(object);      return response.status(201).entity(object).build(); } 

during handling post request on server side, webapplicationexception thrown, handled by:

@provider public class webexceptionmapper implements exceptionmapper<webapplicationexception> {      @override      public response toresponse(webapplicationexception exception) {           int httpstatus = exception.getresponse().getstatus();           errormessage em = new weberrormessage(exception.getmessage());           return response.status(httpstatus).entity(em).build();      } } 

on client side:

response = rest.exchange(requesturl, method, requestentity, mydomain.class); 

the resourceaccessexception thrown:

2017-07-25 15:46:41,489 error [com.my.code.web.presentation.managementcontroller:63] (http-nio-8080-exec-7#34): internal exception occured cause: org.springframework.web.client.resourceaccessexception: i/o error on post request "http://localhost:8080/": server returned http response code: 500 url: http://localhost:8080/; nested exception java.io.ioexception: server returned http response code: 500 url: http://localhost:8080/      @ org.springframework.web.client.resttemplate.doexecute(resttemplate.java:633) ~[spring-web-4.3.1.release.jar:4.3.1.release]      @ org.springframework.web.client.resttemplate.execute(resttemplate.java:580) ~[spring-web-4.3.1.release.jar:4.3.1.release]      @ org.springframework.web.client.resttemplate.exchange(resttemplate.java:498) ~[spring-web-4.3.1.release.jar:4.3.1.release]      @ com.my.code.web.integration.client.rest.feedbackrestclient.callforapicreatefeedback(feedbackrestclient.java:218) ~[classes/:?]      @ com.my.code.web.integration.client.rest.feedbackrestclient.pushfeedbackdata(feedbackrestclient.java:70) ~[classes/:?]      @ com.my.code.web.presentation.feedbackcontroller.feedback(feedbackcontroller.java:91) ~[classes/:?]      @ sun.reflect.generatedmethodaccessor72.invoke(unknown source) ~[?:?]      @ sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:43) ~[?:1.8.0_131]      @ java.lang.reflect.method.invoke(method.java:498) ~[?:1.8.0_131]      @ org.springframework.web.method.support.invocablehandlermethod.doinvoke(invocablehandlermethod.java:221) ~[spring-web-4.3.1.release.jar:4.3.1.release]      @ org.springframework.web.method.support.invocablehandlermethod.invokeforrequest(invocablehandlermethod.java:136) ~[spring-web-4.3.1.release.jar:4.3.1.release]      @ org.springframework.web.servlet.mvc.method.annotation.servletinvocablehandlermethod.invokeandhandle(servletinvocablehandlermethod.java:114) ~[spring-webmvc-4.3.1.release.jar:4.3.1.release]      @ org.springframework.web.servlet.mvc.method.annotation.requestmappinghandleradapter.invokehandlermethod(requestmappinghandleradapter.java:827) ~[spring-webmvc-4.3.1.release.jar:4.3.1.release]      @ org.springframework.web.servlet.mvc.method.annotation.requestmappinghandleradapter.handleinternal(requestmappinghandleradapter.java:738) ~[spring-webmvc-4.3.1.release.jar:4.3.1.release]      @ org.springframework.web.servlet.mvc.method.abstracthandlermethodadapter.handle(abstracthandlermethodadapter.java:85) ~[spring-webmvc-4.3.1.release.jar:4.3.1.release]      @ org.springframework.web.servlet.dispatcherservlet.dodispatch(dispatcherservlet.java:963) [spring-webmvc-4.3.1.release.jar:4.3.1.release]      @ org.springframework.web.servlet.dispatcherservlet.doservice(dispatcherservlet.java:897) [spring-webmvc-4.3.1.release.jar:4.3.1.release]      @ org.springframework.web.servlet.frameworkservlet.processrequest(frameworkservlet.java:970) [spring-webmvc-4.3.1.release.jar:4.3.1.release]      @ org.springframework.web.servlet.frameworkservlet.doget(frameworkservlet.java:861) [spring-webmvc-4.3.1.release.jar:4.3.1.release]      @ javax.servlet.http.httpservlet.service(httpservlet.java:635) [servlet-api.jar:?]      @ org.springframework.web.servlet.frameworkservlet.service(frameworkservlet.java:846) [spring-webmvc-4.3.1.release.jar:4.3.1.release]      @ javax.servlet.http.httpservlet.service(httpservlet.java:742) [servlet-api.jar:?]      @ org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:231) [catalina.jar:8.5.16]      @ org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:166) [catalina.jar:8.5.16]      @ org.springframework.web.filter.characterencodingfilter.dofilterinternal(characterencodingfilter.java:197) [spring-web-4.3.1.release.jar:4.3.1.release]      @ org.springframework.web.filter.onceperrequestfilter.dofilter(onceperrequestfilter.java:107) [spring-web-4.3.1.release.jar:4.3.1.release]      @ org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:193) [catalina.jar:8.5.16]      @ org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:166) [catalina.jar:8.5.16]      @ org.apache.tomcat.websocket.server.wsfilter.dofilter(wsfilter.java:52) [tomcat-websocket.jar:8.5.16]      @ org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:193) [catalina.jar:8.5.16]      @ org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:166) [catalina.jar:8.5.16]      @ org.apache.catalina.core.standardwrappervalve.invoke(standardwrappervalve.java:198) [catalina.jar:8.5.16]      @ org.apache.catalina.core.standardcontextvalve.invoke(standardcontextvalve.java:96) [catalina.jar:8.5.16]      @ org.apache.catalina.authenticator.authenticatorbase.invoke(authenticatorbase.java:478) [catalina.jar:8.5.16]      @ org.apache.catalina.core.standardhostvalve.invoke(standardhostvalve.java:140) [catalina.jar:8.5.16]      @ org.apache.catalina.valves.errorreportvalve.invoke(errorreportvalve.java:80) [catalina.jar:8.5.16]      @ org.apache.catalina.valves.abstractaccesslogvalve.invoke(abstractaccesslogvalve.java:624) [catalina.jar:8.5.16]      @ org.apache.catalina.core.standardenginevalve.invoke(standardenginevalve.java:87) [catalina.jar:8.5.16]      @ org.apache.catalina.connector.coyoteadapter.service(coyoteadapter.java:342) [catalina.jar:8.5.16]      @ org.apache.coyote.http11.http11processor.service(http11processor.java:799) [tomcat-coyote.jar:8.5.16]      @ org.apache.coyote.abstractprocessorlight.process(abstractprocessorlight.java:66) [tomcat-coyote.jar:8.5.16]      @ org.apache.coyote.abstractprotocol$connectionhandler.process(abstractprotocol.java:868) [tomcat-coyote.jar:8.5.16]      @ org.apache.tomcat.util.net.nioendpoint$socketprocessor.dorun(nioendpoint.java:1455) [tomcat-coyote.jar:8.5.16]      @ org.apache.tomcat.util.net.socketprocessorbase.run(socketprocessorbase.java:49) [tomcat-coyote.jar:8.5.16]      @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1142) [?:1.8.0_131]      @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:617) [?:1.8.0_131]      @ org.apache.tomcat.util.threads.taskthread$wrappingrunnable.run(taskthread.java:61) [tomcat-util.jar:8.5.16]      @ java.lang.thread.run(thread.java:748) [?:1.8.0_131] caused by: java.io.ioexception: server returned http response code: 400 url: http://localhost:8080/      @ sun.net.www.protocol.http.httpurlconnection.getinputstream0(httpurlconnection.java:1876) ~[?:1.8.0_131]      @ sun.net.www.protocol.http.httpurlconnection.getinputstream(httpurlconnection.java:1474) ~[?:1.8.0_131]      @ org.springframework.http.client.simpleclienthttpresponse.getbody(simpleclienthttpresponse.java:85) ~[spring-web-4.3.1.release.jar:4.3.1.release]      @ com.my.code.web.integration.client.rest.bufferingclienthttpresponsewrapper.getbody(bufferingclienthttpresponsewrapper.java:46) ~[classes/:?]      @ com.my.code.web.integration.client.rest.loggingrequestinterceptor.traceresponse(loggingrequestinterceptor.java:66) ~[classes/:?]      @ com.my.code.web.integration.client.rest.loggingrequestinterceptor.intercept(loggingrequestinterceptor.java:34) ~[classes/:?]      @ org.springframework.http.client.interceptingclienthttprequest$interceptingrequestexecution.execute(interceptingclienthttprequest.java:85) ~[spring-web-4.3.1.release.jar:4.3.1.release]      @ org.springframework.http.client.interceptingclienthttprequest.executeinternal(interceptingclienthttprequest.java:69) ~[spring-web-4.3.1.release.jar:4.3.1.release]      @ org.springframework.http.client.abstractbufferingclienthttprequest.executeinternal(abstractbufferingclienthttprequest.java:48) ~[spring-web-4.3.1.release.jar:4.3.1.release]      @ org.springframework.http.client.abstractclienthttprequest.execute(abstractclienthttprequest.java:53) ~[spring-web-4.3.1.release.jar:4.3.1.release]      @ org.springframework.web.client.resttemplate.doexecute(resttemplate.java:619) ~[spring-web-4.3.1.release.jar:4.3.1.release]      ... 47 more 

question:

why exception org.springframework.web.client.resourceaccessexception thrown, not org.springframework.web.client.httpclienterrorexception, what's happening handling request when webapplicationexception occurs?

the resourceaccessexception exception not have methods allow me read http status, eg getrawstatuscode (), getresponseheaders(), etc.

because it's wrapping ioexception, not looking @ actual response code.

the root cause appears because bufferingclienthttpresponsewrapper trying read body without checking if there 1 first.


Comments

Popular posts from this blog

networking - Vagrant-provisioned VirtualBox VM is not reachable from Ubuntu host -

c# - ASP.NET Core - There is already an object named 'AspNetRoles' in the database -

ruby on rails - ArgumentError: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true -