HttpClientErrorException 400 null using RestTemplate in microServices

resttemplate.exchange returns null
org.springframework.web.client.httpclienterrorexception: 400 null
org springframework web client httpclienterrorexception: 404 null
resttemplate 400 bad request
org.springframework.web.client.httpclienterrorexception: 415 null
resttemplate.exchange not working
resttemplate exchange response body null
resttemplate exchange null pointer exception

I have two microservices. The first one receives a call from the Frontend and then it calls to the second uService to receive some data. The last is returning an error response (Bad Request, this is ok - it is a use-case). However, I am losing the body (message) returned from the second microservice, as the first is throwing a HttpClientErrorException 400 null in the call

This is my code:

ResponseEntity<MyEntity> entityResponse = restTemplate.getForEntity(url, MyEntity.class, id);

I am not able to do entityResponse.getStatusCode() as an exception is thrown.

Handled it in the ControllerAdvice, my exception message is "400 null" even I return a custom message from the service.

So, I would like to get the response message sent in the called uservice to manage it.

Thanks in advance.


The answers here that explain how to catch the exception and access the body are correct. However, you may use a different approach. You can use a 3-d party library that sends Http request and handles the response. One of the well-known products would be Apache commons HTTPClient: HttpClient javadoc, HttpClient Maven artifact. There is by far less known but much simpler HTTPClient (part of an open source MgntUtils library written by me): MgntUtils HttpClient javadoc, MgntUtils maven artifact, MgntUtils Github. Using either of those libraries you can send your REST request and receive response independently from Spring as part of your business logic

RestTemplate post method is not working with exchange method., I'm using spring boot microservice project. I have a rest HttpClientErrorException: 400 null**] with root cause org.springframework.web.​client. However, I am losing the body (message) returned from the second microservice, as the first is throwing a HttpClientErrorException 400 null in the call This is my code: ResponseEntity<MyEntity> entityResponse = restTemplate.getForEntity(url, MyEntity.class, id);


What I'm doing in my project is the following.

MicroService_2 calls MicroService_1.

MicroService_1

MicroService_1 returns for example a HTTP 404 exception if the entity isn't found.

@RestController
@RequestMapping(value = "/api/v1/")
public class Service1Controller {


    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public @ResponseBody MyEntity getMyEntity(@PathVariable String id) throws NotFoundException {
        MyEntity result = ...
        if(result == null) {
            throw new NotFoundException("MyEntity [id: "+id+"] not found");
        }

        return result;
    }


    @ControllerAdvice
    public class RestEndpointExceptionHandler extends RestExceptionHandler {

        @ExceptionHandler(NotFoundException.class)
        public ResponseEntity<String> handleNotFoundException(HttpServletRequest req, NotFoundException ex) throws NotFoundException {
            return new ResponseEntity<String>(ex.getMessage(), HttpStatus.NOT_FOUND);
        }
    }
}

MicroService_2

The MicroService_2 calls MicroService_1 and catches the exception by HTTP code and regenerate the NotFoundException.

@Override
public MyEntity getMyEntity(Principal principal) {
    try {
        ResponseEntity<MyEntity> entityResponse = restTemplate.getForEntity(url, MyEntity.class, id);

        return entityResponse.getBody();

    } catch(HttpClientErrorException e) {
        HttpStatus status = e.getStatusCode();
        if (status == HttpStatus.NOT_FOUND) { 
            throw new NotFoundException(e.getResponseBodyAsString()); // should be "MyEntity [id: {id}] not found"
        } else {
            throw new UnexpectedServerException(e.getResponseBodyAsString());
        }
    }
}

How to fix the error org.springframework.web.client , HttpClientErrorException: 400 null, when using RestTemplate to I am trying to access a microservice using RestTemplate as the following: Oracle summarises the purpose of unchecked exceptions as: The next question might be: "If it's so good to document a method's API, including the exceptions it can throw, why not specify runtime


Java Examples for org.springframework.web.client , Project: microservices-demo-master File: WebAccountsService.java View source code () invoked: for " + name); Account[] accounts = null; try { accounts = restTemplate. returned an HTTP 400: Bad Request status code"); } catch (​HttpClientErrorException e) { assertThat(e. getPassword() + ", using algorithm " + response. In this Microservices Architecture Spring Boot tutorial, we will discuss to creating a microservices with spring and will see microservices architecture. Microservices allow large systems to be built up from a number of collaborating components. Microservices allows doing loose coupling between application processes instead of loose coupling


Resttemplate exception response body, Exception Handling with RestTemplate, Spring Framework Response 401 is class using that easily communication between microservices is possible. I need to pass headers as HttpClientErrorException: 400 null**] with root cause org. By the way when I'm doing a CURL directly in my swagger it works but in my Android client it always returns HttpClientErrorException code 400 Bad Request. AndroidAnnotations 4.3.1 : Rest Swagger Setup :


Spring Boot RestTemplate GET Example, Learn to use RestTemplate to invoke REST GET API verify api response By Lokesh Gupta | Filed Under: Spring Boot RestTemplate HttpEntity<Employee> requestEntity = new HttpEntity<>( null , headers); catch (​HttpClientErrorException ex). {. //Verify bad request and missing header. Assert.​assertEquals( 400 , ex. About this page This is a preview of a SAP Knowledge Base Article. Click more to access the full version on SAP ONE Support launchpad (Login required). Search for additional results


org.springframework.web.client.HttpClientErrorException: 404 null, RestTemplate.execute() has thrown a HttpClientErrorException. Spring Web. 2. 55. 83 Eureka Tests fail with Java 9. via GitHub by chaitan64arun, 2 years ago. https://github.com/ewolff/microservice/issues/20. 404 null 400 Bad Request  Construct a new instance of HttpClientErrorException based on a HttpStatus, status text, and respons