Spring RestTemplate with rootUri return URI is not absolute error Spring RestTemplate with rootUri return URI is not absolute error kubernetes kubernetes

Spring RestTemplate with rootUri return URI is not absolute error


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


I try with the same code. I do not get this error. Spring boot version: 1.5.0.RELEASE

Instead of POST, I tried with a GET API with same URL pattern.The / at the end of the path does not matter.

@Componentpublic class CoreServiceClient {    private RestTemplate restTemplate;    private static final Logger LOGGER = LoggerFactory.getLogger(CoreServiceClient.class);    private static final String root = "http://localhost:8080/test/api/";    public CoreServiceClient(RestTemplateBuilder restTemplateBuilder) {        restTemplate = restTemplateBuilder.rootUri(root).build();    }    public void updateState(String id) {        try {            ResponseEntity<String> response =                    restTemplate.exchange("/food/{id}/state", HttpMethod.GET, null, String.class, id);            LOGGER.info("Resp: {}", response.getStatusCode());            LOGGER.info("Resp: {}", response.getBody());        } catch (Exception e) {            LOGGER.error(e.getMessage(), e);        }    }}

I added a dummy controller with the same path:

@RestController@RequestMapping("/test/api")public class FooController {    @GetMapping("/food/{id}/state")    public ResponseEntity<String> fooState(@PathVariable String id) {        return new ResponseEntity<String>("EATING", HttpStatus.OK);    }}

To test, I added another controller:

@RestController@RequestMapping("/client")public class CoreServiceClientController {    @Autowired    private CoreServiceClient client;    @GetMapping    public ResponseEntity<String> goGet() {        client.updateState("1001");        return new ResponseEntity<>("HELLO", HttpStatus.OK);    }}

Everything works fine for me.

Log:

2019-01-15 23:23:19.870  INFO 22570 --- [nio-8080-exec-1] com.example.demo001.CoreServiceClient    : Resp: 2002019-01-15 23:23:19.871  INFO 22570 --- [nio-8080-exec-1] com.example.demo001.CoreServiceClient    : Resp: EATING