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

拈花ヽ惹草 提交于 2019-12-10 15:30:08

问题


This is our rest template config

@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 which works successfully with Postman and returns proper response.

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 invocation fails with below error. 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 on this regard will be very helpful.


回答1:


To get more people know this issue:

@Sameer, I found the problem is you're using HttpHeaders ,you can try to build your header with this code
MultiValueMap<String, String> headers =new LinkedMultiValueMap<String, String>(); And not use the HttpHeaders and then in your HttpEntity to build the object entity as
new HttpEntity<Object>(record, headers); Then it should solve problem .




回答2:


Try creating the ResTemplate object in the following way,

ResTemplate restTemplate = new RestTemplate(new HttpComponentsClientHttpRequestFactory()); HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_JSON); headers.set("Authorization", authorizationProperty); //Can also use add(String headerName, String headerValue)

This way it works.



来源:https://stackoverflow.com/questions/32851267/spring-resttemplate-unsupportedoperationexception-with-collectionsunmodifiablec

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!