7

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.

Sameer
  • 425
  • 1
  • 7
  • 15
  • Your configuration chain fails, because the header cannot be changed afterwards. Have a look at http://stackoverflow.com/questions/14383240/basic-authentication-with-resttemplate-3-1 – Hannes Sep 29 '15 at 19:41

2 Answers2

2

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 .

Alter Hu
  • 739
  • 1
  • 7
  • 15
0

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)

This way it should works.

Fmerco
  • 1,160
  • 5
  • 20