Search code examples
javaspring-bootresttemplate

org.springframework.web.client.HttpClientErrorException: 401 null


I am calling a service from one module to another module. but it throws an exception:

org.springframework.web.client.HttpClientErrorException: 401 null

POST request for "http://localhost:80../.../...Report" resulted in 401 (null); invoking error handler.

I can able to hit this service from post man directly. But when its called from spring boot module it throws error.

BaseResponseDTO baseResponseDTO = restTemplate.postForObject("http://localhost:80../.../...Report", requestDTO, BaseResponseDTO.class);

error throws in this line


Solution

  • I added authorization headers in MultiValueMap and i added object request in HttpEntity. here i added code.
    
    MultiValueMap<String, String> headers = new LinkedMultiValueMap<String, String>();
    headers.add("Authorization", "bearer "+<TOKEN>);
    HttpEntity<MyObject> httpEntityRequest = new HttpEntity<>(myObjectReference, headers);
    
    BaseResponseDTO baseResponseDTO = restTemplate.postForObject("http://localhost:80../.../...Report", httpEntityRequest , BaseResponseDTO.class);