为了从xwiki Rest API获取Json Content,我使用了RestTemplate的方法RestTemplate.exchange(..),但是我找到了404找不到的错误。 Uri包含%2520而不是%20。
这是我的代码:
public static void main(String[] args) throws IOException{
private static final String link = "http://swininducasa02.ptx.fr.sopra:8080/xwiki-enterprise-web-8.4.5/rest/wikis/xwiki/spaces/Main/spaces/PIC/spaces/Serveur%20de%20virtualisation/spaces/Serveur%20de%20Marcoussis/spaces/choix/spaces/SGBFV/pages/WebHome";
String plainCreds = "user:passwd";
byte[] plainCredsBytes = plainCreds.getBytes();
byte[] base64CredsBytes = Base64.encodeBase64(plainCredsBytes);
String base64Creds = new String(base64CredsBytes);
HttpHeaders headers = new HttpHeaders();
headers.add("Authorization", "Basic " + base64Creds);
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
RestTemplate restTemplate = new RestTemplate();
System.out.println("Begin /GET request!");
HttpEntity<String> request = new HttpEntity<>(headers);
ResponseEntity<String> response = restTemplate.exchange(link+ "?media=json", HttpMethod.GET, request, String.class);
Map<String, String> map = new ObjectMapper().readValue(response.getBody(), Map.class);
System.out.println( map.get("content"));
}
我搜索过&amp;我发现问题涉及Content-Type。我尝试了以下所有行,但它仍无效。
headers.add( “内容类型”,MediaType.APPLICATION_JSON_UTF8_VALUE);
headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
UriComponents uriComponents = UriComponentsBuilder.fromPath(link).build(true);
URI uri= uriComponents.toUri();
以下是我想要访问的链接:
但我的要求是:
角色%20变为%2520。我使用交换,因为我必须设置Auth标头
拜托,你能帮助我吗?
答案 0 :(得分:0)
exchange(...)
第二次对您的URI进行编码。尝试使用UriComponentsBuilder.build(true)
构建URI(true
表示您的URI已经编码)。我在你的帖子中看到你说你试过了,但看起来你把它转换成字符串时把它转换成exchange()
这可能导致它第二次被编码。
UriComponents uriComponents = UriComponentsBuilder.fromPath(link).queryParam("media","json").build(true);
URI uri= uriComponents.toUri();
或
UriComponents uriComponents = UriComponentsBuilder.fromHttpUrl(path + "?media=json").build(true);
也应该没问题。然后使用它,做
ResponseEntity<String> response = restTemplate.exchange(uri, HttpMethod.GET, request, String.class);
而不是
ResponseEntity<String> response = restTemplate.exchange(uri.toString()+ "?media=json", HttpMethod.GET, request, String.class);