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

查看:123
本文介绍了Spring RestTemplate UnsupportedOperationException with Collections$UnmodifiableCollection.add(Unknown Source)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我们的休息模板配置

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;
    }

我们正在尝试进行 POST 调用,该调用可与 Postman 成功配合并返回正确的响应.

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);

但是此 POST 调用失败并显示以下错误.这是我的堆栈跟踪:

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.

推荐答案

尝试通过以下方式创建 RestTemplate 对象,

Try creating the RestTemplate 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)

这样它应该可以工作.

这篇关于Spring RestTemplate UnsupportedOperationException with Collections$UnmodifiableCollection.add(Unknown Source)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆