0

我使用下面的 URL 和 RestTemplate 来获取数据 http://localhost:8161/console/jolokia/exec/org.apache.activemq.artemis:broker="0.0.0.0"/listQueues/{"field":"", "操作":"","值":""}/1/100

我的代码如下所示

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);

从上面的代码我得到以下错误

{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}

有什么方法可以通过 RestTempate 在 URL 中使用 / 传递 {} 吗?

我尝试了以下解决方案,但它不起作用。

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

任何人都可以在这方面帮助我。

4

1 回答 1

0

经过长时间的搜索后,我找到了以下解决方案,它对我有用

URI uri = UriComponentsBuilder.fromUriString(url).build().encode().toUri();
ResponseEntity<Object> response = restTemplate.exchange(uri, HttpMethod.GET, httpEntity, Object.class);
于 2020-12-04T20:53:02.320 回答