I wonder how to handle exception in ResponsesEntity.
When it receive wrong url, it supposes to go to the catch block and display the log. But I keep getting error message in console. The try-catch not working as it not display log message.

        public ResponseEntity info(@PathVariable("Id") String id) {
            HttpEntity httpEntity = new HttpEntity(buildHttpHeaders());
            String theUrl = url + "/revenue_harvest/transactions/" +id;
            ResponseEntity<TDto> responseEntity = restTemplate.exchange(theUrl, HttpMethod.GET, httpEntity, TDto.class);
            TDto tdto = responseEntity.getBody();
            Tran tran = new Tran();
            try {
                  tran.setDate(new Date());
                  tran.saveAndFlush(tran); // save to database
            }catch(Exception e)
                log.info("Log: "+e);
            return responseEntity;


    org.springframework.web.client.HttpClientErrorException: 404 Not Found
        at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:63)
        at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:700)
        at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:653)
        at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:613)
        at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:531)
        at com.rh.tranglo.controller.ApiController.retrieveTransactionInfo(ApiController.java:41)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205)
        at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:133)
        at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:97)

Is there a way to handle exception by displaying the log in catch block?

Also posted at https://stackoverflow.com/questions/44952572/handle-exception-in-responsesentity-java

Edited by John_165

10 Months
Discussion Span
Last Post by JamesCherrill

There's an active discussion ongoing in S.O. for this question, so there's no need to reply here until/unless OP comes back first.

Yes,I am here

Edited by John_165


Yes, but the guys at SO seem to be doing a good job, so there's no point duplicating it unless you get stuck again.

Votes + Comments
Many chefs make a fine stew. But not much else.
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.