RestTemplate获取带有括号和斜杠的请求



我使用下面的URL和RestTemplate来获取数据http://localhost:8161/console/jolokia/exec/org.apache.activemq.artemis:broker="0.0.0.0"/listQueues/{"字段":"操作":"值":"}/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

有人能帮我吗。

经过长时间的搜索,我找到了下面的解决方案,它对我来说是有效的

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

相关内容

  • 没有找到相关文章

最新更新