Httpclienterrorexception 400 Null Using Resttemplate in Microservices

HttpClientErrorException 400 null using RestTemplate in microServices

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

org.springframework.web.client.HttpClientErrorException: 400 Bad Request

This is what worked for me. Issue is earlier I didn't set Content Type(header) when I used exchange method.

MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>();
map.add("param1", "123");
map.add("param2", "456");
map.add("param3", "789");
map.add("param4", "123");
map.add("param5", "456");

HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);

final HttpEntity<MultiValueMap<String, String>> entity = new HttpEntity<MultiValueMap<String, String>>(map ,
headers);
JSONObject jsonObject = null;

try {
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<String> responseEntity = restTemplate.exchange(
"https://url", HttpMethod.POST, entity,
String.class);

if (responseEntity.getStatusCode() == HttpStatus.CREATED) {
try {
jsonObject = new JSONObject(responseEntity.getBody());
} catch (JSONException e) {
throw new RuntimeException("JSONException occurred");
}
}
} catch (final HttpClientErrorException httpClientErrorException) {
throw new ExternalCallBadRequestException();
} catch (HttpServerErrorException httpServerErrorException) {
throw new ExternalCallServerErrorException(httpServerErrorException);
} catch (Exception exception) {
throw new ExternalCallServerErrorException(exception);
}

ExternalCallBadRequestException and ExternalCallServerErrorException are the custom exceptions here.

Note: Remember HttpClientErrorException is thrown when a 4xx error is received. So if the request you send is wrong either setting header or sending wrong data, you could receive this exception.

Spring RestTemplate : BadRequest 400,null. while calling Get Request

restTemplate.getForObject(String url, Class responseType, Object... uriVariables) is supposed to deal with uri variables like {myVar} in your String url.

If you want to set query parameters, consider using the UriComponentsBuilder.

URI uri = UriComponentsBuilder.fromHttpUrl("http://localhost:8064/spacestudy/" +instituteIdentifier +"/communication/alertmanagement/getDepartmentInstitute")
.queryParam("nUserId", nUserId)
.queryParam("nDeptInst", nInstId)
.build().toUri();

Integer nInstTo = restTemplate.getForObject(uri,Integer.class);

Or you can set them manually like :

String uri = "http://localhost:8064/spacestudy/" +instituteIdentifier +"/communication/alertmanagement/getDepartmentInstitute"
+ "?nUserId=" + nUserId + "&nDeptInst=" + nInstId;

I prefer the UriComponentsBuilder solution.

Spring HttpClientErrorException provides no details from response body

The response body is actually a property on HttpClientErrorException. It can be accessed via the following two accessors which it inherits from its parent class HttpStatusCodeException:

public byte[] getResponseBodyAsByteArray()
public String getResponseBodyAsString()

RestTemplate Spring boot 400 error Servlet.service() for servlet [dispatcherServlet] in context with path [] threw excepti. One method works rest dont

400 bad request is due to the missing query parameter of that API.

try to provide url as below in rest template

UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(url)
// Add query parameter
.queryParam("customernumber", customernumber);
HttpEntity<?> request = new HttpEntity<>(requestHeaders);

ResponseEntity<Account> response = restTemplate.exchange(builder.toUriString(), HttpMethod.GET, request, Account.class);
return response;


Related Topics



Leave a reply



Submit