gpt4 book ai didi

java - 带有 Collections$UnmodifiableCollection.add(未知来源)的 Spring RestTemplate UnsupportedOperationException

转载 作者:搜寻专家 更新时间:2023-11-01 00:55:34 25 4
gpt4 key购买 nike

这是我们的rest模板配置

@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 配合使用并返回正确的响应。

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 调用失败并出现以下错误。这是我的堆栈跟踪:

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)

在这方面的任何帮助都会非常有帮助。

最佳答案

为了让更多人知道这个问题:

@Sameer,我发现问题是你正在使用 HttpHeaders ,你可以尝试用这段代码构建你的标题

MultiValueMap<String, String> headers =new LinkedMultiValueMap<String, String>();
并且不使用 HttpHeaders然后在你的 HttpEntity将对象实体构建为
new HttpEntity<Object>(record, headers);
然后它应该解决问题。

关于java - 带有 Collections$UnmodifiableCollection.add(未知来源)的 Spring RestTemplate UnsupportedOperationException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32851267/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com