Spring RestTemplate UnsupportedOperationException with $ UnmodifiableCollection.add Collections (Unknown source)

This is our holiday pattern configuration.

@Bean
    public RestTemplate infoBloxRestTemplate() {
        RestTemplate restTemplate=new RestTemplate();
        ArrayList<ClientHttpRequestInterceptor> interceptors = new ArrayList<>();
        interceptors.add(httpBasicAuthenticationInterceptor());
        restTemplate.setInterceptors(interceptors);
        restTemplate.getMessageConverters().add(jacksonConverter());
        restTemplate.setRequestFactory(genericHttpRequestFactory());
        return restTemplate;
    }

We are trying to make a POST call that works successfully with Postman and returns the correct answer.

final HttpHeaders headers = new HttpHeaders();
headers.add("Accept", "application/json");
headers.add("Content-Type", "application/json");

HttpEntity<Object> httpEntity = new HttpEntity<Object>(record, headers);

StringBuilder uri = new StringBuilder(infobloxRestClient.createUrl("/record:host"));
infobloxRestClient.getRestTemplate().exchange(uri.toString(), HttpMethod.POST, httpEntity, String.class);

But this POST call fails with the error below. Here is my stack trace:

com.sun.xml.ws.server.sei.TieHandler createResponse
SEVERE: null
java.lang.UnsupportedOperationException
    at java.util.Collections$UnmodifiableCollection.add(Unknown Source)
    at org.springframework.http.HttpHeaders.add(HttpHeaders.java:558)
    at com.test.externalinterfaces.HTTPBasicAuthenticationInterceptor.intercept(HTTPBasicAuthenticationInterceptor.java:30)
    at org.springframework.http.client.InterceptingClientHttpRequest$RequestExecution.execute(InterceptingClientHttpRequest.java:81)
    at org.springframework.http.client.InterceptingClientHttpRequest.executeInternal(InterceptingClientHttpRequest.java:67)
    at org.springframework.http.client.AbstractBufferingClientHttpRequest.executeInternal(AbstractBufferingClientHttpRequest.java:46)
    at org.springframework.http.client.AbstractClientHttpRequest.execute(AbstractClientHttpRequest.java:49)
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:488)
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:452)

Any help in this regard would be very helpful.

+4
source share
1 answer

To find out more about this issue:

@Sameer, , HttpHeaders,
MultiValueMap<String, String> headers =new LinkedMultiValueMap<String, String>(); HttpHeaders, HttpEntity
new HttpEntity<Object>(record, headers); .

0

Source: https://habr.com/ru/post/1609525/


All Articles