RestTemplate Get request with Brackets and Slash

1k Views Asked by At

I am using below URL with RestTemplate to get data http://localhost:8161/console/jolokia/exec/org.apache.activemq.artemis:broker="0.0.0.0"/listQueues/{"field":"","operation":"","value":""}/1/100

And my code looks like below

HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setBasicAuth(user, pass);
HttpEntity<String> httpEntity = new HttpEntity<>(httpHeaders);
            
MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
converter.setSupportedMediaTypes(MediaType.parseMediaTypes(Arrays.asList("text/plain;charset=utf-8", "application/json")));
restTemplate.getMessageConverters().add(0, converter);

ResponseEntity<Object> response = restTemplate.exchange(url, HttpMethod.GET, httpEntity, Object.class);

From the above code I am getting below error

{request={mbean=org.apache.activemq.artemis:broker="0.0.0.0", arguments=[%7B"field":"","operation":"","value":""%7D, 1, 100], type=exec, operation=listQueues}, error_type=javax.json.stream.JsonParsingException, error=javax.json.stream.JsonParsingException : Unexpected character '%' (Codepoint: 37) on [lineNumber=1, columnNumber=2, streamOffset=1]. Reason is [[Expected structural character or digit or 't' or 'n' or 'f' or '-']], status=500}

Is there any way to pass {} with / in the URL by RestTempate ?

I have tried below solutions but it is not working.

https://stackoverflow.com/questions/43917408/resttemplate-request-with-braces

https://stackoverflow.com/questions/41267993/how-to-properly-escape-a-url-to-be-used-in-resttemplate-that-has-flower-brackets?noredirect=1&lq=1

Can anyone please help me in this.

1

There are 1 best solutions below

0
On

After doing long search I found below solution and it is working for me

URI uri = UriComponentsBuilder.fromUriString(url).build().encode().toUri();
ResponseEntity<Object> response = restTemplate.exchange(uri, HttpMethod.GET, httpEntity, Object.class);