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

We Are Going To Discuss About org.springframework.web.client.HttpClientErrorException: 400 Bad Request. So lets Start this Java Article.

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

  1. 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");

  2. 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");

Solution 1

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.

Original Author Arun Of This Content

Conclusion

So This is all About This Tutorial. Hope This Tutorial Helped You. Thank You.

Also Read,

Siddharth

I am an Information Technology Engineer. I have Completed my MCA And I have 4 Year Plus Experience, I am a web developer with knowledge of multiple back-end platforms Like PHP, Node.js, Python and frontend JavaScript frameworks Like Angular, React, and Vue.

Leave a Comment