Spring RestTemplate with rootUri return URI is not absolute error

mohammad_1m2 picture mohammad_1m2 · Jan 15, 2019 · Viewed 7.2k times · Source

I have a RestTemplate that I build it with RestTemplateBuilder. I set the rootUri for builder. In below method (updateState1) sometimes I got the "URI is not absolute" error. For example when I called this method concurrently for 2 times I often got 1 error.

EDIT and Solution: I use this RestTemplate in service task of camunda process. I launch this project in kubernetes container that has different timezone with the oracle database. When I add timezone variable every things work fine.

Spring boot version: 2.1.1.RELEASE

Here is my code:

@Component
@Slf4j
public class CoreServiceClient {

    private RestTemplate restTemplate;

    private static final String root = "http://localhost:8080/test/api/";

    public CoreServiceClient(RestTemplateBuilder restTemplateBuilder) {
        restTemplate = restTemplateBuilder.rootUri(root).build();
    }


    public void updateState1(UpdateParam updateParam) {
        HttpHeaders headers = generateHeader();
        UpdateRequest updateRequest = new UpdateRequest(updateParam.getState());

        HttpEntity<UpdateRequest> httpEntity = new HttpEntity<>(updateRequest, headers);

        ResponseEntity<String> response = restTemplate.exchange(
                "/food/{id}/state",
                HttpMethod.PUT, httpEntity, String.class, updateParam.getId());

    }
    public void updateState2(String id) {
        HttpHeaders headers = generateHeader();
        UpdateRequest updateRequest = new UpdateRequest("done");

        HttpEntity<UpdateRequest> httpEntity = new HttpEntity<>(updateRequest, headers);

        ResponseEntity<String> response = restTemplate.exchange(
                "/food/{id}/state",
                HttpMethod.PUT, httpEntity, String.class, id);

    }
}

cuase (stacktrace):

Caused by: java.lang.IllegalArgumentException: URI is not absolute
    at java.net.URI.toURL(URI.java:1088)
    at org.springframework.http.client.SimpleClientHttpRequestFactory.createRequest(SimpleClientHttpRequestFactory.java:145)
    at org.springframework.http.client.support.HttpAccessor.createRequest(HttpAccessor.java:87)
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:730)
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:669)
    at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:578)
    at com.test.client.CoreServiceClient.updateState(CoreServiceClient.java:39)
    at sun.reflect.GeneratedMethodAccessor263.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.camunda.bpm.engine.impl.javax.el.BeanELResolver.invoke(BeanELResolver.java:479)
    ... 85 more

Answer

stacker picture stacker · Jan 15, 2019

Remove / in root:

private static final String root = "http://localhost:8080/test/api";

RestTemplate accepts uriTemplate as long as they start with / so your root should be without it. if it doesn't start with / it will consider it as a full URL