【问题标题】:REST API, can't put "%20" or "+" in my URL for spaces. The URL isn't being fetchedREST API,不能在我的 URL 中输入“%20”或“+”作为空格。未获取 URL
【发布时间】:2018-09-28 15:25:18
【问题描述】:

我从控制台收到错误消息 org.springframework.web.client.HttpClientErrorException: 400 null,这意味着我的 URL 发送了服务器无法理解的错误请求。

但是,每当我输入一个没有 %20+ 符号的 URL 时

示例: http://ksr-ca-qmaltjira.ca.kronos.com:8061/rest/api/2/search?jql=project=SUP&maxResults=2 有效,我的数据显示在我的本地页面上。

String username = "confidential";
String password = "confidential";

private static final String jiraBaseURL = "http://ksr-ca-qmaltjira.ca.kronos.com:8061/rest/api/search?jql=project%3D%22Customer%20Support%22%20%26%20type%20%3D%20%22Change%20Request%22%20&maxResults=2";
private RestTemplate restTemplate;
private HttpHeaders httpHeaders;





private HttpHeaders createHeadersWithAuthentication() {
    String plainCreds = username + ":" + password;
    byte[] base64CredsBytes = Base64.getEncoder().encode(plainCreds.getBytes());
    String base64Creds = new String(base64CredsBytes);

    HttpHeaders headers = new HttpHeaders();
    headers.add("Authorization", "Basic " + base64Creds);

    return headers;
}




@RequestMapping("/cr-follow-up")
public @ResponseBody ResponseEntity<String> getData()
{
    restTemplate = new RestTemplate();
    httpHeaders = createHeadersWithAuthentication();
    httpHeaders.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
    HttpEntity<String> entity = new HttpEntity<String>(httpHeaders);
    ResponseEntity<String> response = restTemplate.exchange(jiraBaseURL, HttpMethod.GET, entity, String.class);

    return response;
}

【问题讨论】:

  • 您是否尝试过使用诸如 spring UriComponentsBuilder 之类的 uri 构建器?
  • 如果你输入一个空格会发生什么。
  • 当你的意思是添加时你做了一个uriencode吗?
  • UriComponentsBuilder我没试过,嗯我试试
  • 如果我使用空间它不起作用

标签: java rest spring-boot jira-rest-api


【解决方案1】:

尝试使用URLEncoder.encode() 编码您的网址

【讨论】:

  • 我试试看
  • 然后尝试使用对象 URL。它应该根据需要对其进行格式化。
  • 我将编辑我的代码帖子并向你展示我如何返回我的字符串,看看你是否有任何想法来解决我的问题:/
  • 问题出在我的网址中 100%
  • 这个答案有帮助吗? stackoverflow.com/questions/20885521/…
猜你喜欢
  • 1970-01-01
  • 2022-01-25
  • 2010-12-10
  • 1970-01-01
  • 2011-12-27
  • 2015-01-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多