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 + "/xxx ...";
            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

Recommended Answers

All 3 Replies

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.

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

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.

commented: Many chefs make a fine stew. But not much else. +12
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.